Home > Java > javaTutorial > How to Create Interconnected Dynamic JComboBoxes in Java?

How to Create Interconnected Dynamic JComboBoxes in Java?

Susan Sarandon
Release: 2024-12-16 11:24:16
Original
808 people have browsed it

How to Create Interconnected Dynamic JComboBoxes in Java?

How to Create Dynamic JComboBoxes

In order to create two dynamic JComboBoxes which are interconnected, such that when a value is selected in the first JComboBox, the second JComboBox gets populated with corresponding values, follow these steps:

1. Create a DefaultComboBoxModel for each set of data.
This model will store the values that will be populated in the second JComboBox.

2. Set the model of the second JComboBox to the corresponding model when the value in the first JComboBox changes.
This will ensure that the second JComboBox gets populated with the appropriate values based on the selection in the first JComboBox.

Example:

The following Java code demonstrates how to implement this functionality:

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());
    }
}
Copy after login

This code will create a JFrame with two JComboBoxes. When you select a value from the first JComboBox, the second JComboBox will be populated with the corresponding values.

The above is the detailed content of How to Create Interconnected Dynamic JComboBoxes in Java?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template