Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Drop Down JToolBar Element?

16 views
Skip to first unread message

Nicholas Pappas

unread,
Sep 1, 2002, 11:12:01 AM9/1/02
to
I was wondering if there is an element, or a built in mutation of
JComboBox (perhaps), that provided a drop down button like some programs
have for their Undo button.
The button is an icon w/ an arrow next to it. Hit the icon and you undo
(in this case) the last change. Hit the arrow and you get a list of all
the actions, allowing you to select multiple undos at once.

I thought I remembered seeing this widget in one of my books, but maybe
I'm starting to remember things for lack of sleep (and too much Dew).
It seems like I can create a simple button that will trigger a popup
element with a list in it (or whatever element I need). Is the route I
am going to need to take?

Many thanks, as always!

Nick
--
My software has no bugs. It just develops random features.

reply to ni...@DROPME.rightstep.org

Nicholas Pappas

unread,
Sep 1, 2002, 11:30:19 AM9/1/02
to
I found an example of what I am trying to do:
http://www.esus.com/docs/GetQuestionPage.jsp?uid=1046

... but then want me to subscribe before getting the code. I'm going to
keep digging around since I know I was not hallucinating and the widget
is possible. :)

Nicholas Pappas

unread,
Sep 1, 2002, 12:54:25 PM9/1/02
to
I broke down and paid for the subscription... curse my impatience. :(
Hopefully I'll be able to find other good source there that will make the
cost worthwhile. :P

Nicholas Pappas wrote:
> I found an example of what I am trying to do:
> http://www.esus.com/docs/GetQuestionPage.jsp?uid=1046

--

Frank Breuer

unread,
Sep 1, 2002, 1:57:11 PM9/1/02
to
Nicholas Pappas <righ...@hotmail.com> wrote in
news:3D722DA...@hotmail.com:

> I was wondering if there is an element, or a built in mutation of
> JComboBox (perhaps), that provided a drop down button like some
> programs have for their Undo button.

Sun's Look & Feel guidelines specify such a toolbar button. However,
there is no implmentation in Swing. All examples in the L&F guidelines
use a drop-down menu, but don't provide code. There are example images
(maybe done with Photoshop ...), but the guidelines are not even clear
if a click (button down + button up), or already a button down should
activate the menu.

I have never coded such a thingy, but if I would have to, I would start
with a JMenu.

J. Todd Slack

unread,
Sep 1, 2002, 3:06:53 PM9/1/02
to

There is an example of how to do this in the Sun Java LAF Design Guidelines.

I use it my a few of my apps.

-Jason

"Nicholas Pappas" <righ...@hotmail.com> wrote in message
news:3D7231EE...@hotmail.com...

Nicholas Pappas

unread,
Sep 1, 2002, 5:46:29 PM9/1/02
to
Many thanks for the info. I looked at the LAF Guideline book homepage
but could not find the code for that drop down toolbar item.
Did you get the source out of the book? If so, I guess I'll be going to
the store later. ;)

Thomas Weidenfeller

unread,
Sep 2, 2002, 8:55:41 AM9/2/02
to
Nicholas Pappas <righ...@hotmail.com> writes:
> I was wondering if there is an element, or a built in mutation of
> JComboBox (perhaps), that provided a drop down button like some programs
> have for their Undo button.

There is no magic involved, just use a button and a normal popup menu,
so that the popup behaves as adrop-down menu.

Here is some code taken from one of my applications, and simplified
(maybe to much, might not compile :-)):

//
// Create an example toolbar menu with two menu items
//
JPopupMenu ToolbarMenu = new JPopupMenu();
JMenuItem ToolbarMenuItem1 = new JMenuItem();
ToolbarMenuItem1.setText("Item 1");
ToolbarMenu.add(ToolbarMenuItem1);

JMenuItem ToolbarMenuItem2 = new JMenuItem();
ToolbarMenuItem2.setText("Item 2");
ToolbarMenu.add(ToolbarMenuItem2);

//
// Create the toolbar with one toolbar button
//
JToolBar Toolbar = new JToolBar();

// Use a text for demo purposes. In a real application an
// icon with an L&F guidelines compliant marker should be used
JButton ToolbarButton = new JButton();
ToolbarButton.setText("Toolbar Button");

//
// The event handling is not L&F guidelines compliant because
// (a) the guidelines are rather vague regarding what should
// trigger the popup menu, and
// (b) the guidelines don't allow for a default behavior, which
// is, however, rather common.
//
// The following implements:
// Right-Mouse + platform popup trigger: Pop-up the menu
// Left-Click: Default action
//
ToolbarButton.addMouseListener(new MouseAdapter() {
void postToolbarMenu(MouseEvent e) {
if(e.isPopupTrigger()) {
Component c = (Component)e.getSource();
ToolbarMenu.show(c, 0, c.getHeight());
}
}
public void mousePressed(MouseEvent e) {
postToolbarMenu(e);
}
public void mouseReleased(MouseEvent e) {
postToolbarMenu(e);
}
});
ToolbarButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Default action");
}
});

Toolbar.add(ToolbarButton);

JPanel cp = (JPanel) this.getContentPane();
cp.setLayout(new BorderLayout());
cp.add(Toolbar, BorderLayout.NORTH);


/Thomas

0 new messages