1

I want create custom button with image throught JLabel If I want make new "button" I have to put this code in every class

{
protected static JLabel B;


@Override
public void mouseEntered(MouseEvent e) 
{
    if(e.getSource()==B)
    {
        ImageIcon testI = new ImageIcon(R.exitP1);
        B.setIcon(testI);
    }else
    {
        ImageIcon testI = new ImageIcon(R.exitP0);
        B.setIcon(testI);
    }
}

@Override
public void mouseReleased(MouseEvent e) 
{
    if(e.getSource()==B)
    {
        ImageIcon testI = new ImageIcon(R.exitP0);
        B.setIcon(testI);
    }
}
public void mousePressed(MouseEvent e) 
{

    if(e.getSource()==B)
    {
        ImageIcon testI = new ImageIcon(R.exitP2);
        B.setIcon(testI);
    }
}

@Override
public void mouseClicked(MouseEvent e) 
{

}

@Override
public void mouseExited(MouseEvent e) 
{

}

@Override
public void mouseDragged(MouseEvent arg0) 
{

}

@Override
public void mouseMoved(MouseEvent arg0) 
{

}}

R.exitP0 , R.exitP1 , R.exitP0 are button's image

R.exitP0 - non-active state

R.exitP1 - mouse is on the "button"

R.exitP3 - mouse is clicking on "button" So... is possible to do this ? Or I have to paste this code in every class where I want have this "button" ?

  • Any reason why you wouldn't just use a `JButton` which has support for this (roll over, pressed icon, etc)? – MadProgrammer Mar 06 '16 at 09:42
  • Wait.... `JButton` support pressed icons ? Ouh.... I... I didn't know that... – Mirek Jozefek Mar 06 '16 at 09:47
  • 1
    [`JButton#setPressedIcon`](https://docs.oracle.com/javase/8/docs/api/javax/swing/AbstractButton.html#setPressedIcon-javax.swing.Icon-) – MadProgrammer Mar 06 '16 at 09:50
  • Yea, but after adding images to button and making it invisible button is still too big, I want it 20x20 in `FlowLayout`, how can I do this ? – Mirek Jozefek Mar 06 '16 at 10:01
  • That would come down to; 1- The size of the icons; 2- if you want to paint the border/content; 3- What margins you've supplied to the button – MadProgrammer Mar 06 '16 at 10:40
  • I hope i understand because I'am from Czech republic :D So... 1 - the icon size is 20x20 and I want the button's size 20x20. 2 - I don't want paint border ( if it's the white thing arround icon ) 3 - I can't translate it... – Mirek Jozefek Mar 06 '16 at 10:54
  • Take a look at `setBorderPainted` and `setContentAreaFilled`, start by setting those to `false` – MadProgrammer Mar 06 '16 at 10:55
  • I have it, but the white border on button is still there, I want vanish it. ( I don't have mouse on it and I'am not clicking on it ) – Mirek Jozefek Mar 06 '16 at 11:16
  • See [this post](http://stackoverflow.com/a/10862262/418556) for tips on removing the stray space from the button. – Andrew Thompson Mar 06 '16 at 11:20
  • Yes, thak you, that completly want I wanted, and last thing: resize button in `FlowLayout` I want it 20x20 is this possible, or I have to set it to `AbsoluteLayout` ? – Mirek Jozefek Mar 06 '16 at 11:20
  • The only three primary things which affect the size of a button is, the text, the icon and the margins (now assuming you've removed the border and content fill) – MadProgrammer Mar 06 '16 at 20:15

0 Answers0