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

Tip: Looking for answers? Try searching our database.

rollover effect on a button?

Thread view: 
Sandro - 14 Jan 2005 21:43 GMT
Hi,

I want a border appears when I roll over with the mouse over a button.
And when I quit the button area the border should be disappeared. At
first I resolved the problem by setting a LineBorder in the
mouseEntered method and setting an EmptyBorder in the mouseExited
method. However the border is drawed around the hole button. But I
want only the icon at the button surrounded by a border. Therefore I
tried to resolve it in another way which is show in the following
class myButton:

[CODE]
public class myButton extends JButton {
 
 private boolean isOver = false;
 int iconHeight, iconWidth;
 

 public myButton(String s, ImageIcon icon) {
   super(s, icon);
   iconWidth = icon.getIconWidth();
   iconHeight = icon.getIconHeight();
   
   setBorder(BorderFactory.createLineBorder(Color.blue));
   setFocusPainted(false);    
   setRolloverEnabled(true);    
   
   addMouseListener(new MouseAdapter() {
     
     public void mouseEntered(MouseEvent e) {
       isOver = true;
       repaint();
     }
     
     public void mouseExited(MouseEvent e) {
       isOver = false;
       repaint();
     }
   });
 
 }
 
 public void paint(Graphics g) {
   super.paint(g);
   
   if(isOver) {
     
     g.setColor(Color.blue);
     g.drawRect(    getX() + getInsets().left,
            getY() + getInsets().top,
            iconWidth,
            iconHeight);
       
   }
   
 }
}
[/CODE]

Much to my surprise I found that only the first used instance of the
class myButton shows the implemented behaviour. All afterwards
instanced objects of myButton seem to be without the new extended
paint method. But why? I would be very grateful if you could test the
myButton class and post your experience.

Thx
Andrew Thompson - 14 Jan 2005 22:56 GMT
> I want a border appears...

..on any number of Usenet new groups?

See the answer you already got on the other group.
<http://www.physci.org/codes/javafaq.jsp#xpost>

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

Andrew McDonagh - 14 Jan 2005 23:15 GMT
> Hi,
>
[quoted text clipped - 62 lines]
>
> Thx

Try just setting the border, it'll repaint when/if it needs to itself.

final JButton button = new JButton("rollover me");
final Border normalBorder = button.getBorder();

button.addMouseListener(new MouseAdapter() {

   public void mouseEntered(MouseEvent e) {
       button.setBorder(BorderFactory.createEmptyBorder());
   }

   public void mouseExited(MouseEvent e) {
       button.setBorder(normalBorder);
   }
});
Andrew McDonagh - 14 Jan 2005 23:19 GMT
> Hi,
>
[quoted text clipped - 62 lines]
>
> Thx

Actually here's a fully working test app, this time with the border
appearing when the mouse enters, rather than leaves ;-)

package com.test;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.Border;

public class RolloverButtonTest {

   public static void main(String[] args) {
      JFrame frame = new JFrame("Test");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      Container contentPane = frame.getContentPane();
      contentPane.setLayout(new FlowLayout());

      final JButton button = new JButton("rollover me");
      final Border normalBorder = button.getBorder();

      button.addMouseListener(new MouseAdapter() {

         public void mouseEntered(MouseEvent e) {
            button.setBorder(normalBorder);
         }

         public void mouseExited(MouseEvent e) {
            button.setBorder(BorderFactory.createEmptyBorder());
         }
      });

      contentPane.add(button);

      frame.pack();
      frame.setVisible(true);

   }

}


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.