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);
Related
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));
}
});
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
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this question
Is there any good and free Date AND Time Picker available for Java Swing?
There are a lot date pickers available but no date AND time picker. This is the closest I came across so far: Looking for a date AND time picker
Anybody?
For a time picker you can use a JSpinner and set a JSpinner.DateEditor that only shows the time value.
JSpinner timeSpinner = new JSpinner( new SpinnerDateModel() );
JSpinner.DateEditor timeEditor = new JSpinner.DateEditor(timeSpinner, "HH:mm:ss");
timeSpinner.setEditor(timeEditor);
timeSpinner.setValue(new Date()); // will only show the current time
You can extend the swingx JXDatePicker component:
"JXDatePicker only handles dates without time. Quite often we need to let the user choose a date and a time. This is an example of how to make use JXDatePicker to handle date and time together."
http://wiki.java.net/twiki/bin/view/Javadesktop/JXDateTimePicker
EDIT: This article disappeared from the web, but as SingleShot discovered, it is still available in an internet archive. Just to be sure, here is the full working example:
import org.jdesktop.swingx.calendar.SingleDaySelectionModel;
import org.jdesktop.swingx.JXDatePicker;
import javax.swing.*;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.DateFormatter;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.*;
import java.awt.*;
/**
* This is licensed under LGPL. License can be found here: http://www.gnu.org/licenses/lgpl-3.0.txt
*
* This is provided as is. If you have questions please direct them to charlie.hubbard at gmail dot you know what.
*/
public class DateTimePicker extends JXDatePicker {
private JSpinner timeSpinner;
private JPanel timePanel;
private DateFormat timeFormat;
public DateTimePicker() {
super();
getMonthView().setSelectionModel(new SingleDaySelectionModel());
}
public DateTimePicker( Date d ) {
this();
setDate(d);
}
public void commitEdit() throws ParseException {
commitTime();
super.commitEdit();
}
public void cancelEdit() {
super.cancelEdit();
setTimeSpinners();
}
#Override
public JPanel getLinkPanel() {
super.getLinkPanel();
if( timePanel == null ) {
timePanel = createTimePanel();
}
setTimeSpinners();
return timePanel;
}
private JPanel createTimePanel() {
JPanel newPanel = new JPanel();
newPanel.setLayout(new FlowLayout());
//newPanel.add(panelOriginal);
SpinnerDateModel dateModel = new SpinnerDateModel();
timeSpinner = new JSpinner(dateModel);
if( timeFormat == null ) timeFormat = DateFormat.getTimeInstance( DateFormat.SHORT );
updateTextFieldFormat();
newPanel.add(new JLabel( "Time:" ) );
newPanel.add(timeSpinner);
newPanel.setBackground(Color.WHITE);
return newPanel;
}
private void updateTextFieldFormat() {
if( timeSpinner == null ) return;
JFormattedTextField tf = ((JSpinner.DefaultEditor) timeSpinner.getEditor()).getTextField();
DefaultFormatterFactory factory = (DefaultFormatterFactory) tf.getFormatterFactory();
DateFormatter formatter = (DateFormatter) factory.getDefaultFormatter();
// Change the date format to only show the hours
formatter.setFormat( timeFormat );
}
private void commitTime() {
Date date = getDate();
if (date != null) {
Date time = (Date) timeSpinner.getValue();
GregorianCalendar timeCalendar = new GregorianCalendar();
timeCalendar.setTime( time );
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, timeCalendar.get( Calendar.HOUR_OF_DAY ) );
calendar.set(Calendar.MINUTE, timeCalendar.get( Calendar.MINUTE ) );
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
Date newDate = calendar.getTime();
setDate(newDate);
}
}
private void setTimeSpinners() {
Date date = getDate();
if (date != null) {
timeSpinner.setValue( date );
}
}
public DateFormat getTimeFormat() {
return timeFormat;
}
public void setTimeFormat(DateFormat timeFormat) {
this.timeFormat = timeFormat;
updateTextFieldFormat();
}
public static void main(String[] args) {
Date date = new Date();
JFrame frame = new JFrame();
frame.setTitle("Date Time Picker");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DateTimePicker dateTimePicker = new DateTimePicker();
dateTimePicker.setFormats( DateFormat.getDateTimeInstance( DateFormat.SHORT, DateFormat.MEDIUM ) );
dateTimePicker.setTimeFormat( DateFormat.getTimeInstance( DateFormat.MEDIUM ) );
dateTimePicker.setDate(date);
frame.getContentPane().add(dateTimePicker);
frame.pack();
frame.setVisible(true);
}
}
Use the both combined.. that's what i did:
public static JPanel buildDatePanel(String label, Date value) {
JPanel datePanel = new JPanel();
JDateChooser dateChooser = new JDateChooser();
if (value != null) {
dateChooser.setDate(value);
}
for (Component comp : dateChooser.getComponents()) {
if (comp instanceof JTextField) {
((JTextField) comp).setColumns(50);
((JTextField) comp).setEditable(false);
}
}
datePanel.add(dateChooser);
SpinnerModel model = new SpinnerDateModel();
JSpinner timeSpinner = new JSpinner(model);
JComponent editor = new JSpinner.DateEditor(timeSpinner, "HH:mm:ss");
timeSpinner.setEditor(editor);
if(value != null) {
timeSpinner.setValue(value);
}
datePanel.add(timeSpinner);
return datePanel;
}
There is the FLib-JCalendar component with a combined Date and Time Picker.
As you said Date picker is easy, there are many out there.
As for a Time picker, check out how Google Calendar does it when creating a new entry. It allows you to type in anything while at the same time it has a drop down in 30 mins increments. The drop down changes when you change the minutes.
If you need to allow the user to pick seconds, then the best you can do is a typable/drop down combo