Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / September 2005

Tip: Looking for answers? Try searching our database.

Howto: Do menu-style drop down in a JToolBar

Thread view: 
Casper - 23 Sep 2005 11:43 GMT
Many UI's, including Oracle JDeveloper, make use of the menu-style
(rather than combo-style) drop down list in a JToolBar. I have however
been unable to find an example of this. I find that placing a JCombo in
the toolbar looks ugly and inkonsistant with the rest of the toolbar.

What I want to do, is integrate a button with a little arrow pointing
towards south, which when pressed, presents some related options in the
drop down list. E.g. Left, Right and Justified (muturally exclusive, but
that could always be done in programming logic).

Hope someone out there knows how to do this, thanks in advance,
Casper
Vova Reznik - 23 Sep 2005 14:51 GMT
> Many UI's, including Oracle JDeveloper, make use of the menu-style
> (rather than combo-style) drop down list in a JToolBar. I have however
[quoted text clipped - 8 lines]
> Hope someone out there knows how to do this, thanks in advance,
> Casper
What do you think JComboBox is?
It contains a button and the CellRendererPane.
Clicking on button shows drop down menu (JPopupMenu) with items.
Casper - 23 Sep 2005 18:43 GMT
Vova Reznik:
> What do you think JComboBox is?
> It contains a button and the CellRendererPane.
> Clicking on button shows drop down menu (JPopupMenu) with items.

As I already said, if I just add a JComboBox to the toolbar, it just
shows a normal ComboBox and NOT a menu-style drop down.
Vova Reznik - 23 Sep 2005 19:10 GMT
> Vova Reznik:
>
[quoted text clipped - 4 lines]
> As I already said, if I just add a JComboBox to the toolbar, it just
> shows a normal ComboBox and NOT a menu-style drop down.

As i already said - put button on a toolbar and show your own JPopupMenu
    popup.show(toolBar, btn.getX(), btn.getY() + btn.getHeight());

Size of JPopuMenu depends on preferred size of inside component.
Casper - 24 Sep 2005 03:37 GMT
Vova Reznik skrev:

>> Vova Reznik:
>>
[quoted text clipped - 9 lines]
>
> Size of JPopuMenu depends on preferred size of inside component.

It still does not look like a real toolbar drop-down, I am missing the
little "expand" icon. Also, this arrow toolbar button is typically
pressed down while the menu is shown. I just though there would be a
standard way for this.

Thanks though,
Casper
Vova Reznik - 24 Sep 2005 14:55 GMT
> It still does not look like a real toolbar drop-down, I am missing the
> little "expand" icon. Also, this arrow toolbar button is typically
Little "expand" icon is a button.
> pressed down while the menu is shown. I just though there would be a
> standard way for this.
Everything is possible and not everywhere are standard ways.
Casper - 24 Sep 2005 17:17 GMT
Vova Reznik skrev:

>> It still does not look like a real toolbar drop-down, I am missing the
>> little "expand" icon. Also, this arrow toolbar button is typically
[quoted text clipped - 5 lines]
>
> Everything is possible and not everywhere are standard ways.

No but had there been a standard way, it would be pretty dumb to put
together a hack. Hence the question to this newsgroup.

Casper
Kari Ikonen - 25 Sep 2005 20:46 GMT
> It still does not look like a real toolbar drop-down, I am missing the  
> little "expand" icon. Also, this arrow toolbar button is typically  
> pressed down while the menu is shown. I just though there would be a  
> standard way for this.

Just define appropriate "arrow-down" icon and attach it into Action  
-button whichyou place into toolbar. That works easily if you want to have  
"[foobar-text v]" -style toolbar menubutton. However, I'm guessing you are  
wanting to have "[[some-icon]v]".

Such can be done approximately like this:

Ingredients are:
1) One standard toolbar A ImageIcon (assuming 24x24)
2) One dropdown arrow B ImageIcon (8x24)
3)
public Icon merge(Icon pA, Icon pB) {
   final Image a = ((ImageIcon)pA).getImage();
   final Image b = ((ImageIcon)pB).getImage();
   final int w1 = pA.getIconWidth();
   final int w2 = pB.getIconWidth();
   final int h = pA.getIconHeight() + pB.getIconHeight();
   final BufferedImage image = new BufferedImage(w1+w2, h);
   Graphics2D g2d = image.createGraphics();
   g2d.drawImage(0, 0, a);
   g2d.drawImage(w1, 0, b);
   return new ImageIcon(image);
}

To be honest, you actually need to also have special ToolBarMenuButton, to  
manage menu show/hide logic properly. I.e. some additional logic is needed  
to make it behave correctly (= in similar manner than menubar menus).

For example,
- Managing keyboard
- Cancelling menu (escape, mouseclick)
- Opening another menu while toolbar menubutton's menu is still open

Signature

KI

Casper - 26 Sep 2005 12:18 GMT
Kari Ikonen:
> Ingredients are:
> 1) One standard toolbar A ImageIcon (assuming 24x24)
[quoted text clipped - 12 lines]
>    return new ImageIcon(image);
> }

Thanks for the idea. However doing this still does not mimic other
implementations very well, particulary the dual border in respect to
rollover/pressdown events.

I am implementing my solution as a JMenuComboButton which subclasses
JToolBar and has a button for the action itself and one for the
drop-down triggering. I then subclass these JButton's and add my own logic.

Casper


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.