Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic JComboBoxes

I have following data (String):

Course1: A1
Course1: A2
Course2: B1
Course2: B2
Course2: B3
Course2: B4
Course3: C1
Course3: C2

I'd like to create two JComboBox (JComboBox1, JComboBox2) so that JComboBox1 contains Course1,Course2,Course3, etc.

If I select, say, Course2 from JComboBox1 then corresponding B1,B2,B3,B4 should be populated in JComboBox2.

How to implement this? Many Thanks.

like image 879
Pujan Avatar asked Jul 07 '10 04:07

Pujan


People also ask

What is a JComboBox good for?

Commonly used Methods: It is used to add an item to the item list. It is used to delete an item to the item list. It is used to remove all the items from the list. It is used to determine whether the JComboBox is editable.

What is JComboBox in Java Swing?

JComboBox is a part of Java Swing package. JComboBox inherits JComponent class . JComboBox shows a popup menu that shows a list and the user can select a option from that specified list . JComboBox can be editable or read- only depending on the choice of the programmer .

How remove all items from JComboBox in Java?

use . removeAllItems() methods to remove all items from combo box.

What is DefaultComboBoxModel?

DefaultComboBoxModel(Object[] items) Constructs a DefaultComboBoxModel object initialized with an array of objects. DefaultComboBoxModel(Vector v) Constructs a DefaultComboBoxModel object initialized with a vector.


2 Answers

Yes, simply create a DefaultComboBoxModel for each set, and do setModel() on JComboBox2 when JComboBox1 changes.

Addendum: For example,

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ComboTest extends JPanel implements ActionListener, Runnable {

    private final JComboBox combo1 = new JComboBox(
        new String[]{"Course 1", "Course 2", "Course 3"});
    private final JComboBox combo2 = new JComboBox();
    private ComboBoxModel[] models = new ComboBoxModel[3];

    public ComboTest() {
        models[0] = new DefaultComboBoxModel(
            new String[]{"A1", "A2"});
        models[1] = new DefaultComboBoxModel(
            new String[]{"B1", "B2", "B3", "B4"});
        models[2] = new DefaultComboBoxModel(
            new String[]{"C1", "C2"});

        combo2.setModel(models[0]);
        this.add(combo1);
        this.add(combo2);
        combo1.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        int i = combo1.getSelectedIndex();
        combo2.setModel(models[i]);
    }

    @Override
    public void run() {
        JFrame f = new JFrame("ComboTest");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new ComboTest());
    }
}
like image 68
trashgod Avatar answered Oct 13 '22 09:10

trashgod


Yes. You can add a change event listener to the first JComboBox that updates the values of the second JComboBox.

Something like this

// first comboBox
final JComboBox courseBox = new JComboBox(
                               new String[]{"Course 1", "Course 2", "Course 3"});

final JComboBox box2 = new JComboBox();

// Now listen for changes
courseBox.addActionListener(new ActionListener(){
   void actionPerformed(ActionEvent e){
       if(courseBox.getSelectedItem().equals("Course 1")){
           // we know that the user picked "Course 1", now change box2 to match
           // first clear everything
           box2.removeAllItems();
           // now add back relevant values
           box2.addItem("A1");
           box2.addItem("A2");
       }else if(...){
           // ...
       }
   }
});
like image 36
Mark Elliot Avatar answered Oct 13 '22 09:10

Mark Elliot