#1
| |||
| |||
Java drop down menu 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 ); } } Last edited by Ameeryan : 05-11-2009 at 01:20 PM. |
#2
| |||
| |||
Re: Java drop down menu Check out this modified code. This will help you. 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 ComboBoxFrame me; private String courses[] = { "Advanced Functions", "Calculus", "Chemistry", "Physics" }; public ComboBoxFrame() { super("Pick your courses!"); setLayout(new GridLayout(3, 3)); me = this; b = new JComboBox(courses); b.addItemListener( new ItemListener() { public void itemStateChanged(ItemEvent event) { if(event.getStateChange() == ItemEvent.SELECTED) { s = (String) b.getSelectedItem(); System.out.println(s); g[x] = new JButton(s); add(g[x]); me.validate(); //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 ); } }
__________________ The FIFA Manager 2009 PC Game |
#3
| |||
| |||
Re: Java drop down menu Thanks When I click on the button it should be removed, but its not working. I am trying it with the remove and repaint methods but I am not getting the desired result. Thanks in advance. |
#4
| |||
| |||
Re: Java drop down menu Hi You need to use an Action Listener with the button as they are created, also it depends how you declare the ItemListner. Any more queries feel free to ask, I will try to solve them to my best.
__________________ The FIFA Manager 2009 PC Game |
![]() |
|
Tags: java, programming language |
Thread Tools | Search this Thread |
|
![]() | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
How to add drop-down menu in Word ? | Japheth | Windows Software | 21 | 19-01-2013 02:29 AM |
Windows 7: Drop-down Menu Malfunction | Esmel | Operating Systems | 4 | 13-12-2010 10:25 AM |
How to create drop down menu in php | Sukhvinder | Software Development | 3 | 06-08-2009 11:42 AM |
How to add drop-down menu in Word ? | Japheth | Windows Software | 0 | 23-03-2009 02:28 PM |
ListBox Drop down menu in vb.net | SamsherR | Software Development | 3 | 16-02-2009 04:46 PM |