Solved

toolbar button with drop-down menu

Posted on 2001-07-18
3
826 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses

751 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