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

Set JButton icon

Complete source code below will show you, how to set icon on JButton.

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


import javax.swing.*;

public class SetJButtonIcon
{
public static void main(String[]args)
{
//Create an image icon from png file named ( callme.png )
//You can download this image below
//This file is locate at same location with this java file.
//If your file locate at other location,
//you must put it's full location address. For example :
//C:\\Documents and Settings\\evergreen\\Desktop\\callme.png
ImageIcon iconForButton=new ImageIcon("callme.png");

//Create a button from JButton with text ( Call me ) and image icon ( callme.png )
JButton button=new JButton("Call me",iconForButton);

//set button horizontal text position to center
//You can select one of the following SwingConstants:
//SwingConstants.CENTER
//SwingConstants.RIGHT
//SwingConstants.LEFT
//SwingConstants.LEADING
//SwingConstants.TRAILING (the default)
button.setHorizontalTextPosition(SwingConstants.CENTER);

//set vertical text position to bottom
//It make our text button locate under button icon
//You can select one of the following SwingConstants:
//SwingConstants.CENTER (the default)
//SwingConstants.TOP
//SwingConstants.BOTTOM
button.setVerticalTextPosition(SwingConstants.BOTTOM);

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

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

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

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

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

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


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



callme.png

RELAXING NATURE VIDEO