简体   繁体   English

只读JFormattedTextField

[英]Readonly JFormattedTextField

Supossing that a JFormattedTextField is enabled and contains a formatter, is there a way to make it readonly? 假设JFormattedTextField已启用并且包含格式化程序,是否有办法将其设置为只读? 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. 使用JTextField ,我们只需要提供一个自定义Document并在那里进行所有控制,但是使用JFormattedTextField,我想事情就有些复杂了,因为我们不想弄乱它的文档过滤器。 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. 更新:仅是我的观点,但是从这个意义上讲, JFormattedTextField的设计是如此糟糕,以至于没有留下简单的解决方案来解决这个简单的问题。

Marcos 马可斯

Can't you disable editing by setting isEditable to false? 您不能通过将isEditable设置为false来禁用编辑吗?

textfield.setEditable(false); textfield.setEditable(false);

You can simply use: 您可以简单地使用:

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

just like for the JTextField since JFormattedTextField extends JTextField 就像JTextField一样,因为JFormattedTextField扩展了JTextField

Check out the javadoc as well. 还要查看javadoc

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM