简体   繁体   中英

How to enable label and textfield when combo box item selected

I created one combobox with two item as " Active " and " Droped " then i take one label with jtextfield and write code for enable and disable of label and jtextfield in the jcomboobx action event so code is working but here one problem which is that label and jtextfield initially not disabled...when i select item " Droped " then select item " Active " after its going disabled otherwise initially when run jframe, label and jtextfield enabled so i want how to jlablel and Jtextfield enable when jcombobx select item " Droped " only

source code:

private void wtdlActionPerformed(java.awt.event.ActionEvent evt) {                                     

        txt_reason.setEnabled(false); //txt_reason is jTextField
        lab.setEnabled(false); //lab is jLabel

        if(wtdl.getSelectedItem().equals("Active")) //wtdl is jConobbox
        {
        txt_reason.setEnabled(false);
        lab.setEnabled(false);
        }
      else if(wtdl.getSelectedItem().equals("Droped"))
        {
        txt_reason.setEnabled(true); 
        lab.setEnabled(true);
        }
    }

check snapshot:

在此处输入图片说明

Simply create a method updateState() as following:

protected void updateState() {
    boolean enabled = wtdl.getSelectedItem().equals("Droped");
    txt_reason.setEnabled(enabled ); 
    lab.setEnabled(enabled );
}

and call it after you have initialized your components and in your actionPerformed()

Unless I misunderstood:

You should add code to set the JTextField and JLabel disabled via setEnabled(false) after the component(s) has been created.

If you are using a IDE Im sure you can set the property of the component by right clicking on it and navigating to properties (this will than of course become the default when JFrame is created).

You could disable the JTextField once you have created the component. For example:
JTextField txtYourTextField = new JTextField();

txtYourTextField.setEnabled(false);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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