简体   繁体   中英

Making a label editable depending on a variable in an array

I am making a registration page but I am fairly new to Java, I have a combo box for peoples Titles such as "Mr, Mrs, Miss, etc" one of the options is "Other..." And I have a text field next to the combo box to specify your title, I would like the text field to not be editable unless someone selects "Other..." in the combo box, How would I do this?

What it looks like at the moment: I can't see what I'm doing wrong?

TitleSpecifyChoiceField.setEditable(false);
    TitleSpecifyChoiceField.setText("Please specify title...");


    TitleChoice.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Mr", "Mrs", "Miss", "Ms", "Dr", "Other..." }));
    TitleChoice.setToolTipText("");
    TitleChoice.addItemListener(new ItemListener(){
        public void itemStateChanged(ItemEvent e) {


if (TitleChoice.getSelectedItem().equals("Other...")){
TitleSpecifyChoiceField.setEditable(true);
    };

You would do this the same way you'd respond to any changes in a JComboBox -- by adding a listener to the JComboBox as per the Swing combo box tutorial . Inside the listener, change the setEnabled(...) setting on the JTextField depending on the selected item. ie, by calling getSelectedItem() on the JComboBox and testing whether calling equalsIgnoreCase("other") is true.

Note that I recommend that you use setEnabled(...) not setEditable(...) as the former will give the user visual cues as to whether the JTextField should be edited or not.


Edit
Regarding your code:

TitleSpecifyChoiceField.setEditable(false);
TitleSpecifyChoiceField.setText("Please specify title...");

TitleChoice.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Mr", "Mrs", "Miss", "Ms", "Dr", "Other..." }));
TitleChoice.setToolTipText("");
TitleChoice.addItemListener(new ItemListener(){
  public void itemStateChanged(ItemEvent e) {
    if (TitleChoice.getSelectedItem().equals("Other...")){
      TitleSpecifyChoiceField.setEditable(true);
    }
  }
});

Some problems and issues:

  • Does your JComboBox use Strings or does it hold other types of items?
  • You will want to add debugging code into your code to try to isolate the problem. For instance, inside of your ItemListener, add a System.out.println(...) to print out the selected item to be sure that the listener is working as expected.
  • You are checking if the item .equals("Other...") , a String literal. Instead consider making a String constant, OTHER that the JComboBox uses and that you test in the listener to be sure that the tested String and and the displayed are the same.
  • Again, I suggest you use setEnabled(...) not setEditable(...) .
  • You should learn and follow Java naming conventions including beginning all variable names with lower case letter as this will help us to better understand your code.
  • You should fix your posted code indentation so that it is regular and makes sense (note my code above vs yours). Why do you want to make it harder for folks who are trying to help you to understand your code? Your job is to make ours as easy as possible as we're all volunteers.
  • Create and post an sscce to get the best and fastest help.

Add a listener to the combo box. When the selected item changes, call setEditable() on the text field.

You could try adding an ItemListener to your JComboBox and (as @HovercraftFullOfEels suggested) use setEnabled as opposed to setEditable . For a general idea, you could do something like this:

    JTextField textField = ...;
    JComboBox<String> comboBox = ...;
    comboBox.addItemListener(
            new ItemListener(){
                public void itemStateChanged(ItemEvent e){
                    final String selected = (String)comboBox.getSelectedItem();
                    textField.setEnabled(selected.equals("other"));
                }
            }
    );

Or if you are using Java 8 you could use this:

    JTextField textField = ...;
    JComboBox<String> comboBox = ...;
    comboBox.addItemListener(
            e -> {
                final String selected = (String)comboBox.getSelectedItem();
                textField.setEnabled(selected.equals("other"));
            }
    );

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