JComboBox that is a Menu
Asked Answered
A

2

6

I'm looking to create a JComboBox that acts as a menu. For example, when you drop it down, you can select items like you would in a JMenuBar.

So it would take JMenus and JMenuItems as instead of Strings.

Is this possible?

Absorber answered 14/3, 2011 at 18:8 Comment(3)
That is the way a JCombobox works. You click on an item to select it. You can add an ActionListener to the combo box to do so processing based on the item selected. I don't see the reason for confusing the user by trying to mix the functionality of a menu and a combo box.Roger
I dont see any difference between JMenu and JCombobox because both uses JPopupMenu. JMenu acts as JCombobox what is the need of JCombobox?Chuddar
I want it to be able to have submenus. So instead of just dropping a list, it drops a list of menus, you can then go into that menu. I would like it to be consistent with the other components of my GUI. I didnt want to have a random menu in thereAbsorber
A
2

One way to accomplish this would be to create a button that when clicked shows a JPopupmenu, just below the button. The menu would allow the user to select from the menu or submenu. The label/selection of the original button should be changed when a menu item is selected.

Aggappe answered 14/3, 2011 at 19:32 Comment(0)
C
-1

Is this you are looking for??

  //package combo2;

  import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;

  public class Combo2 implements ItemListener {

JFrame f1;
JComboBox c;
JPanel p ;

JLabel j;
Combo2()
{
JFrame f1 = new JFrame("Selection");
            Container f = new Container();
            f.setLayout(new FlowLayout());

            String s [] = {"Red","Green","Yellow","Black"};
            c = new JComboBox(s);
            j = new JLabel();
             p= new JPanel();

            c.addItemListener(this);

            f1.add(p);
            p.add(c);
            p.add(j);

            f1.setSize(500,500);
            f1.setVisible(true);

            }
            public void itemStateChanged(ItemEvent ie)
            {
            String str = (String)c.getSelectedItem();
            j.setText(str);
            }
                public static void main(String[] args) {
                    Combo2 l = new Combo2();
                }
            }
China answered 23/7, 2013 at 13:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.