JComboBox is a menu

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

Thus, instead of strings, JMenus and JMenuItems will be used.

Is it possible?

+5
source share
2 answers

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

+2
source

Are you looking for this?

  //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();
                }
            }
-1
source

All Articles