Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 05-11-2009
Member
 
Join Date: Jul 2009
Posts: 140
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.
Reply With Quote
  #2  
Old 05-11-2009
Member
 
Join Date: May 2008
Posts: 2,383
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 );
}
}
If any problem please do post back.
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
  #3  
Old 05-11-2009
Member
 
Join Date: Jul 2009
Posts: 140
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.
Reply With Quote
  #4  
Old 05-11-2009
Member
 
Join Date: May 2008
Posts: 2,383
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
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Java drop down menu"
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


All times are GMT +5.5. The time now is 02:23 AM.