How would I add an actionListener to the jDayChooser component of an existing jCalendar placed using netbeans?
I would like to only trigger an event only when the day buttons are clicked. as the propertyChange in jCalendar listens to even the jMonthChooser and jYearChooser
P.S. using toedter's jCalendar
Alternatively, you can listen for the specific propertyName, "day".
JDayChooser jdc = new JDayChooser();
jdc.addPropertyChangeListener("day", new PropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent e) {
System.out.println(e.getPropertyName()+ ": " + e.getNewValue());
}
});
Addendum: How do I get it to work on a JCalendar?
Similarly, the propertyName, "calendar" represents a Calendar from which you can get() the DAY_OF_MONTH.
JCalendar jc = new JCalendar();
jc.addPropertyChangeListener("calendar", new PropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent e) {
final Calendar c = (Calendar) e.getNewValue();
System.out.println(c.get(Calendar.DAY_OF_MONTH));
}
});
In case somebody misses reading the comments. Here's a sample working code.
JCalendar jCalendar = new JCalendar();
jCalendar.getDayChooser().addPropertyChangeListener("day", new PropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent e) {
System.out.println(e.getPropertyName()+ ": " + e.getNewValue());
}
});
Related
I am using toedter JDateChooser, and I am having problems retrieving the date picked from it.
jDateChooser2.setDateFormatString("dd-MMMM-yy");
jDateChooser2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jDateChooser2MouseClicked(evt);
}
});
private void jDateChooser2MouseClicked(java.awt.event.MouseEvent evt) {
Date dateFromDateChooser = jDateChooser2.getDate();
System.out.println(dateFromDateChooser);
}
How can I retrieve the date? Is there a better way to do it? I think the listener is not being fired or triggered. i tried replacing the listener with:
System.out.println("triggered");
Still there are no output.
Basically, you don't want to listener for MouseEvents, as these could be changing the state of the component in a number of ways, most of which you don't want to know about.
You should be monitoring the date property change event, for example...
JDateChooser dateChooser = new JDateChooser();
dateChooser.addPropertyChangeListener("date", new PropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent evt) {
Date date = (Date)evt.getNewValue();
System.out.println("Date changed " + date);
}
});
Just beware, this could be triggered in response to calling setDate or by the user selecting a date from the picker, generally, you won't be able to tell
How i would add an action listener to the jcalendar? I want to get the date whenever you click in a day, so i will show the entire date on a jtextfield.
I have tried something like this, but It does nothing when i click a day.
cal = new JCalendar();
cal.setWeekOfYearVisible(false);
cal.getDayChooser().getDayPanel().addPropertyChangeListener(new PropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent e) {
System.out.println(e.getPropertyName()
+ ": " + (Date) e.getNewValue());
}
});
I tried the code of the answer to this question too, that is similar, but nothing.
Adding actionListener to jCalendar
I think you are adding the property change listener to the wrong bean. I inspected the JCalendar code and the getDayPanel() method returns just a regular JPanel that I don't think knows about the "day" property you are interested in.
/**
* Returns the day panel.
*
* #return the day panel
*/
public JPanel getDayPanel() {
return dayPanel;
}
I think you should add your property change listener to the daychooser itself, which is the class that knows about the "day" property. Also, you might want to register for the "day" property of the day chooser:
cal = new JCalendar();
cal.setWeekOfYearVisible(false);
cal.getDayChooser().addPropertyChangeListener("day", new PropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent e) {
System.out.println(e.getPropertyName()
+ ": " + e.getNewValue());
}
});
Still, that will only give you the day that the user picked, not the entire date.
How to receive a selected date using method actionPerformed?
Do you know other useful methods?
I cannot find proper method to receive selected date.
_jCalendarButton.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
Object[] tab = _jCalendarButton.getSelectedObjects();
}
});
The project is documented here: http://www.jbundle.org/jbundle/jcalendarbutton/quickstart.html. You must add a PropertyChangeListener on the button to be notified when the date changes. The ActionListener is only called when you press the button. Given that the project has no published javadoc, look at the source code for more information.
You will need to use a PropertyChangeListener rather than an ActionListener and read the new value from its PropertyChangeEvent
jCalendarButton.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getNewValue() instanceof Date) {
System.out.println(evt.getNewValue());
}
}
});
If you want to select an date with GUI of Calendar you can use JXDatePicker (Component of SwingX). You will need an SwingX jar file to use it.
Use the following code:
JXDatePicker datePicker = new JXDatePicker(System.currentTimeMillis());
datePicker.addActionListener(new ActionListener(){
label.setText(datePicker.getDate().toString());
});
Using JCalendar, I have entered the following code from this site:
private void jCalendar2PropertyChange(java.beans.PropertyChangeEvent evt)
{
JCalendar jc = new JCalendar();
jc.addPropertyChangeListener("calendar", new PropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent evt) {
final Calendar c = (Calendar) evt.getNewValue();
System.out.println(c.get(Calendar.DAY_OF_MONTH));
}
})
but I am not getting a result in the output screen? Also the build is taking 23 seconds is this normal?
thank you for your help. Also do I need to create the mouse event?
I want a listener that autoselects the entry on the JXDatePickers editor cell when same gains focus.
DatePicker.getEditor().selectAll();
doesn´t work. So i tried this :
DatePicker.getEditor().addFocusListener(new FocusListener() {
#Override
public void focusGained(FocusEvent e) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
DatePicker. getEditor().selectAll();
}
});
}
public void focusLost(FocusEvent e) {
}
});
Any suggestions ?
Edit
just realized that you probably have stand-alone datepicker, and run your snippet: worksforme. So we'll need to dig for differences - what's your swingx/jdk version and OS?
Original
typically, the JFormattedTextField is tricky to convince being selected ;-) See
Combining JXTable with RXTable
and adapt the solution to handle JXDatePicker as well - by adding
if (editor instanceof JXDatePicker) {
LOG.info("got picker: " + editor);
invokeSelectAll(((JXDatePicker) editor).getEditor());
return;
}