4 Replies - 457 Views - Last Post: 28 June 2012 - 08:14 PM Rate Topic: -----

#1 uzzi7862  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 28-February 12

custom java button with hovering effect

Posted 27 June 2012 - 04:20 PM

Hello. I am having a problem finding my problem i am trying to create a hovering effect on my button which i have created from a image.could any one tell me what i have to do to fix the error.it does not change when it is hovered over

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

public class mainPanel extends JButton {
    public  Image borderImage=new ImageIcon(this.getClass().getResource("001.png")).getImage();
 JFrame f= new JFrame();
 JPanel p = new JPanel();   
  JButton b1 = new JButton("asdf");
  JTextField tf = new JTextField(15);
 public mainPanel(){
     JButton button = new JButton(new CloseIcon());     
          button.setContentAreaFilled(false);
        button.setFocusPainted(false);
        button.setBorder(BorderFactory.createEmptyBorder());
        button.setOpaque(false);//enable this to create a button border 
  
       
       button.addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent e){
               System.exit(0);
           }
       });
        f.setSize(500,500);
       p.add(button);
       p.add(tf);
        f.add(p);
        f.setVisible(true);   
    }
}
class CloseIcon implements Icon,MouseListener {
    public int width;
    public int height;
  boolean hovered;
     public Image img1=new ImageIcon(this.getClass().getResource("button1.png")).getImage();
 public Image img2=new ImageIcon(this.getClass().getResource("button2.png")).getImage();
    public CloseIcon() {
        width  = img1.getWidth(null);
        height = img1.getHeight(null);
    
    }
   
    @Override public void paintIcon(Component c, Graphics g, int x, int y) {
        if(hovered=true){
g.drawImage(img1, 0, 0, null);            
        }else if(hovered=false){
g.drawImage(img2, 0, 0, null);            
            
        }
        
       
          
      }
    
    @Override public int getIconWidth() {
        return width;
    }
    @Override public int getIconHeight() {
        return height;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
 
  hovered=true;
    }

    @Override
    public void mouseExited(MouseEvent e) {
  
    hovered=false;
    }
   
     
}



Is This A Good Question/Topic? 0
  • +

Replies To: custom java button with hovering effect

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2115
  • View blog
  • Posts: 8,813
  • Joined: 20-September 08

Re: custom java button with hovering effect

Posted 27 June 2012 - 04:36 PM

Why not use the following?

http://docs.oracle.c...vax.swing.Icon)
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8030
  • View blog
  • Posts: 31,177
  • Joined: 06-March 08

Re: custom java button with hovering effect

Posted 27 June 2012 - 06:05 PM

If your class mainPanel extends JButton (honestly you could have found a better name than mainPanel for a class that extends JButton like MyButton as example) why do you create a JButton inside it ?
It is already a JButton so should be:

setContentAreaFilled(false);
button.setFocusPainted(false);
button.setBorder(BorderFactory.createEmptyBorder());

p.add(this);
...
Was This Post Helpful? 0
  • +
  • -

#4 uzzi7862  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 28-February 12

Re: custom java button with hovering effect

Posted 28 June 2012 - 04:14 AM

View Postpbl, on 27 June 2012 - 06:05 PM, said:

If your class mainPanel extends JButton (honestly you could have found a better name than mainPanel for a class that extends JButton like MyButton as example) why do you create a JButton inside it ?
It is already a JButton so should be:

setContentAreaFilled(false);
button.setFocusPainted(false);
button.setBorder(BorderFactory.createEmptyBorder());

p.add(this);
...

i am doing the extend of the button because i want to use an image as a button.and i want to create a hovering effectwith the mouse
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8030
  • View blog
  • Posts: 31,177
  • Joined: 06-March 08

Re: custom java button with hovering effect

Posted 28 June 2012 - 08:14 PM

Does not explain: why you create a JButton inside your class that is already a JButton

Also, JButton are put inside JPanel or JFrame. Why a class that extends JButton would create a JFrame ?

I would understand that a JFrame would contain different different JPanel that would contain one or multiple mainPanel (aka JButton).
There is one JFrame per application, if all the JPanel contained in the master JFrame contain a total 20 mainPanel(JButton) you will end up with 20 JFrame and 20 JPanel in the 20 mainPanel... just does not make sense

Something like:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class mainPanel extends JButton {
    public  Image borderImage=new ImageIcon(this.getClass().getResource("001.png")).getImage();

 public mainPanel(){
     super(new CloseIcon());     
     setContentAreaFilled(false);
     setFocusPainted(false);
     setBorder(BorderFactory.createEmptyBorder());
     setOpaque(false);//enable this to create a button border 
  
       
     addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent e){
               System.exit(0);
           }
       });
    }
}



would make sense
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1