Hi
I am writing a code in java, which contains a drop down menu when I click on an item in a menu I should be able to add a button with that course. Unfortunetally, this is not working for me. I can't get the button to be displayed. I am posting my code, plz check it. Any help is greatly appreciated. Thanks
Code:
public class ComboBoxFrame extends JFrame {
private static final long serialVersionUID = 1L;
private JComboBox b;
private String s;
private int x = 0;
private JButton g[] = new JButton[10];
private String courses[] =
{ "Advanced Functions", "Calculus", "Chemistry", "Physics" };
public ComboBoxFrame() {
super("Pick your courses!");
setLayout(new GridLayout(3, 3));
b = new JComboBox(courses);
b.addItemListener( new ItemListener() {
public void itemStateChanged(ItemEvent event) {
if(event.getStateChange() == ItemEvent.SELECTED) {
s = (String) b.getSelectedItem();
g[x] = new JButton(s);
add(g[x]);
//g[x].addActionListener(this);
x++;
} } } );
add(b);
}
public static void main(String args[]) {
ComboBoxFrame comboBoxFrame = new ComboBoxFrame();
comboBoxFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
comboBoxFrame.setSize( 350, 150 );
comboBoxFrame.setVisible( true );
}
}
Bookmarks