Results 1 to 4 of 4

Thread: Java drop down menu

  1. #1
    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.

  2. #2
    Join Date
    May 2008
    Posts
    2,389

    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.

  3. #3
    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.

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    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.

Similar Threads

  1. How to add drop-down menu in Word ?
    By Japheth in forum Windows Software
    Replies: 21
    Last Post: 19-01-2013, 02:29 AM
  2. Windows 7: Drop-down Menu Malfunction
    By Esmel in forum Operating Systems
    Replies: 4
    Last Post: 13-12-2010, 10:25 AM
  3. How to create drop down menu in php
    By Sukhvinder in forum Software Development
    Replies: 3
    Last Post: 06-08-2009, 11:42 AM
  4. How to add drop-down menu in Word ?
    By Japheth in forum Windows Software
    Replies: 0
    Last Post: 23-03-2009, 02:28 PM
  5. ListBox Drop down menu in vb.net
    By SamsherR in forum Software Development
    Replies: 3
    Last Post: 16-02-2009, 04:46 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,140,118.23232 seconds with 17 queries