java - 使用 SpinDateModel 处理 JSpinner 中的日期格式

标签 java swing jspinner

我有以下几行代码:

    SpinnerDateModel spinMod=new SpinnerDateModel();
    JSpinner spin=new JSpinner(spinMod);
    //spin.setEditor(new JSpinner.DateEditor(spin,"dd.mm.yyyy"));

当我显示微调器时,我可以编辑它并在月份字段中写入我想要的任何两位数。

如果我点击 JSpinner 一侧,我只能增加和减少月份或日期数字,以便它们的值是合法的。但是,如果我添加最后一个注释掉的行,这也不起作用。

问题是如何使 JSpinner 只接受具有自定义格式的合法日期。

此外,如果我将 ActionListener 添加到获取日期的按钮,我可以通过将 getValue() 直接应用于 JSpinner 来获得“正确的日期”,但如果我将日期编辑器的格式应用于从 JSpinner 获取的值,则会出现错误(输入的值)。我希望操作监听器以自定义格式获取正确的值,并且希望 JSpinner 只接受自定义格式的输入。这可能吗?

我的测试代码如下:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import java.text.*; //for the date format

public class TestDates{

    JLabel jl=new JLabel("Date:");
    JFrame jf=new JFrame("Test SpinnerDateFormat");

    JButton jb=new JButton("Get Date");
    SpinnerDateModel spinMod=new SpinnerDateModel();
    JSpinner spin=new JSpinner(spinMod);

    class jbHandler implements ActionListener{
        public void actionPerformed(ActionEvent evt){
            JSpinner.DateEditor de=(JSpinner.DateEditor)spin.getEditor();
            String wrongDate=de.getFormat().format(spin.getValue());
            Date okDate=(Date)spin.getValue();
            System.out.println(">>>\n"+wrongDate+"\n"+okDate);
        }
    }

    JPanel pane=new JPanel();

    public TestDates(){

        jf.setLocationRelativeTo(null);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        pane.add(jl);

        jb.addActionListener(new jbHandler());
        pane.add(jb);

        spin.setValue(new Date());//this doesn't work properly with the custom format
        spin.setEditor(new JSpinner.DateEditor(spin,"dd.mm.yyyy"));
        pane.add(spin);

        jf.add(pane);
        jf.pack();
        jf.setVisible(true);
    }

    public static void main(String[] args){

    SwingUtilities.invokeLater(new Runnable(){
        public void run(){
            TestDates tdate=new TestDates();
        }
        });
    }
}

最佳答案

If I click on the JSpinner side I can only increase and decrease the month or day numbers so that their values are legal. But, if I add the last outcommented line, this wouldn't work, either.

问题是这一行的日期模式中有一个小(但很常见)错误:

spin.setEditor(new JSpinner.DateEditor(spin,"dd.mm.yyyy"));

在此模式中,“mm”指的是分钟而不是月份,因此会导致不同的输出。正确的模式是:

spin.setEditor(new JSpinner.DateEditor(spin,"dd.MM.yyyy"));

The question is how to make the JSpinner accept only legal dates with the custom format.

如果您想防止用户输入无效日期,那么您需要使用编辑器的格式化程序并禁止无效输入。请参阅以下代码片段:

SpinnerDateModel model = new SpinnerDateModel();
JSpinner spinner = new JSpinner(model);

JSpinner.DateEditor editor = new JSpinner.DateEditor(spinner, "dd.MM.yyyy");
DateFormatter formatter = (DateFormatter)editor.getTextField().getFormatter();
formatter.setAllowsInvalid(false); // this makes what you want
formatter.setOverwriteMode(true);

自定义日期时间格式

参见Customizing Formats踪迹。

另请参阅从 SimpleDateFormat 中的日期和时间模式部分摘录的下表javadoc:

enter image description here

关于java - 使用 SpinDateModel 处理 JSpinner 中的日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25769024/

相关文章:

java - java 体验式代码审查

java - 如何根据内容找出未显示的 JPanel 的首选大小?

java - 当 JOptionPane 显示在 ChangeListener 上时,JSpinner 不断旋转

java - java中构造函数返回值有什么用以及如何访问这个值?

java - GAE 数据存储 API 直接替换

java - 关于构造函数的奇怪 NullPointerException

java - 用Java绘制图形

java - JSpinner 的 JButton 到 ImageIcon

java - 具有 DateFormatter 删除行为的 JSpinner

java - 正确设置mysql排序规则