JSpinner giving old values

北城以北 提交于 2019-12-01 12:50:49

Java swing use a MVC pattern, so it is the same in JSpinner. If you look into JSpinner source code, you will find that the getValue() method is actually call getModel().getValue(), so it is calling the model's getValue. and the model you use is SpinnerDateModel, and the value of it will be Date Object,when you print the Date object, it will display the default format like "Thu Jan 01 11:18:00 IST 1970", if your want to get something like "11:18", you will need to format it yourself.like

 SimpleDateFormat sdf = new SimpleDateFormat("hh:mm");
 System.out.println(sdf.format(spinner.getValue()) );

You may wonder why tf.getText() only get the old value, because after the ChangeEvent on the spinner occurs, a PropertyChangeEvent on the tf will occurs,will set the new value to it. Of course, you can listen to tf like tf.addPropertyChangeListener. but i will suggest use the solution above.

You could maintain a "master date" which you re-merge the time value back into on each stateChanged event.

public class Main {

    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            add(new DateSpinner(new Date()));
        }

    }

    public class DateSpinner extends JSpinner {

        private Date masterDate;

        public DateSpinner(Date date) {
            super(new SpinnerDateModel());
            this.masterDate = date;
            SpinnerDateModel model = (SpinnerDateModel) getModel();
            model.setCalendarField(Calendar.HOUR);
            setEditor(new JSpinner.DateEditor(this, "hh:mm"));
            JFormattedTextField tf = ((JSpinner.DefaultEditor) getEditor()).getTextField();
            DefaultFormatterFactory factory = (DefaultFormatterFactory) tf.getFormatterFactory();
            DateFormatter formatter = (DateFormatter) factory.getDefaultFormatter();

            formatter.setFormat(new SimpleDateFormat("hh:mm"));

            setValue(date);

            addChangeListener(new ChangeListener() {
                @Override
                public void stateChanged(ChangeEvent e) {

                    Calendar cal = Calendar.getInstance();
                    cal.setTime(masterDate);

                    Calendar time = Calendar.getInstance();
                    time.setTime((Date) getValue());

                    cal.set(Calendar.HOUR_OF_DAY, time.get(Calendar.HOUR_OF_DAY));
                    cal.set(Calendar.MINUTE, time.get(Calendar.MINUTE));

                    masterDate = cal.getTime();

                    System.out.println(masterDate);

                }

            });
        }

        public String getTime() {
            return new SimpleDateFormat("hh:mm").format((Date)getValue());
        }

    }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!