Is there any good and free Date AND Time Picker available for Java Swing?
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
Is there any date picker java swing which is easy to use and program?
Professionally we use JDatePicker but you will have to pay for it. there tutorial is here. If you are looking for a lower cost option I'd consider using JIDE's DateSpinner you can try it here and see the source code. Unfortunately JIDE's component is just a spinner to use the ComboBox or Panel you will need a paid for license.
I should point out that we also use JIDE and JDatePicker we don't receive any royalties
date and time picker in JAVA
Check this thread (uses swingx JXDatePicker
component, he extends it's functionality)
Is there any good and free Date AND Time Picker available for Java Swing?
Second answer has a full example
download swingx from here: http://www.java2s.com/Code/Jar/s/Downloadswingxjar.htm
example from the other post:
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.*;
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);
}
}
Which one is the best Java datepicker..?
SwingLabs has SwingX project which provides suite of components including JXDatePicker (API) works with Date class, which supports dates starting from January 1, 1970, 00:00:00 GMT.
Try demo, as an option. Not 199x and not ugly. Widely supported and used.
Is there any good and free Date AND Time Picker available for Java Swing?
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
What are good Java date-chooser Swing GUI widgets?
Recently I found and use the Microba DatePicker (http://microba.sourceforge.net/) for a personal project involving Swing GUIs, and I actually I really liked the way this control is implemented. Besides, the license is BSD, so you will be able to customize the control and use it for commercial purposes if you need to do so.
add date & time with calendar
You can use JDatePicker from http://jdatepicker.org/.
It looks like
There are some code snippets How to use JDatePicker to display calendar component, for example to create the picker shown above:
UtilDateModel model = new UtilDateModel();
JDatePanelImpl datePanel = new JDatePanelImpl(model);
JDatePickerImpl datePicker = new JDatePickerImpl(datePanel);
frame.add(datePicker);
what is the best way to use dates with java swing
Use a date picker, like JCalendar or JDatePicker. Since users can't type raw date strings, you don't have to worry about their mistakes.
Edit: drhorrible is right. I've fixed the link now.
Related Topics
Where Is the Correct Location to Put Log4J.Properties in an Eclipse Project
How to Disable the Default Console Handler, While Using the Java Logging API
Creating Animated Gif with Imageio
Trouble with Gson Serializing an Arraylist of Pojo'S
Spring Cache with Collection of Items/Entities
Why Does This Gridbaglayout Not Appear as Planned
How to Improve the Performance of G.Drawimage() Method for Resizing Images
Why Dispatcherservlet Creates Another Application Context
Java Switch Statement Multiple Cases
Find Files in a Folder Using Java
String Replacement in Java, Similar to a Velocity Template
How to Get the Http Status Code Out of a Servletresponse in a Servletfilter
Jtable Getselectedrow Does Not Return the Selected Row Index
How to Get Method Parameter Names in Java 8 Using Reflection
Is It Discouraged to Use @Spy and @Injectmocks on the Same Field
How to Deserialize a List Using Gson or Another JSON Library in Java