How to detect which week and month are clicked on a JCalendar? - java

I'm creating a Java GUI calendar application using the JCalendar Library, and I want to be able to detect which week of which month was clicked by the user so I can then open a new JFrame.
This is the code I have right now, I just sysouted the DAY_OF_WEEK_IN_MONTH because I wanted to see if it was working, but all I get are singular int values, which are not useful. Any advice would be greatly appreciated.
EDIT: The JCalendar Library I'm using is https://toedter.com/jcalendar/.
JCalendar calendar = new JCalendar();
calendar.setBounds(416, 70, 304, 243);
frame.getContentPane().add(calendar);
calendar.addPropertyChangeListener("calendar", new PropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent e) {
final Calendar c = (Calendar) e.getNewValue();
System.out.println(c.get(Calendar.DAY_OF_WEEK_IN_MONTH));
}
});

Related

Add days to a Java textfield through combobox

LINK TO GUI IMAGE HERE -------> http://imgur.com/uPD0K5S
public class MainMenu extends javax.swing.JFrame {
public MainMenu() {
initComponents();
cmbRoomNumber.setEnabled(false);
jPanel1.setVisible(false);
btnBook.setEnabled(false);
//SETTING COMBOBOXES TO NONE
cmbPhotoId.setSelectedIndex(-1);
cmbStayDuration.setSelectedIndex(-1);
//LABELS VALIDATION
jlblNameVer.setVisible(false);
//SETTING DATE TODAY
Date now = new Date();
//Set date format as you want
SimpleDateFormat sf = new SimpleDateFormat("dd/MM/yyyy");
this.ftxtCheckinDate.setText(sf.format(now));
}
As you can see i want to add days to Check-out Date(ftxtCheckOutDate) depending on how many days selected in the combobox(cmbStayDuration)
Im using netbeans JFrame
Thanks :)
private void cmbStayDurationActionPerformed(java.awt.event.ActionEvent evt) {
}
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.DATE, combobox number);
Basically Calendar class has a function to add days.
Get the date now, get the combo box day, then add it.
For example:
public static void main(String[] args) {
// TODO code application logic here
Calendar c = Calendar.getInstance();
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
c.setTime(d);
System.out.println(sdf.format(c.getTime()));
c.setTime(d);
c.add(Calendar.DATE, 10);
System.out.println(sdf.format(c.getTime()));
}
Output:
05/11/2015
15/11/2015
As for changing the value of Check-out Date form as the ComboBox changes, you can add either an ActionListener to listen to it change.
Example

Select "Today" in Vaadin’s calendar widgets, DateField and InlineDateField?

Vaadin has a pair of nice calendar widgets, DateField & InlineDateField.
One feature I've not detected: Can the user get back to "Today" after perusing various months and dates?
Or must I add my own separate "Today" button? At least I could do so for InlineDateField, but not DateField.
I don't believe so, I think you would have to code it yourself.
I think its the same for JodaTime too.
I'm sure you already figured this out, but here is some simple code for anyone else!
final DateField x = new DateField();
final InlineDateField y = new InlineDateField();
HorizontalLayout layout = new HorizontalLayout();
layout.setSpacing(true);
layout.addComponent(x);
layout.addComponent(y);
Button button = new Button("Today");
layout.addComponent(button);
button.addClickListener(new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
Date date = new Date();
x.setValue( date );
y.setValue( date );
}
});
this.setContent(layout);

Toedter Get Date From JDateChooser

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 get a date from JCalendar when you click on a day?

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.

JCalendar Build

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?

Categories