Set JButton pressed icon

Complete source code below will show you, how to set JButton pressed icon. When someone click the button, icon on that button we will change to pressed icon that we set for the button. You can download icon that we use in this tutorial at below.When you want to compile and execute it, please make sure, icon file locate at same location with this java file.

Note : If you want to create pressed icon, i suggest you make it size and shape same with non-pressed icon.

*****************************************************************
COMPLETE SOURCE CODE FOR : SetJButtonPressedIcon.java
*****************************************************************


import javax.swing.*;

public class SetJButtonPressedIcon
{
public static void main(String[]args)
{
//Create image icon from "notpressedicon.png"
//You can download it at below
//If your icon locate at other location,
//please put it's full address location.For example :
//C:\\Documents and Settings\\evergreen\\Desktop\\notpressedicon.png
ImageIcon notPressIcon=new ImageIcon("notpressedicon.png");

//Create image icon from "pressedicon.png"
//You can download it at below
//If your icon locate at other location,
//please put it's full address location.For example :
//C:\\Documents and Settings\\evergreen\\Desktop\\pressedicon.png
ImageIcon pressIcon=new ImageIcon("pressedicon.png");

//Create a button with text ( GO ) and not press icon ( notpressedicon.png )
JButton button=new JButton("GO",notPressIcon);

//Set presses icon for JButton
button.setPressedIcon(pressIcon);

//Set horizontal text position to center
button.setHorizontalTextPosition(SwingConstants.CENTER);

//Set vertical text position to bottom.
//So, text will locate under icon
button.setVerticalTextPosition(SwingConstants.BOTTOM);

//Create a window using JFrame with title ( Set JButton pressed icon )
JFrame frame=new JFrame("Set JButton pressed icon");

//Add created button into JFrame
frame.add(button);

//Set JFrame's default close operation
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size
frame.setSize(350,150);

//Make JFrame center on screen
frame.setLocationRelativeTo(null);

//Make JFrame visible
frame.setVisible(true);
}
}


*****************************************************************
JUST COMPILE AND EXECUTE IT
*****************************************************************


pressedicon.png




notpressedicon.png

RELAXING NATURE VIDEO