Solved

toolbar button with drop-down menu

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

896 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