Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes, simply create a <a href="http://java.sun.com/javase/6/docs/api/javax/swing/DefaultComboBoxModel.html" rel="noreferrer"><code>DefaultComboBoxModel</code></a> for each set, and do <code>setModel()</code> on <code>JComboBox2</code> when <code>JComboBox1</code> changes.</p> <p>Addendum: For example,</p> <pre><code>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()); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload