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 / July 2005

Tip: Looking for answers? Try searching our database.

Internet Explorer/Firefox, Back or Forward Button.

Thread view: 
Xu - 13 Jul 2005 13:31 GMT
Hi,

I am trying to make a button that looks like the buttons you see in IE
or FF. (The Back and forward buttons).
My first try is a JButton with a borderlayout, center i put a label en
in the east i've put a jcombobox. (dont know if this is the right thing
to do, putting a jcombobox in a jbutton...)
The problem that i'm facing now is, when i set a size/prefered size to
the combobox, the dropdown list is also effected. Can someone suggest
how i can handle this? i've already posted this question in
c.l.j.programmer, but didnt got many replies. Seems that
c.l.j.programmers can't hack out a GUI even if their life are depending
on it. :P

oh btw, does anyone know how this widget is called? (that would help
with googling)

Thanks

--
Xu
Lou Lipnickey - 13 Jul 2005 14:09 GMT
try sizing the jcombobox also. - Lou

> Hi,
>
[quoted text clipped - 14 lines]
>
> Thanks
Thomas Weidenfeller - 13 Jul 2005 14:33 GMT
> I am trying to make a button that looks like the buttons you see in IE
> or FF. (The Back and forward buttons).
> My first try is a JButton with a borderlayout, center i put a label en
> in the east i've put a jcombobox. (dont know if this is the right thing
> to do, putting a jcombobox in a jbutton...)

No, it is not. It sounds like you want a drop-down menu from a button.
Ironically, such a beast is defined in Sun' Java Look and Feel Design
Guidelines

  http://java.sun.com/products/jlf/ed2/book/HIG.Menus4.html#44052

but it is not implemented in Swing. You have to hack it together by
using a normal button, a button graphics with a menu "badge"

  http://java.sun.com/products/jlf/ed2/book/HIG.Graphics5.html#73823

a JPopupMenu (yes, a JPopupMenu can work as a drop-down menu) and a
mouse event listener.

The wiring logic (click behavior) will probably deviate from the one
specified in the JLF Design Guidelines so to make sense (the one in the
guide is too simple).

> Seems that
> c.l.j.programmers can't hack out a GUI even if their life are depending
> on it. :P

They can, but why should they? No one here and no one there owes you
anything, and it is probably not a good idea to alienate and annoy
people you are asking a favour. Oh, and it is first of all you who
failed to hack that particular GUI feature, isn't it? Otherwise you
wouldn't have to ask.

/Thomas

Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/

Xu - 13 Jul 2005 14:55 GMT
>> Seems that c.l.j.programmers can't hack out a GUI even if their life
>> are depending on it. :P
[quoted text clipped - 6 lines]
>
> /Thomas

Sorry... i probably should have put the original source of my double
post. And not use it without any reference. Now it seems that i am
trying to provoke people.. that isnt and wasnt my purpose of posting a
question in here.

Andrew Thompson wrote:

>> Andrew Thompson wrote:
>>
>>> On Wed, 13 Jul 2005 09:41:35 +0200, Xu wrote:
>>>
>>>> Hi i am wondering if there is a simple way to reduce a jcombobox
visual size

> ..
>
>>> Sounds more like a Menu you are after.  Or at least that
>>> is what the end user would probably prefer to a drop-down list that
looks or behaves in a way that is 'unusual'..

>> ..you can find them in firefox or in internet explorer.(Back and
Forward Button's).

> Aha!  Good point.  Yes.. I not only see the type
> of control you mean, but am so used to using it
> I barely notice it.  This pretty much blows my theory of 'unusual'
out of the water!

> I'll wait to see what the GUI experts here on..
> Wait a second..  You won't find the GUI experts here!
[quoted text clipped - 7 lines]
>
> HTH

Ah ok.. tnx for your advise :)
Andrew Thompson - 13 Jul 2005 15:40 GMT
>>> Seems that c.l.j.programmers can't hack out a GUI even if their life
>>> are depending on it. :P
..
>> They can, but why should they? No one here and no one there owes you
>> anything, and it is probably not a good idea to alienate and annoy
>> people you are asking a favour. Oh, and it is first of all you who
>> failed to hack that particular GUI feature, isn't it? Otherwise you
>> wouldn't have to ask.
..
> Sorry... i probably should have put the original source of my double
> post. And not use it without any reference.

> Andrew Thompson wrote: (from a thread on c.l.j.p.)

>  > These c.l.j.programmers could not hack out a GUI if
>  > their life depended on it.  [  ;-)   ]

Yep.. guilty.  (silly grin)  

Sorry if it offended you Thomas, I did not mean it seriously
(well ..not _that_ seriously - *some* of the suggestions for
GUI's I see on c.l.j.p. make my skin crawl!).

Signature

Andrew Thompson
physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
In Hypno-Vision

Tom N - 13 Jul 2005 15:07 GMT
> I am trying to make a button that looks like the buttons you see in IE
> or FF. (The Back and forward buttons).
[quoted text clipped - 10 lines]
> oh btw, does anyone know how this widget is called? (that would help
> with googling)

I'd try a JPopupMenu which is shown when the user clicks on the correct area of the button.
See http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html#popup

I've so far got one which appears on a right-click (but the button action also gets called).
Tom N - 13 Jul 2005 15:20 GMT
>> I am trying to make a button that looks like the buttons you see in
>> IE or FF. (The Back and forward buttons).
[quoted text clipped - 10 lines]
>> oh btw, does anyone know how this widget is called? (that would help
>> with googling)

Mozilla docs call it a Dual Menu or a menu-button (neither of which is terribly clear.
http://docs.mandragor.org/files/Misc/Mozilla_applications_en/mozilla-chp-3-sect-
6.html


> I'd try a JPopupMenu which is shown when the user clicks on the
> correct area of the button. See
> http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html#po
> pup

More hints...

There is a concept on a frame called a "glass pane" which is by default clear and over the top of all
components in the JFrame.  You can draw on it but more usefully in this case, you can add event handlers to it
so you get the mouse clicks etc before the underlying component that the user clicked on.

You could put a mouse listener on the glass pane and if the click is on the back button in the right place for
popping up the history menu then do that, otherwise redispatch the mouse event to the appropriate widget.

See http://java.sun.com/docs/books/tutorial/uiswing/components/rootpane.html
Xu - 13 Jul 2005 17:15 GMT
>>>I am trying to make a button that looks like the buttons you see in
>>>IE or FF. (The Back and forward buttons).
[quoted text clipped - 29 lines]
>
> See http://java.sun.com/docs/books/tutorial/uiswing/components/rootpane.html
tnx for the hints.

i got now two buttons, one button is just for a single action and a
second button to show a jpopupmenu beneath the first button.
i only need to put them/stitch them together so they look like one
button. i'm also looking in the abstractbutton source now to see where
the mouseover effect is of a button.. i need to disabled that :S, its
giving a ugle border effect, especially if they need to be seen as one.
Tom N - 14 Jul 2005 03:40 GMT
>> I'd try a JPopupMenu which is shown when the user clicks on the
>> correct area of the button. See
[quoted text clipped - 16 lines]
> See
> http://java.sun.com/docs/books/tutorial/uiswing/components/rootpane.html

Here's one that works - an interesting exercise.

It uses the glass pane to intercept events.  When you click button1 (left mouse button) on the right
hand 25% of the history button, it intercepts the mouse pressed event and pops up a menu.  When
you release button1 in the same place, it discards the event.  When you do the invoke-popup-menu
mouse action (button3 on Windows) anywhere over the history button, it invokes the pop up menu.  
This is pretty much the same as an Internet Explorer back or forward button.  Any other mouse
events are passed to the underlying component.

I put my history button in a JToolBar.  To get a down arrow on the right hand side, I just screen-
dumped IE and put the image from the Back button on the JButton using a .png.

In your code to initialise your frame (e.g. JFrame constructor), do this:

     getGlassPane().addMouseListener(
         new HistoryButtonMouseAdapter(getContentPane(),
                                       popupMenu,
                                       getGlassPane(),
                                       backButton,
                                       menuBar));
     getGlassPane().setVisible(true);

class HistoryButtonMouseAdapter extends MouseInputAdapter
{
 // Copyright (C) Tom N 2005
 // Permission granted to use without restriction, provided these
 // 3 lines stay in the source code.
 private Container contentPane;
 private JPopupMenu popupMenu;
 private Component glassPane;
 private JButton historyButton;
 private JMenuBar menuBar;
 /**
  * Handle a history button.  Typical calls is as follows (from within
  * a JFrame constructor).
  *       getGlassPane().addMouseListener(
  *           new HistoryButtonMouseAdapter(getContentPane(),
  *                                         popupMenu,
  *                                         getGlassPane(),
  *                                         backButton,
  *                                         menuBar));
  *       getGlassPane().setVisible(true);
  *
  * @param contentPane result of getContentPane on the frame
  * @param popupMenu the history menuu to pop up.
  * @param glassPane result of getGlassPane on the frame
  * @param historyButton the button we want history facility on
  * @param menuBar menu bar or null if there is none.
  */
 HistoryButtonMouseAdapter(Container contentPane, JPopupMenu popupMenu,
                           Component glassPane, JButton historyButton,
                           JMenuBar menuBar)
 {
   this.contentPane = contentPane;
   this.popupMenu = popupMenu;
   this.glassPane = glassPane;
   this.historyButton = historyButton;
   this.menuBar = menuBar;
 }
 public void mouseClicked(MouseEvent e)
 {
   redispatchMouseEvent(e);
 }
 public void mouseEntered(MouseEvent e)
 {
   redispatchMouseEvent(e);
 }
 public void mouseExited(MouseEvent e)
 {
   redispatchMouseEvent(e);
 }
 public void mousePressed(MouseEvent e)
 {
   redispatchMouseEvent(e);
 }
 public void mouseReleased(MouseEvent e)
 {
   redispatchMouseEvent(e);
 }
 private void showPopup(MouseEvent e)
 {
   popupMenu.show(e.getComponent(),
                    e.getX(), e.getY());
 }
 private void redispatchMouseEvent(MouseEvent e)
 {
   Point glassPanePoint = e.getPoint();
   Point containerPoint = SwingUtilities.convertPoint(
                                   glassPane,
                                   glassPanePoint,
                                   contentPane);
   boolean handled = false;
   if (containerPoint.y < 0)
   { // We're not in the content pane so we're probably in the menu bar
     // so we have to redispatch the event.
     if (menuBar != null && containerPoint.y + menuBar.getHeight() >= 0)
     {
         //The mouse event is over the menu bar.
         containerPoint = SwingUtilities.convertPoint(
           menuBar,
           glassPanePoint,
           menuBar);
         if (containerPoint.y >= 0)
         {
           Component component =
               SwingUtilities.getDeepestComponentAt(menuBar,
               containerPoint.x,
               containerPoint.y);
           Point componentPoint = SwingUtilities.convertPoint(
               menuBar,
               glassPanePoint,
               component);
           component.dispatchEvent(new MouseEvent(component,
               e.getID(),
               e.getWhen(),
               e.getModifiers(),
               componentPoint.x,
               componentPoint.y,
               e.getClickCount(),
               e.isPopupTrigger()));
       }
     }
   }
   else
   { // The mouse event is possibly over thing we are interested in.
     // Find out exactly which component it's over.
     Component component =
         SwingUtilities.getDeepestComponentAt(
         contentPane,
         containerPoint.x,
         containerPoint.y);
     Point componentPoint = SwingUtilities.convertPoint(
         glassPane,
         glassPanePoint,
         component);
     if (component == historyButton)
     {
       // Check events over backbutton
       if (e.isPopupTrigger())
       {
         // Popup menu button (varies with platform) operates anywhere
         // on history button.
         handled = true;
         showPopup(e);
       }
       else if (e.getButton() == e.BUTTON1)
       {
         // Look for right hand 25% of button being clicked on.
         int minX = (historyButton.getWidth() >> 2) +
                    (historyButton.getWidth() >> 1);
         if (componentPoint.x > minX)
         {
           if (e.getID() == e.MOUSE_PRESSED)
           {
             handled = true;
             showPopup(e);
           }
           else if (e.getID() == e.MOUSE_RELEASED)
           {
             handled = true;
           }
         }
       }
     }
     if (! handled)
     {
       //Forward events (note menu bar events forwarded above).
       component.dispatchEvent(new MouseEvent(component,
                                            e.getID(),
                                            e.getWhen(),
                                            e.getModifiers(),
                                            componentPoint.x,
                                            componentPoint.y,
                                            e.getClickCount(),
                                            e.isPopupTrigger()));
     }
   }
 }
}
Not Xu - 15 Jul 2005 04:09 GMT
> Here's one that works

<sarcasm>
Wow Tom N, that's fantastic.  Thanks.
</sarcasm>
Xu - 15 Jul 2005 14:12 GMT
>>Here's one that works
>
> <sarcasm>
> Wow Tom N, that's fantastic.  Thanks.
> </sarcasm>
Sorry i don't work like that
Tom N - 15 Jul 2005 15:15 GMT
>>>Here's one that works
>>
>> <sarcasm>
>> Wow Tom N, that's fantastic.  Thanks.
>> </sarcasm>
> Sorry i don't work like that

What does that mean?

At least I know you read it even if I don't know whether it was useful to you.
Xu - 15 Jul 2005 15:58 GMT
>>>>Here's one that works
>>>
[quoted text clipped - 8 lines]
> At least I know you read it even if I don't know whether it was useful to you.
:P like to do my own solution first :) be tnx very much for your help
Xu - 15 Jul 2005 16:13 GMT
>>>>Here's one that works
>>>
[quoted text clipped - 8 lines]
> At least I know you read it even if I don't know whether it was useful to you.
:S did u post under the sender name: Not Xu ...
I thought it was a flame o.0
Thorsten - 15 Jul 2005 21:32 GMT
We published a JPopupMenuButton widget on our website, which is exactly
what you described. And there will be a small history framework based
on that widget in the next week.

It's under LGPL.

http://flexo.cismet.de/gadgets/

Hope that will help you.

Have fun :-)

Thorsten
Tom N - 19 Jul 2005 04:30 GMT
> We published a JPopupMenuButton widget on our website, which is exactly
> what you described. And there will be a small history framework based
[quoted text clipped - 5 lines]
>
> Hope that will help you.

Nice work.

A minor comment.

You refer to "e.getButton()==MouseEvent.BUTTON3"
Might be better to use "e.isPopupTrigger()" since that is platform independent.
Thorsten - 20 Jul 2005 14:54 GMT
The small history framework is ready now. It uses the JPopupMenuButton
described in this thread.

Available under LGPL on:

http://flexo.cismet.de/gadgets/JHistory

Have fun :-)

Thorsten


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.