📄 jxdatepicker.java
字号:
} /** * Sets the string used to identify fired ActionEvents. * * @param actionCommand The string used for identifying ActionEvents. */ public void setActionCommand(String actionCommand) { _actionCommand = actionCommand; } /** * Adds an ActionListener. * <p> * The ActionListener will receive an ActionEvent when a selection has * been made. * * @param l The ActionListener that is to be notified */ public void addActionListener(ActionListener l) { listenerList.add(ActionListener.class, l); } /** * Removes an ActionListener. * * @param l The action listener to remove. */ public void removeActionListener(ActionListener l) { listenerList.remove(ActionListener.class, l); } /** * Fires an ActionEvent to all listeners. */ protected void fireActionPerformed() { Object[] listeners = listenerList.getListenerList(); ActionEvent e = null; for (int i = listeners.length - 2; i >= 0; i -=2) { if (listeners[i] == ActionListener.class) { if (e == null) { e = new ActionEvent(JXDatePicker.this, ActionEvent.ACTION_PERFORMED, _actionCommand); } ((ActionListener)listeners[i + 1]).actionPerformed(e); } } } /** * {@inheritDoc} */ public void doLayout() { int width = getWidth(); int height = getHeight(); Insets insets = getInsets(); _dateField.setBounds(insets.left, insets.bottom, width - _popupButtonWidth, height); _popupButton.setBounds(width - _popupButtonWidth + insets.left, insets.bottom, _popupButtonWidth, height); } /** * {@inheritDoc} */ public Dimension getMinimumSize() { return getPreferredSize(); } /** * {@inheritDoc} */ public Dimension getPreferredSize() { Dimension dim = _dateField.getPreferredSize(); dim.width += _popupButton.getPreferredSize().width; Insets insets = getInsets(); dim.width += insets.left + insets.right; dim.height += insets.top + insets.bottom; return dim; } /** * Action used to commit the current value in the JFormattedTextField. * This action is used by the keyboard bindings. */ private class TogglePopupAction extends AbstractAction { public TogglePopupAction() { super("TogglePopup"); } public void actionPerformed(ActionEvent ev) { _handler.toggleShowPopup(); } } /** * Action used to commit the current value in the JFormattedTextField. * This action is used by the keyboard bindings. */ private class CommitEditAction extends AbstractAction { public CommitEditAction() { super("CommitEditPopup"); } public void actionPerformed(ActionEvent ev) { try { // Commit the current value. _dateField.commitEdit(); // Reformat the value according to the formatter. _dateField.setValue(_dateField.getValue()); fireActionPerformed(); } catch (java.text.ParseException ex) { } } } private class Handler implements MouseListener, MouseMotionListener { private boolean _forwardReleaseEvent = false; public void mouseClicked(MouseEvent ev) { } public void mousePressed(MouseEvent ev) { if (!isEnabled()) { return; } if (_dateField.isEditValid()) { try { _dateField.commitEdit(); } catch (java.text.ParseException ex) { } } toggleShowPopup(); } public void mouseReleased(MouseEvent ev) { // Retarget mouse event to the month view. if (_forwardReleaseEvent) { ev = SwingUtilities.convertMouseEvent(_popupButton, ev, _monthView); _monthView.dispatchEvent(ev); _forwardReleaseEvent = false; } } public void mouseEntered(MouseEvent ev) { } public void mouseExited(MouseEvent ev) { } public void mouseDragged(MouseEvent ev) { _forwardReleaseEvent = true; if (!_popup.isShowing()) { return; } // Retarget mouse event to the month view. ev = SwingUtilities.convertMouseEvent(_popupButton, ev, _monthView); _monthView.dispatchEvent(ev); } public void mouseMoved(MouseEvent ev) { } public void toggleShowPopup() { if (_popup == null) { _popup = new JXDatePickerPopup(); } if (!_popup.isVisible()) { if (_dateField.getValue() == null) { _dateField.setValue(new Date(_linkDate)); } DateSpan span = new DateSpan((java.util.Date)_dateField.getValue(), (java.util.Date)_dateField.getValue()); _monthView.setSelectedDateSpan(span); _monthView.ensureDateVisible( ((Date)_dateField.getValue()).getTime()); _popup.show(JXDatePicker.this, 0, JXDatePicker.this.getHeight()); } else { _popup.setVisible(false); } } } /** * Popup component that shows a JXMonthView component along with controlling * buttons to allow traversal of the months. Upon selection of a date the * popup will automatically hide itself and enter the selection into the * editable field of the JXDatePicker. */ protected class JXDatePickerPopup extends JPopupMenu implements ActionListener { public JXDatePickerPopup() { _monthView.setActionCommand("MONTH_VIEW"); _monthView.addActionListener(this); setLayout(new BorderLayout()); add(_monthView, BorderLayout.CENTER); if (_linkPanel != null) { add(_linkPanel, BorderLayout.SOUTH); } } public void actionPerformed(ActionEvent ev) { String command = ev.getActionCommand(); if ("MONTH_VIEW" == command) { DateSpan span = _monthView.getSelectedDateSpan(); _dateField.setValue(span.getStartAsDate()); _popup.setVisible(false); fireActionPerformed(); } } } private final class TodayPanel extends JXPanel { TodayPanel() { super(new FlowLayout()); setDrawGradient(true); setGradientPaint(new GradientPaint(0, 0, new Color(238, 238, 238), 0, 1, Color.WHITE)); JXHyperlink todayLink = new JXHyperlink(new TodayAction()); Color textColor = new Color(16, 66, 104); todayLink.setUnclickedColor(textColor); todayLink.setClickedColor(textColor); add(todayLink); } protected void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(new Color(187, 187, 187)); g.drawLine(0, 0, getWidth(), 0); g.setColor(new Color(221, 221, 221)); g.drawLine(0, 1, getWidth(), 1); } private final class TodayAction extends AbstractAction { TodayAction() { super(_linkFormat.format(new Object[] { new Date(_linkDate) })); } public void actionPerformed(ActionEvent ae) { DateSpan span = new DateSpan(_linkDate, _linkDate); _monthView.ensureDateVisible(span.getStart()); } } } /** * Default formatter for the JXDatePicker component. This factory * creates and returns a formatter that can handle a variety of date * formats. */ static class JXDatePickerFormatter extends JFormattedTextField.AbstractFormatter { private DateFormat _formats[] = null; private int _formatIndex = 0; public JXDatePickerFormatter() { _formats = new DateFormat[3]; String format = UIManager.getString("JXDatePicker.longFormat"); if (format == null) { format = "EEE MM/dd/yyyy"; } _formats[0] = new SimpleDateFormat(format); format = UIManager.getString("JXDatePicker.mediumFormat"); if (format == null) { format = "MM/dd/yyyy"; } _formats[1] = new SimpleDateFormat(format); format = UIManager.getString("JXDatePicker.shortFormat"); if (format == null) { format = "MM/dd"; } _formats[2] = new SimpleDateFormat(format); } public JXDatePickerFormatter(DateFormat formats[]) { _formats = formats; } public DateFormat[] getFormats() { return _formats; } /** * {@inheritDoc} */ public Object stringToValue(String text) throws ParseException { Object result = null; ParseException pex = null; if (text == null || text.trim().length() == 0) { return null; } // If the current formatter did not work loop through the other // formatters and see if any of them can parse the string passed // in. for (int i = 0; i < _formats.length; i++) { try { result = (_formats[i]).parse(text); // We got a successful formatter. Update the // current formatter index. _formatIndex = i; pex = null; break; } catch (ParseException ex) { pex = ex; } } if (pex != null) { throw pex; } return result; } /** * {@inheritDoc} */ public String valueToString(Object value) throws ParseException { if (value != null) { return _formats[_formatIndex].format(value); } return null; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -