开发者

JComboBox that is a Menu

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 i开发者_开发技巧n a JMenuBar.

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

Is this possible?


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.


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();
                }
            }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜