Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.awt.GridLayout;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Main {

    public static void main(String[] args) {
        JPanel gui = new JPanel(new GridLayout(0, 1, 5, 5));

        String[] speciesName = { "1", "2", "3" };
        String[][] breedName = { { "A", "P", "A" }, { "B", "P", "S" }, { "DDo", "A", "P" } };
        JComboBox<String> petSpecies = new JComboBox<>(speciesName);
        JComboBox<String> petBreed = new JComboBox<>();
        petSpecies.addItemListener(e -> {
            int ii = petSpecies.getSelectedIndex();
            ComboBoxModel cbm = new DefaultComboBoxModel(breedName[ii]);
            petBreed.setModel(cbm);
            petBreed.requestFocusInWindow();
        });
        gui.add(petSpecies);
        gui.add(petBreed);

        JOptionPane.showMessageDialog(null, gui);
    }
}