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 / April 2004

Tip: Looking for answers? Try searching our database.

Making a button behave as in a toolbar

Thread view: 
Soeren - 30 Apr 2004 14:33 GMT
I have some buttons in a dialog. I would like if they behaved as the buttons
in a toolbar, so only the icon is showed but if the mouse is located in the
button area, the frame is showed as well.

Best regards
Soeren
Andrew Thompson - 30 Apr 2004 14:53 GMT
> I have some buttons in a dialog. I would like if they behaved as the buttons
> in a toolbar, so only the icon is showed but if the mouse is located in the
> button area, the frame is showed as well.

Are you referring to a JToolBar?  

I do not recognise the behaviour you
describe and am not sure what you mean.

When you say 'frame', are you referring
to the border of the dialog itself?

Signature

Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology

Soeren - 30 Apr 2004 15:12 GMT
Hi Andrew, Thanks for your answer

I have only viewed the behaviour as a user. If you take for instance
NetBeans, the buttons in the toolbar have no border (I guess that is a more
correct term for buttons, sorry).

Once you move the mouse over the button, the border appears. When you move
the mouse to an area outside the button, the border disappears.

That is what I am looking for.

Thank you in advance.

Best regards
Soeren

> > I have some buttons in a dialog. I would like if they behaved as the buttons
> > in a toolbar, so only the icon is showed but if the mouse is located in the
[quoted text clipped - 7 lines]
> When you say 'frame', are you referring
> to the border of the dialog itself?
Andrew Thompson - 30 Apr 2004 15:37 GMT
> Once you move the mouse over the button, the border appears. When you move
> the mouse to an area outside the button, the border disappears.

Aha!  You want a button border on mouseover..

There are several ways you can do this,
but I will hang off giving any because
I am almost sure there is a 'best' way of
doing it, ..and I do not know what that is!   ;-)

Hopefully one of the Guru's will be
along any moment.

Signature

Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology

Soeren - 30 Apr 2004 15:52 GMT
YES :-)

Thanks again

I took your very precise words and search google groups, and I found
following code that I will try out.:

JButton button = new JButton(someText, someIcon);
ButtonHighlighter highlighter = new
ButtonHighlighter(button.getBackground());

button.setRolloverEnabled(true);
button.setBorderPainted(false);
button.addMouseListener(highlighter);

-------------------------------------------

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

Color normalColor;
Color brightColor;

public class ButtonHighlighter extends MouseAdapter
{
 public ButtonHighlighter(Color normalColor)
 {
    this.normalColor = normalColor;
    this.brightColor = normalColor.brighter();
 }

 public void mouseEntered(MouseEvent e)
 {
   ((AbstractButton)e.getSource()).setBackground(brightColor);
   ((AbstractButton)e.getSource()).setForeground(Color.red);
   ((AbstractButton)e.getSource()).setBorderPainted(true);
 }

 public void mouseExited(MouseEvent e)
 {
   ((AbstractButton)e.getSource()).setBackground(normalColor);
   ((AbstractButton)e.getSource()).setForeground(Color.black);
   ((AbstractButton)e.getSource()).setBorderPainted(false);
 }

> > Once you move the mouse over the button, the border appears. When you move
> > the mouse to an area outside the button, the border disappears.
[quoted text clipped - 8 lines]
> Hopefully one of the Guru's will be
> along any moment.
Andrew Thompson - 30 Apr 2004 15:59 GMT
> I took your very precise words and search google groups,

A poster that knows how to Google.  
Music to my ears.   :-)

> ..and I found
> following code that I will try out.:

Excellent!  Give us a hail if you
have any problems with it.

Signature

Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology

ak - 30 Apr 2004 21:40 GMT
> JButton button = new JButton(someText, someIcon);
> ButtonHighlighter highlighter = new
[quoted text clipped - 3 lines]
> button.setBorderPainted(false);
> button.addMouseListener(highlighter);

you can do also following:

1. Button#setRollover(true);
2.Button#setBorder(new
javax.swing.plaf.metal.MetalBorders.RolloverButtonBorder());

--

____________

http://reader.imagero.com the best java image reader.


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.