Readonly JFormattedTextField

℡╲_俬逩灬. 提交于 2019-12-02 22:10:08

问题


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 Document and do all the control there, but with a JFormattedTextField I suppose that things are a little more complicated as we don't want to mess with its document filter. I really need this feature. So, is there a way to achieve this?

UPDATE: Just my opinion, but it seems that the JFormattedTextField is so bad designed in this sense that it leaves no easy solution to this simple problem.

Marcos


回答1:


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.




回答2:


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

textfield.setEditable(false);




回答3:


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


来源:https://stackoverflow.com/questions/15904796/readonly-jformattedtextfield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!