JSpinner给出了旧的价值观 [英] JSpinner giving old values

查看:149
本文介绍了JSpinner给出了旧的价值观的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在项目中使用了几个JSpinner来显示小时和分钟。当JSpinner递增或递减时,我必须将值保存到数据库中。但问题是JSpinners给了我很旧的价值观。
eg-如果显示的时间是 09:30 ,我将时间增加到 10:30 ,我将09:30作为返回值。我正在使用以下代码

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 the problem is that the JSpinners are giving me old values. eg- If the time displayed is 09:30 and i increment the time to 10:30, I am getting 09:30 as the returned value. I am using following code

更新的SSCCE

package spinnerupdation;

import java.awt.Container;
import java.awt.FlowLayout;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerDateModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.text.DateFormatter;
import javax.swing.text.DefaultFormatterFactory;

/**
 *
 * @author Rohan Kandwal
 */
public class SpinnerUpdation extends JFrame{
 public JSpinner spinner;
    SpinnerUpdation(){
        Container pane=this.getContentPane();
        JPanel panel=new JPanel();
        panel.setLayout(new FlowLayout());
        SpinnerDateModel model=new SpinnerDateModel();
        model.setCalendarField(Calendar.HOUR);
        spinner=new JSpinner();
        spinner.setModel(model);
        spinner.setEditor(new JSpinner.DateEditor(spinner,"hh:mm"));
        panel.add(spinner);
        pane.add(panel);
        spinner.addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent e) {
                        JFormattedTextField tf = ((JSpinner.DefaultEditor) spinner.getEditor()).getTextField();
                        DefaultFormatterFactory factory = (DefaultFormatterFactory) tf.getFormatterFactory();
                        DateFormatter formatter = (DateFormatter) factory.getDefaultFormatter();

                        // Change the date format to only show the hours
                        formatter.setFormat(new SimpleDateFormat("hh:mm"));
                        //formatter.setCommitsOnValidEdit(true);

                System.out.println(spinner.getValue());
                //System.out.println(tf.getText());
            }
        });


            }
    public static void main(String[] args) {
        SpinnerUpdation ss=new SpinnerUpdation();

        ss.setDefaultCloseOperation(ss.EXIT_ON_CLOSE);
        ss.setSize(574, 445);
     //ss.pack();

     ss.setLocationRelativeTo(null);
     ss.setResizable(false);
     ss.setVisible(true);
    }
}

如果我使用 tf .getText()我得到旧值两次但是如果我使用 spinner.getValue 我得到的是新值,但是它很长格式

if i am using tf.getText() i am getting the old value twice but if i am using spinner.getValue I am getting the new value but it is in long format

Thu Jan 01 10:18:00 IST 1970
Thu Jan 01 11:18:00 IST 1970

我应该如何格式化微调器以仅提供 11:18

How should i format spinner to give only 11:18 ?

推荐答案

Java swing使用MVC模式,因此它在JSpinner中是相同的。如果查看JSpinner源代码,您会发现getValue()方法实际上是调用getModel()。getValue(),因此它调用模型的getValue。你使用的模型是SpinnerDateModel,它的值是Date Object,当你打印Date对象时,它会显示默认格式,如Thu Jan 01 11:18:00 IST 1970,如果你想获得像11:18之类的东西,你需要自己格式化。像

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()) );

您可能想知道为什么tf.getText()只获取旧值,因为在ChangeEvent之后spinner发生时,tf上会发生一个PropertyChangeEvent,会为它设置新值。当然,您可以像tf.addPropertyChangeListener一样收听tf。但我建议使用上面的解决方案。

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.

这篇关于JSpinner给出了旧的价值观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆