-2

I have tried to use the suggested methods but unable to determine how I can use action listeners within an action listener as suggested...

I want to change the value of first combo box and want the next combo box to be updated automatically upon change, similarly text box is changed when combobox_1 is changed...

    String[] b = a.getCourseCodes();
    final List f = new ArrayList();

    final JComboBox comboBox = new JComboBox(b);
    comboBox.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent arg0) {

            String item = comboBox.getSelectedItem().toString();
         }
    });

    comboBox.setEditable(true);

    comboBox.setBounds(360, 70, 86, 20);
    contentPane.add(comboBox);

    JLabel lblStudentName = new JLabel("Student Name");
    lblStudentName.setBounds(270, 149, 80, 14);
    contentPane.add(lblStudentName);

    String[] v = a.getStudentID(comboBox.getSelectedItem().toString());
    final JComboBox comboBox_1 = new JComboBox(v);
    comboBox_1.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent arg0) {

            String item = comboBox_1.getSelectedItem().toString();
         }
    });

    comboBox_1.setBounds(360, 108, 86, 20);
    contentPane.add(comboBox_1);

    textField_3 = new JTextField();
    String y = a.getStudentName(comboBox_1.getSelectedItem().toString());
    textField_3.setText(y);
    textField_3.setEditable(false);
    textField_3.setBounds(360, 146, 86, 20);
    contentPane.add(textField_3);
    textField_3.setColumns(10);

Kindly help by editing the code so can have a clear idea... Thanks

Danish Ali
  • 137
  • 7
  • 20
  • 2
    I'm not sure that the code you've posted is adequate for us to determine just what you might be doing wrong, other than the `item` String's scope is limited to the if block within the `itemStateChanged(...)` method. If you need that String elsewhere, you're stuck. For better help, consider creating and posting an [**MCVE**](http://stackoverflow.com/help/mcve). We don't want to see your whole program, but rather you should condense your code into the smallest bit that still compiles, has no extra code that's not relevant to your problem, but still demonstrates your problem. – Hovercraft Full Of Eels Jul 25 '15 at 19:20
  • 1
    Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). Further, statements like `textField_3 = new JTextField();` should instead be more like `textField_3 = new JTextField(10);` to suggest a required size (in columns: 10 for that one). – Andrew Thompson Jul 25 '15 at 19:47

1 Answers1

2

Rather then adding an ItemListener I would simply add an ActionListener which will be triggered every time the selected value is changed. Then you are able to just use comboBox.getSelectedItem() like so:

JComboBox comboBox_1; //you need to declare the comboBox and textField before the ActionListener.
comboBox.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        String[] v = a.getStudentID(comboBox.getSelectedItem().toString());
        comboBox_1.setModel(new DefaultComboBoxModel<String>(v));

        String y = a.getStudentName(comboBox_1.getSelectedItem().toString());
        textField_3.setText(y);
    }
});

Add you could extend this to change the values of your ComboBox or TextField within the actionPerformed method.

I think this is what you mean, though I may be wrong in the intent of your ActionListener.

Stephen Buttolph
  • 643
  • 8
  • 16
  • Still not working, is this the right format??? final JComboBox comboBox = new JComboBox(b); comboBox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { String item = comboBox.getSelectedItem().toString(); String[] v = a.getStudentID(item); final JComboBox comboBox_1 = new JComboBox(v); comboBox_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { String item1 = comboBox.getSelectedItem().toString(); String y = a.getStudentName(item1); textField_3 = new JTextField(y); } }); } }); – Danish Ali Jul 25 '15 at 20:03
  • @DanishAli What part isn't working? This alone won't done anything visible. you need to add your own code that will change the values of your `ComboBox` and `TextField`. – Stephen Buttolph Jul 25 '15 at 20:08
  • @DanishAli I'm not really sure that I know fully what you are trying to do. However, I don't think you should be recreating a `ComboBox` inside of the `ActionListener`. – Stephen Buttolph Jul 25 '15 at 20:17
  • 1
    @DanishAli: Please don't post code in comments since it loses its formatting making it unreadable. Instead, post any new code to the bottom of your original question by [editing your question](http://stackoverflow.com/posts/31629989/edit). Also, please don't ignore the comment I made to your original question as it offers advice that if you follow should help us to better understand your problem, and thus better help you. – Hovercraft Full Of Eels Jul 25 '15 at 20:18
  • @HovercraftFullOfEels Noted. Thanks – Danish Ali Jul 25 '15 at 21:09
  • @DanishAli I updated my answer to include how you would use an `ActionListener` to update a `ComboBox` and `TextField`. – Stephen Buttolph Jul 25 '15 at 21:31