本文介绍了JTextField setEnabled与setEditable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

我对继承类的代码是:

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

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

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 Jcomponent in my code which have this CCField as 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:

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

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

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

我想知道我没有重写此组件的任何方法,为什么它显示这种行为.

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

推荐答案

在我的情况下,setEditable(false)使字段变灰,而setEnabled(false)没有使字段变灰.

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



有用链接

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

这篇关于JTextField setEnabled与setEditable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 02:48