Complete source code below will show you, how to set warning icon in JOptionPane.
************************************************************************** COMPLETE SOURCE CODE FOR : SetJOptionPaneWarningIcon.java **************************************************************************
import javax.swing.*;
public class SetJOptionPaneWarningIcon { public static void main(String[]args) { //null-Message Box appear in no parent component //Warning Icon-text in message box //WARNING-JOptionPane title //JOptionPane.WARNING_MESSAGE-Set warning icon that will be show in message box JOptionPane.showMessageDialog(null,"Warning Icon","WARNING",JOptionPane.WARNING_MESSAGE); } }
************************************************************************** JUST COMPILE AND EXECUTE IT **************************************************************************
Complete source code below will show you, how to set error icon in JOptionPane.
************************************************************************** COMPLETE SOURCE CODE FOR : SetJOptionPaneErrorIcon.java **************************************************************************
import javax.swing.*;
public class SetJOptionPaneErrorIcon { public static void main(String[]args) { //null-Message Box appear in no parent component //Error Icon-text in message box //ERROR-JOptionPane title //JOptionPane.ERROR_MESSAGE-Set error icon that will be show in message box JOptionPane.showMessageDialog(null,"Error Icon","ERROR",JOptionPane.ERROR_MESSAGE); } }
************************************************************************** JUST COMPILE AND EXECUTE IT **************************************************************************
Complete source code below will show you, how to set information icon in JOptionPane.
************************************************************************** COMPLETE SOURCE CODE FOR : SetJOptionPaneInformationIcon.java **************************************************************************
import javax.swing.*;
public class SetJOptionPaneInformationIcon { public static void main(String[]args) { //null-Message Box appear in no parent component //Information Icon-text in message box //INFORMATION-JOptionPane title //JOptionPane.INFORMATION_MESSAGE-Set information icon that will be show in message box JOptionPane.showMessageDialog(null,"Information Icon","INFORMATION",JOptionPane.INFORMATION_MESSAGE); } }
************************************************************************** JUST COMPILE AND EXECUTE IT **************************************************************************
Complete source code below will show you, how to set question icon in JOptionPane.
************************************************************************** COMPLETE SOURCE CODE FOR : SetJOptionPaneQuestionIcon.java **************************************************************************
import javax.swing.*;
public class SetJOptionPaneQuestionIcon { public static void main(String[]args) { //null-Message Box appear in no parent component //Question Icon-text in message box //QUESTION-JOptionPane title //JOptionPane.QUESTION_MESSAGE-Set question icon that will be show in message box JOptionPane.showMessageDialog(null,"Question Icon","QUESTION",JOptionPane.QUESTION_MESSAGE); } }
************************************************************************** JUST COMPILE AND EXECUTE IT **************************************************************************
Complete source code below will show you, how to set JOptionPane icons using any GIF image. Before you compile and execute complete source code below, you need to download all images below and place them with your java file that contain source code below.You also can use any GIF image that you want, but make sure it's location is right. This is because i suggest you place all GIF image at same location with source code file. So you don't need to put any file path in your source code. If you still want to use GIF image at other location, you can try step by step below.
For example, your GIF image with name MyImage.gif locate at C:\Documents and Settings...like below :
C:\Documents and Settings\MyImage.gif
So, you must put in your source code like this : "C:\\Documents and Settings\\MyImage.gif"
public class ChangeJOptionPaneIcons implements ActionListener {
JButton button=new JButton("JOptionPane with Error Icon"); JButton button2=new JButton("JOptionPane with Information Icon"); JButton button3=new JButton("JOptionPane with Question Icon"); JButton button4=new JButton("JOptionPane with Warning Icon");
**************************************************************** JUST COMPILE AND EXECUTE IT ****************************************************************
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 *****************************************************************
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 ************************************************************************