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?
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 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());
});
im looking for the line of code, which will select (highlight) the Date-text-string in the jDateChooser when it gets focused.
I read that I might have to do something like .selectAll();. but i cant get access to the textfield of the jDateChooser.
also jDateChooser.selectOnFocus(true); wont compile. NetBeans says: "cannot find symbol".
eventhough i have imported:
import com.toedter.calendar.JDateChooser;
import com.toedter.calendar.demo.DateChooserPanel;
any ideas anyone ?
Change the library jar for calendar few libraries do not have all the symbols.
You can download from here and replace it with new one and then check:
JDateChooser dateChooser = new JDateChooser(new Date());
dateChooser.getDateEditor().getUiComponent().addFocusListener(new FocusAdapter() {
#Override
public void focusGained(FocusEvent evt) {
((JTextFieldDateEditor)evt.getSource()).selectAll();
}
});
dateChooser.getDateEditor().getUiComponent().addFocusListener(new FocusAdapter() {
#Override
public void focusGained(FocusEvent evt) {
if (evt.getSource() instanceof JTextComponent) {
final JTextComponent textComponent=((JTextComponent)evt.getSource());
SwingUtilities.invokeLater(new Runnable(){
public void run() {
textComponent.selectAll();
}});
}
}
});
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());
}
});
I want to have a clock showing current time and refreshes every second. The code I am using is:
int timeDelay = 1000;
ActionListener time;
time = new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
timeLabel.setText(DateTimeUtil.getTime());
/*timeLabel is a JLabel to display time,
getTime() is samll static methos to return formatted String of current time */
}
};
SwingWorker timeWorker = new SwingWorker() {
#Override
protected Object doInBackground() throws Exception {
new Timer(timeDelay, time).start();
return null;
}
};
timeWorker.execute();
What I want to refresh the timeLabel text in another thread other than EDT.
Am I doing it correct? Any other better way?
Also for information, i've added timeLabel to a extendedJPanel which contains few similar kinds of utilities, and is called in another MainJFrame.
You can do this without a SwingWorker, because this is what the Swing Timer is made for.
int timeDelay = 1000;
ActionListener time;
time = new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
timeLabel.setText(DateTimeUtil.getTime());
/* timeLabel is a JLabel to display time,
getTime() is samll static methos to return
formatted String of current time */
}
};
new Timer(timeDelay, time).start();