无法禁用DateEditor的JTextField的编辑?

时间:2013-10-12 05:01:34

标签: java swing jtextfield jspinner

我过去一直使用以下技术禁用JSpinner s的编辑。但是,它似乎不适用于JTextField中的DateEditor。是否有理由认为此editor.getTextField().setEditable(false)在此方案中不起作用?有没有办法工作?

驱动程序类

public class CLIClockDriver {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ClockFrame();
            }
        });
    }
}

Swing Class

public class ClockFrame extends JFrame implements ActionListener {

    private static final long serialVersionUID = -7009835158496820266L;
    public static final Dimension SIZE = new Dimension(400, 200);

    private final Timer secondsTimer = new Timer(1000, this);
    private final FluentGridBagConstraints gbc = new FluentGridBagConstraints();
    private JPanel mainPanel;
    private JLabel clockLabel;
    private JSpinner newAlarmHourSpinner, newAlarmMinuteSpinner, newAlarmAMPMSpinner;

    public ClockFrame() {
        super();
        setLayout(new GridBagLayout());
        initializeGUIComponents();
        setPreferredSize(SIZE);
        setResizable(false);
        setTitle(Constants.APPLICATION_NAME);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
        secondsTimer.start();
    }



    private void initializeGUIComponents() {
        mainPanel = new JPanel(new GridBagLayout());

        clockLabel = new JLabel();
        updateClock();
        mainPanel.add(clockLabel, gbc.setXY(0, 0));

        newAlarmHourSpinner = new JSpinner();
        newAlarmHourSpinner.setModel(new CyclingSpinnerNumberModel(9, 1, 12, 1));
        DefaultEditor editor = new JSpinner.DefaultEditor(newAlarmHourSpinner);
        editor.getTextField().setColumns(2);
        editor.getTextField().setEditable(false);
        newAlarmHourSpinner.setEditor(editor);
        mainPanel.add(newAlarmHourSpinner, gbc.reset().setXY(0, 1));

        newAlarmMinuteSpinner = new JSpinner();
        SpinnerDateModel model = new SpinnerDateModel();
        model.setCalendarField(Calendar.MINUTE);
        newAlarmMinuteSpinner.setModel(model);
        DateEditor dateEditor = new JSpinner.DateEditor(newAlarmMinuteSpinner, "mm");
        editor.getTextField().setColumns(2);
        editor.getTextField().setEditable(false);
        newAlarmMinuteSpinner.setEditor(dateEditor);
        mainPanel.add(newAlarmMinuteSpinner, gbc.reset().setXY(1, 1));

        newAlarmAMPMSpinner = new JSpinner();
        newAlarmAMPMSpinner.setModel(new CyclingSpinnerListModel("AM", "PM"));
        editor = new JSpinner.DefaultEditor(newAlarmAMPMSpinner);
        editor.getTextField().setColumns(2);
        editor.getTextField().setEditable(false);
        newAlarmAMPMSpinner.setEditor(editor);
        mainPanel.add(newAlarmAMPMSpinner, gbc.reset().setXY(2, 1));

        add(mainPanel, gbc.reset().setXY(0, 0));
    }

    private void updateClock() {
        clockLabel.setText(Alarm.TIME_FORMAT.format(new Date()));
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        if(event.getSource() == secondsTimer) {
            updateClock();
        }
    }


}

1 个答案:

答案 0 :(得分:2)

确保您的GUI是在EDT上创建的。

当我在How to Use Spinners上的Swing教程中运行SpinnerDemo代码时,它可以正常工作,但有以下更改:

    ftf = getTextField(spinner);
    ftf.setEditable(false);

但是,当我没有在EDT上创建GUI时,我的简单微调器测试程序也遇到了问题。