Readonly JFormattedTextField

前端 未结 3 1477
孤城傲影
孤城傲影 2021-01-27 06:53

Supossing that a JFormattedTextField is enabled and contains a formatter, is there a way to make it readonly? With a JTextField, we just need to suply a custom

相关标签:
3条回答
  • 2021-01-27 07:33

    You can simply use:

    JFormattedTextField field = new JFormattedTextField();
    // ...
    field.setEditable(false);
    

    just like for the JTextField since JFormattedTextField extends JTextField

    Check out the javadoc as well.

    0 讨论(0)
  • 2021-01-27 07:38

    What about undoing every change that occurs:

    JFormattedTextField formattedTextField = new JFormattedTextField("Can't touch this! :)");
    formattedTextField.getDocument().addUndoableEditListener(new UndoableEditListener() {
        @Override
        public void undoableEditHappened(UndoableEditEvent e) {
            e.getEdit().undo();
        }
    });
    
    0 讨论(0)
  • 2021-01-27 07:41

    Can't you disable editing by setting isEditable to false?

    textfield.setEditable(false);

    0 讨论(0)
提交回复
热议问题