Search code examples
javaswingjcomboboxjtextfieldjdialog

how to modify the values of other fields in JDialog box based on the value in combo box


I am making a dialog box for the application.And I want to fetch the values from the other fields(such as textField) based on the value of combo box. Can anyone please tell me how to link both of these components? -Thanks in advance


Solution

  • There are no magic methods for just "linking" components together. From your question I understand that you want to interpret data in a textfield based on current choice of a combobox or something like that? So when you read your data, use JComboBox.getSelectedItem()/getSelectedIndex() to apply your logic.

    If you want to change data in other fields, or disable them, based on a current choice, add a listener:

    comboBox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int index = combo.getSelectedIndex();
            if (index == 0) {
                //disable some textfields or change format if it's a JFormattedField
            } 
        }
    });