Solved

toolbar button with drop-down menu

Posted on 2001-07-18
3
817 Views
Last Modified: 2012-05-04

How do I make a toolbar button (icon) with a drop-down menu to select from. I see this everywhere in browsers, even in many Java applications (like JBuilder). It is even in the Java L&F guidelines, but they don't tell you how to do it.

Anyone know? What it should be is an icon (or even a combo box looking thing with an icon), and when you click on it you get a standard menu dropped-down with menu items (ie. text) to select from. After choosing the item, the icon changes to reflect the selection.

Thanks in advance,
Joel
0
Comment
Question by:joeltt
  • 2
3 Comments
 
LVL 1

Expert Comment

by:poluru
ID: 6297082
have a look at the following code

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

public class PopUp extends JFrame implements ActionListener
{
     JPopupMenu pop;
     JButton jb;
     public PopUp()
     {
          super("PopUpMenu Demo");
          JToolBar bar=new JToolBar();
          jb=new JButton("Pop");
          bar.add(jb);
          pop=new JPopupMenu();
          pop.add("First Action");
          pop.add("Second Action");
          pop.add("Third Action");
          pop.add("Fourth Action");
          pop.setInvoker(jb);
          jb.addActionListener(this);
          getContentPane().add(bar);
          pack();
          setVisible(true);

     }
     public void actionPerformed(ActionEvent ae)
     {
          pop.show(jb,10,10);
     }
     public static void main(String args[])
     {
          new PopUp();
     }
}

0
 
LVL 3

Author Comment

by:joeltt
ID: 6302279
I didn't see how to grab the selection event from the Popup menu. How do I get it so I can change the icon on the button?

Thanks,
Joel
0
 
LVL 1

Accepted Solution

by:
poluru earned 100 total points
ID: 6307133
import javax.swing.*;
import java.awt.event.*;

public class PopUp extends JFrame implements ActionListener
{
     JPopupMenu pop;
     JButton jb;
     JMenuItem jm[]=new JMenuItem[4];

     public PopUp()
     {
          super("PopUpMenu Demo");
          JToolBar bar=new JToolBar();
          jb=new JButton("Pop");
          bar.add(jb);
          pop=new JPopupMenu();
          jm[0]=pop.add("First Action");
          jm[1]=pop.add("Second Action");
          jm[2]=pop.add("Third Action");
          jm[3]=pop.add("Fourth Action");
          for(int i=0;i<jm.length;i++)
          {
               jm[i].addActionListener(this);
          }
          pop.setInvoker(jb);
          jb.addActionListener(this);
          getContentPane().add(bar);
          pack();
          setVisible(true);

     }
     public void actionPerformed(ActionEvent ae)
     {
          if(ae.getSource()==jm[0])
          {
               /*
                 here u can do what ever u want
                 i.e. when first MenuItem is Clicked
                 like changing the button icon...
               */
               JOptionPane.showMessageDialog(this,"First Action Clicked");
          }
          else if(ae.getSource()==jm[1])
          {
               JOptionPane.showMessageDialog(this,"Second Action Clicked");
          }
          else if(ae.getSource()==jm[2])
          {
               JOptionPane.showMessageDialog(this,"Third Action Clicked");
          }
          else if(ae.getSource()==jm[3])
          {
               JOptionPane.showMessageDialog(this,"Fourth Action Clicked");
          }
          else
          {
               java.awt.Dimension d=jb.getPreferredSize();
               pop.show(jb,0,d.height);
          }
     }
     public static void main(String args[])
     {
          new PopUp();
     }
}
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question