Java JTextField setEnabled 与 setEditable

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21695175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:06:48  来源:igfitidea点击:

JTextField setEnabled vs setEditable

javaswingjtextfield

提问by Tariq

What is the difference between JTextField.setEnabled()and JTextField.setEditable()? In my code I have an Instance of a class inherited from JTextField. But when I set its property setEnabled(false)I can still edit and process its contents in my program. However when I set its property setEditable(false)I can no longer edit its text. If it is so. Then what is the purpose of setEnabled()property here?

JTextField.setEnabled()和 和有JTextField.setEditable()什么区别?在我的代码中,我有一个从JTextField. 但是当我设置它的属性时,setEnabled(false)我仍然可以在我的程序中编辑和处理它的内容。但是,当我设置它的属性时,setEditable(false)我无法再编辑它的文本。如果是这样。那么setEnabled()财产在这里的目的是什么?

My Code for inherited class is:

我的继承类代码是:

 private static class CCField extends JTextField{
      private static final long serialVersionUID = 1L;
      public CCField() {
        this( DEFAULT_COLUMN_COUNT );
      }

      public CCField(final int cols) {
        super( cols );
 }

Added INFOWhen I call the setEnabled()property of this class it does not show any effect on the text field and it still remains enabled. I have a container Jcomponentin my code which have this CCFieldas a child component. So when I try to disable it using setEnabled(false)it still editable. Whereas when I try to disable it using setEditable(false)then it is disabled. This is how I am accessing this textField in my container:

添加了信息当我调用setEnabled()此类的属性时,它不会对文本字段显示任何影响,并且仍保持启用状态。Jcomponent我的代码中有一个容器,它将它CCField作为子组件。所以当我尝试禁用它时,setEnabled(false)它仍然可以编辑。而当我尝试禁用它时,setEditable(false)它被禁用。这就是我在容器中访问此 textField 的方式:

 JComponent jComp = DDEUtil.getComponent(icTableLayout,icDS);
 ((JTextField)jComp.getComponent(1)).setEditable(false);

And what is going on in DDEUtil.getComponentis too complex as it involve a number of classes and not possible to post here.

正在发生的事情DDEUtil.getComponent太复杂了,因为它涉及许多类,无法在此处发布。

I wonder I have not overridden any method of this component so why is it showing this behavior.

我想知道我没有覆盖这个组件的任何方法,为什么它会显示这种行为。

采纳答案by ravibagul91

In my case setEditable(false)is graying the field and setEnabled(false)not graying the field.

在我的情况下,setEditable(false)是使字段setEnabled(false)变灰而不是使字段变灰。

TextField are editable by default. The code setEditable(false) makes the TextField uneditable. It is still selectable and the user can copy data from it, but the user cannot change the TextField's contents directly.

默认情况下,TextField 是可编辑的。代码 setEditable(false) 使 TextField 不可编辑。它仍然是可选的,用户可以从中复制数据,但用户不能直接更改 TextField 的内容。



The code setEnabled(false), disables this TextField. It is not selectable and the user can not copy data from it and the user cannot change the TextField's contents directly.

代码 setEnabled(false) 禁用此 TextField。它是不可选择的,用户不能从中复制数据,用户不能直接更改 TextField 的内容。



Useful Links

有用的链接

  1. How to Use Text Fields
  2. Component#setEnabled()
  1. 如何使用文本字段
  2. 组件#setEnabled()

回答by das_j

While setEnabled(false)grays out the field comletely, setEditable(false)just prevents it from beeing edited, but it will still look the same.

虽然setEnabled(false)将字段完全变灰,setEditable(false)只是防止它被编辑,但它仍然看起来一样。