Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

toolbar button with drop-down menu

Posted on 2001-07-18
3
Medium Priority
?
844 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 400 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to implement Singleton Design Pattern in Java.
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.
Suggested Courses

963 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