有什么好的免费的date和时间select器可用于Java Swing吗?
有什么好的免费的date和时间select器可用于Java Swing吗?
有很多dateselect器可用,但没有date和时间select器。 这是迄今为止我所遇到的最接近的: 寻找一个date和时间select器
任何人?
对于时间select器,您可以使用JSpinner并设置仅显示时间值的JSpinner.DateEditor。
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
您可以扩展swingx JXDatePicker组件:
“JXDatePicker只处理没有时间的date,通常我们需要让用户selectdate和时间,这是一个如何使用JXDatePicker来处理date和时间的例子。
http://wiki.java.net/twiki/bin/view/Javadesktop/JXDateTimePicker
编辑:这篇文章从网上消失,但SingleShot发现,它仍然可以在互联网的档案。 可以肯定的是,这里是一个完整的工作示例:
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); } }
使用两者结合..这就是我所做的:
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; }
FLib-JCalendar组件具有组合的date和时间select器。
正如你所说的dateselect器很容易,有很多。
至于时间选取器,请查看Google日历在创build新条目时如何执行此操作。 它允许你input任何东西,同时它有30分钟的增量下降。 更改分钟时,下拉列表会更改。
如果你需要允许用户select秒,那么你可以做的最好的是一个typable /下拉组合
最好的,JCalendar: http : //www.toedter.com/en/jcalendar/index.html LGPL许可。