rollover effect on a button?

Discussion in 'Java' started by Sandro, Jan 14, 2005.

  1. Sandro

    Sandro Guest

    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);
    
    }
    
    }
    }
    
    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
    Sandro, Jan 14, 2005
    #1
    1. Advertising

  2. Sandro

    Bjorn Abelli Guest

    "Sandro" wrote this to c.l.j.h. as well...

    Please don't multi-post.

    You can read my answer to your post in comp.lang.java.help.


    // Bjorn A
    Bjorn Abelli, Jan 14, 2005
    #2
    1. Advertising

  3. On Fri, 14 Jan 2005 23:32:30 +0100, Bjorn Abelli wrote:

    > "Sandro" wrote this to c.l.j.h. as well...
    >
    > Please don't multi-post.
    >
    > You can read my answer to your post in comp.lang.java.help.


    Or my response in c.l.j.gui referring the OP back to it, or..??

    I hope, Sandro, that you now understand why people dislike
    multi-posting so much.

    --
    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 Thompson, Jan 14, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. groves

    rollover effect

    groves, Aug 26, 2006, in forum: Python
    Replies:
    13
    Views:
    552
    Diez B. Roggisch
    Aug 27, 2006
  2. Sorin Schwimmer

    rollover effect

    Sorin Schwimmer, Aug 28, 2006, in forum: Python
    Replies:
    0
    Views:
    285
    Sorin Schwimmer
    Aug 28, 2006
  3. Els
    Replies:
    2
    Views:
    983
    Tom Miller
    Apr 1, 2007
  4. Jeff
    Replies:
    1
    Views:
    379
    Luis Roquette Valdez
    Mar 21, 2009
  5. Replies:
    0
    Views:
    168
Loading...

Share This Page