JSpinner giving old values

前端 未结 2 1043
忘掉有多难
忘掉有多难 2021-01-16 12:07

I am using couple of JSpinners in my project who display the hour and minutes. When the JSpinner is incremented or decremented i have to save the value to the database. But

相关标签:
2条回答
  • 2021-01-16 12:28

    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.

    0 讨论(0)
  • 2021-01-16 12:33

    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());
            }
    
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题