📄 jcalendarcombobox.java
字号:
/**
* Returns the Popup Location
*
* @return Location of the Popup
*/
public int getPopUpLocation() {
return _popupLocation;
}
/**
* In earlier Versions it sets the vertical Position of the Text in the Button,
* but it is not longer used!
*
* @deprecated does nothing!!
* @param value nothing
*/
public void setVerticalAlignment(int value) {
}
/**
* Sets the horizontal Position of the Text in the Button
*
* @param value
* RIGHT, LEFT, CENTER, LEADING, TRAILING for horizontal
* Alignment
*/
public void setHorizontalAlignment(int value) {
((JSpinner.DefaultEditor )_spinner.getEditor ()).getTextField().setHorizontalAlignment(value);
}
/**
* Handles the ToggleButton events for hiding / showing the PopUp
*
* @param e
* the ActionEvent
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent e) {
if ((_calendarWindow != null) && (_calendarWindow.isVisible())) {
hideCalendar();
} else {
showCalender();
}
}
/**
* Hides the Calendar PopUp and fires a ChangeEvent if a change was made
*/
public void hideCalendar() {
if (_calendarWindow.isVisible()) {
_calendarWindow.setVisible(false);
// TODO!!
if (!_calendarPanel.getCalendar().getTime().equals(_spinner.getModel().getValue())) {
_changed = true;
}
if (_changed) {
_spinner.getModel().setValue(
_calendarPanel.getCalendar().getTime());
_selected = (Calendar) _calendarPanel.getCalendar().clone();
_changed = false;
fireChangeEvent();
}
}
}
/**
* Shows the Calendar PopUp
*/
public void showCalender() {
Window ancestor = (Window) this.getTopLevelAncestor();
if ((_calendarWindow == null)
|| (ancestor != _calendarWindow.getOwner())) {
createCalendarWindow();
}
//Update the datefrom the spinner model
Date date = (Date) _spinner.getModel().getValue();
_selected.setTime(date);
_calendarPanel.setCalendar(_selected);
Point location = getLocationOnScreen();
int x;
if (_popupLocation == RIGHT) {
x = (int) location.getX() + _button.getSize().width
- _calendarWindow.getSize().width;
} else if (_popupLocation == CENTER) {
x = (int) location.getX()
+ ((_button.getSize().width - _calendarWindow.getSize().width) / 2);
} else {
x = (int) location.getX();
}
int y = (int) location.getY() + _button.getHeight();
Rectangle screenSize = getDesktopBounds();
if (x < 0) {
x = 0;
}
if (y < 0) {
y = 0;
}
if (x + _calendarWindow.getWidth() > screenSize.width) {
x = screenSize.width - _calendarWindow.getWidth();
}
if (y + 30 + _calendarWindow.getHeight() > screenSize.height) {
y = (int) location.getY() - _calendarWindow.getHeight();
}
_calendarWindow.setBounds(x, y, _calendarWindow.getWidth(),
_calendarWindow.getHeight());
_calendarWindow.setVisible(true);
}
/**
* Gets the screensize. Takes into account multi-screen displays.
*
* @return a union of the bounds of the various screen devices present
*/
private Rectangle getDesktopBounds() {
final GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
final GraphicsDevice[] gd = ge.getScreenDevices();
final Rectangle[] screenDeviceBounds = new Rectangle[gd.length];
Rectangle desktopBounds = new Rectangle();
for (int i = 0; i < gd.length; i++) {
final GraphicsConfiguration gc = gd[i].getDefaultConfiguration();
screenDeviceBounds[i] = gc.getBounds();
desktopBounds = desktopBounds.union(screenDeviceBounds[i]);
}
return desktopBounds;
}
/*
* (non-Javadoc)
*
* @see javax.swing.event.AncestorListener#ancestorAdded(javax.swing.event.AncestorEvent)
*/
public void ancestorAdded(AncestorEvent event) {
hideCalendar();
}
/*
* (non-Javadoc)
*
* @see javax.swing.event.AncestorListener#ancestorMoved(javax.swing.event.AncestorEvent)
*/
public void ancestorMoved(AncestorEvent event) {
hideCalendar();
}
/*
* (non-Javadoc)
*
* @see javax.swing.event.AncestorListener#ancestorRemoved(javax.swing.event.AncestorEvent)
*/
public void ancestorRemoved(AncestorEvent event) {
hideCalendar();
}
/**
* Listens to ChangeEvents of the JCalendarPanel and rembers if something
* was changed.
*
* If the Day was changed, the PopUp is closed.
*
* @param e
* ChangeEvent
* @see javax.swing.event.ChangeListener#stateChanged(javax.swing.event.ChangeEvent)
*/
public void stateChanged(ChangeEvent e) {
_changed = true;
hideCalendar();
}
/**
* Adds a Changelistener to this JCalendarComboBox.
*
* It will be called everytime the ComboBox is closed and the Date was
* changed
*
* @param listener
* ChangeListener
*/
public void addChangeListener(ChangeListener listener) {
_changeListener.add(listener);
}
/**
* Removes a ChangeListener from this JCalendarComboBox
*
* @param listener
* listener to remove
*/
public void removeChangeListener(ChangeListener listener) {
_changeListener.remove(listener);
}
/**
* Gets all ChangeListeners
*
* @return all ChangeListeners
*/
public ChangeListener[] getChangeListener() {
return (ChangeListener[]) _changeListener.toArray();
}
/**
* Fires the ChangeEvent
*/
protected void fireChangeEvent() {
if (!_fireingChangeEvent) {
_fireingChangeEvent = true;
ChangeEvent event = new ChangeEvent(this);
for (int i = 0; i < _changeListener.size(); i++) {
((ChangeListener) _changeListener.get(i)).stateChanged(event);
}
_fireingChangeEvent = false;
}
}
/**
* Enables/Disables the ComboBox
*
* @param enabled
* Enabled ?
*/
public void setEnabled(boolean enabled) {
_spinner.setEnabled(enabled);
_button.setEnabled(enabled);
}
/**
* Is the ComboBox enabled ?
*
* @return enabled ?
*/
public boolean isEnabled() {
return _button.isEnabled();
}
/**
* Gets the Popup Location
*
* @return location of the Popup
*/
public int getPopupLocation() {
return _popupLocation;
}
/**
* Sets the Location of the Popup (LEFT, CENTER or RIGHT)
*
* @param location
* Location of the PopUp
*/
public void setPopupLocation(int location) {
_popupLocation = location;
}
/**
* Returns the model used to hold the Date object
*
* @return
*/
public SpinnerDateModel getModel() {
return (SpinnerDateModel) _spinner.getModel();
}
/**
* Sets the model used to hold the Date object
*
* @param model
* A SpinnerDateModel to be used by the spinner control.
*/
public void setSpinnerDateModel(SpinnerDateModel model) {
_spinner.setModel(model);
}
/**
* Where should be the Popup?
*/
private int _popupLocation = LEFT;
/**
* If the Window looses Focus and the ToggleButton get's it, don't popup
* the Window again...
*/
private boolean _calendarWindowFocusLost = false;
/**
* Current selected Day
*/
private Calendar _selected;
/**
* The ToggleButton for hiding/showing the JCalendarPanel
*/
private BasicArrowButton _button;
/**
* The text field that holds the date
*/
private JSpinner _spinner = new JSpinner();
/**
* The JWindow for the PopUp
*/
private JWindow _calendarWindow;
/**
* The JCalendarPanel inside the PopUp
*/
private JCalendarPanel _calendarPanel;
/**
* The list of ChangeListeners
*/
private ArrayList _changeListener = new ArrayList();
/**
* Currently firing an ChangeEvent?
*/
private boolean _fireingChangeEvent = false;
/**
* Something changed in the JCalendarPanel ?
*/
private boolean _changed = false;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -