Solved

toolbar button with drop-down menu

Posted on 2001-07-18
3
796 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
mapShare challenge 13 69
base64 decode encode 12 100
eclipse formatting 6 43
Groovy:unable to resolve class error 2 31
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now