0

Is there a way to show a menu item list when you click on a Jbutton? For example if I click on the Jbutton labelled wrench, it would show a menu item list with 2 options, like this example. Here's my code (the text emphasised with ** is the button I want to show a menu item list when clicked on):

appletFrame = new JFrame(Settings.serverName);
        Loader.webclient = false;
        appletFrame.setLayout(new BorderLayout());
        appletFrame.setDefaultCloseOperation(3);
        JPanel appletPanel = new JPanel(new BorderLayout()); 
        appletFrame.setIconImage(Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("/resources/icon.png")));
        appletPanel.add(this);
        appletPanel.setPreferredSize(new Dimension(767, 655));
        appletFrame.getContentPane().add(appletPanel, BorderLayout.CENTER);
        appletFrame.pack();
        appletFrame.setLocationRelativeTo(null);
        appletFrame.setVisible(true);
        JMenuBar jmenubar = new JMenuBar();
        appletPanel.add(jmenubar, BorderLayout.PAGE_START); 
        layout = new FlowLayout();
        **ImageIcon wrench = new ImageIcon(Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("/resources/wrench.png")));**
        ImageIcon key = new ImageIcon(Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("/resources/key.png")));
        JButton button1 = new JButton("Vote");
        JButton button2 = new JButton("Item List");
        JButton button3 = new JButton("Screenshot");
        **JButton button4 = new JButton(wrench);**
        JButton button5 = new JButton(key);
        **button4.setBorder(null);
        **button4.setBorderPainted(false);**
        **button4.setContentAreaFilled(false);**
        button5.setBorder(null);
        button5.setBorderPainted(false);
        button5.setContentAreaFilled(false);
        jmenubar.setLayout(layout);
        jmenubar.add(button1);
        jmenubar.add(button2);
        jmenubar.add(button3);
        **jmenubar.add(button4);**
        jmenubar.add(button5);
        button1.addActionListener(this);
        button2.addActionListener(this);
        button3.addActionListener(this);
        **button4.addActionListener(this);**
        button5.addActionListener(this);
Chris
  • 44,602
  • 16
  • 137
  • 156
0x29A
  • 911
  • 1
  • 8
  • 13

1 Answers1

0

Are you needing a SplitButton? Then there're some implemented package (include the Netbeans UI Components), you may love them.

If you need a tiny package for that, please have a look at following link: http://code.google.com/p/jsplitbutton/

Tu Tran
  • 1,957
  • 1
  • 27
  • 50