0% found this document useful (0 votes)
12 views

Chain Select Menu

This document provides an example of how to create a chain-select or dependent dropdown menu in Java using the Swing library. The example creates two combo boxes where the options in the second box depend on the selection in the first. When an item is selected in the first box, it triggers an action listener to update the options in the second box based on the selected index.

Uploaded by

jyoti madje
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Chain Select Menu

This document provides an example of how to create a chain-select or dependent dropdown menu in Java using the Swing library. The example creates two combo boxes where the options in the second box depend on the selection in the first. When an item is selected in the first box, it triggers an action listener to update the options in the second box based on the selected index.

Uploaded by

jyoti madje
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Certainly!

If you want to create a chain-select menu program in Java, you might be


referring to a cascading or dependent dropdown menu where the options in one menu
depend on the selection in the previous menu. Here's a simple example using the
Java Swing library for a desktop application. This example assumes you have a basic
understanding of Java and Swing.

```java
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ChainSelectMenu extends JFrame {


private JComboBox<String> firstMenu;
private JComboBox<String> secondMenu;

private String[] options1 = {"Option 1A", "Option 1B", "Option 1C"};


private String[][] options2 = {
{"Option 2A1", "Option 2A2", "Option 2A3"},
{"Option 2B1", "Option 2B2", "Option 2B3"},
{"Option 2C1", "Option 2C2", "Option 2C3"}
};

public ChainSelectMenu() {
setTitle("Chain Select Menu");
setSize(300, 150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// First menu
firstMenu = new JComboBox<>(options1);
add(firstMenu);

// Second menu
secondMenu = new JComboBox<>();
add(secondMenu);

// Add action listener to the first menu


firstMenu.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
updateSecondMenu();
}
});
}

private void updateSecondMenu() {


// Clear the second menu
secondMenu.removeAllItems();

// Get the selected index from the first menu


int selectedIndex = firstMenu.getSelectedIndex();

// Add options to the second menu based on the selected index


for (String option : options2[selectedIndex]) {
secondMenu.addItem(option);
}
}

public static void main(String[] args) {


SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new ChainSelectMenu().setVisible(true);
}
});
}
}
```

In this example, the program creates a JFrame with two JComboBox components. The
options in the second menu depend on the selection in the first menu. When an item
is selected in the first menu, it triggers an action to update the second menu
based on the selected index.

Please note that this is a basic example, and you may need to modify it according
to your specific requirements or integrate it into a larger application.

You might also like