Set JOptionPane icons



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"


WARNING.gif



QUESTION.gif



INFORMATION.gif



ERROR.gif


Click here for how to create all icons above

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


import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.LookAndFeel;

import java.awt.GridLayout;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

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");

JFrame frame=new JFrame("Change JOptionPane icons");

public ChangeJOptionPaneIcons()
{
//---START SETTING NEW ICON---
UIManager uim=new UIManager();

Object error=LookAndFeel.makeIcon(getClass(),"ERROR.gif");
Object information=LookAndFeel.makeIcon(getClass(),"INFORMATION.gif");
Object question=LookAndFeel.makeIcon(getClass(),"QUESTION.gif");
Object warning=LookAndFeel.makeIcon(getClass(),"WARNING.gif");

//Set new icon for Error message
uim.put("OptionPane.errorIcon",error);

//Set new icon for Information message
uim.put("OptionPane.informationIcon",information);

//Set new icon for Question message
uim.put("OptionPane.questionIcon",question);

//Set new icon for Warning message
uim.put("OptionPane.warningIcon",warning);
//---END SETTING NEW ICON---

button.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);

frame.setLayout(new GridLayout(4,1));
frame.add(button);
frame.add(button2);
frame.add(button3);
frame.add(button4);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public void actionPerformed(ActionEvent event)
{
if(event.getSource()==button)
{
JOptionPane.showMessageDialog(frame,"Error Icon","ERROR",JOptionPane.ERROR_MESSAGE);
}
else if(event.getSource()==button2)
{
JOptionPane.showMessageDialog(frame,"Information Icon","INFORMATION",JOptionPane.INFORMATION_MESSAGE);
}
else if(event.getSource()==button3)
{
JOptionPane.showMessageDialog(frame,"Question Icon","QUESTION",JOptionPane.QUESTION_MESSAGE);
}
else if(event.getSource()==button4)
{
JOptionPane.showMessageDialog(frame,"Warning Icon","WARNING",JOptionPane.WARNING_MESSAGE);
}
}

public static void main(String[]args)
{
ChangeJOptionPaneIcons cjopi=new ChangeJOptionPaneIcons();
}
}
//END


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

Get date using Calendar class

Complete source code below will show you, how to get date on local computer in java using Calendar class.



import java.util.Calendar;
import java.util.Locale;

public class GetDate
{
public static void main(String[]args)
{
//Get a calendar using the default time zone and locale.
Calendar currentComputerDate=Calendar.getInstance();

//Print date
System.out.print(currentComputerDate.get(Calendar.DATE));
System.out.print(".");

/*Print month in text representation. For example January is first month.
*Month that will print is in long representation.
*Short representation for January is Jan
*You can try to change Calendar.LONG to Calendar.SHORT
*Local.ROOT is useful constant for the root locale.
*/
System.out.print(currentComputerDate.getDisplayName(Calendar.MONTH,Calendar.LONG,Locale.ROOT));
System.out.print(".");

//Print year
System.out.print(currentComputerDate.get(Calendar.YEAR));
}
}

Get selected JRadioButton from ButtonGroup

Complete source code below will show you, how to get selected JRadioButton from ButtonGroup.

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


import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JOptionPane;
import javax.swing.AbstractButton;

import java.awt.BorderLayout;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import java.util.Enumeration;

public class GetSelectedJRadioButtonFromButtonGroup implements ActionListener
{
//Create two radio button that will be put into a group
//Set start radio button selection to female
JRadioButton firstRadioButton=new JRadioButton("Female",true);
JRadioButton secondRadioButton=new JRadioButton("Male");

//Create a button with text ( What i select )
JButton button=new JButton("What i select");

//Create a window using JFrame with title ( Get selected JRadioButton from ButtonGroup)
JFrame frame=new JFrame("Get selected JRadioButton from ButtonGroup");

//Create a radio button group using ButtonGroup
ButtonGroup bg=new ButtonGroup();

public GetSelectedJRadioButtonFromButtonGroup()
{
//Add all radio button into created group
bg.add(firstRadioButton);
bg.add(secondRadioButton);

//Set JFrame layout to border layout
frame.setLayout(new BorderLayout());

//Create a panel that will be put radio button into it
JPanel panel=new JPanel();

//Add all created radio button into panel
panel.add(firstRadioButton);
panel.add(secondRadioButton);

//Add action listener to created button
button.addActionListener(this);

//Add panel and button into JFrame
frame.add(panel,BorderLayout.CENTER);
frame.add(button,BorderLayout.SOUTH);

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

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

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

//Action for button
//Get selected JRadioButton from ButtonGroup
public void actionPerformed(ActionEvent event)
{
if(event.getSource()==button)
{
Enumeration<AbstractButton> allRadioButton=bg.getElements();

while(allRadioButton.hasMoreElements())
{
JRadioButton temp=(JRadioButton)allRadioButton.nextElement();
if(temp.isSelected())
{
JOptionPane.showMessageDialog(frame,"You select : "+temp.getText());
}
}
}
}

public static void main(String[]args)
{
GetSelectedJRadioButtonFromButtonGroup gsjrbfbg=new GetSelectedJRadioButtonFromButtonGroup();
}
}



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

Two text field in JOptionPane

Complete source code below will show you, how to put two text field into JOptionPane. First text field is to receive username from user and second is to receive password from user. This gui is suitable when you want to receive username and password from user using JOptionPane.

username : jamesBond
password : 007


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


import javax.swing.*;

import java.awt.*;

public class TwoTextFieldInJOptionPane
{
public static void main(String[]args)
{
//Create a panel that will be use to put
//one JTextField, one JPasswordField and two JLabel
JPanel panel=new JPanel();

//Set JPanel layout using GridLayout
panel.setLayout(new GridLayout(4,1));

//Create a label with text (Username)
JLabel username=new JLabel("Username");

//Create a label with text (Password)
JLabel password=new JLabel("Password");

//Create text field that will use to enter username
JTextField textField=new JTextField(12);

//Create password field that will be use to enter password
JPasswordField passwordField=new JPasswordField(12);

//Add label with text (username) into created panel
panel.add(username);

//Add text field into created panel
panel.add(textField);

//Add label with text (password) into created panel
panel.add(password);

//Add password field into created panel
panel.add(passwordField);

//Create a window using JFrame with title ( Two text component in JOptionPane )
JFrame frame=new JFrame("Two text component in JOptionPane");

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

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

//Set JFrame locate at center
frame.setLocationRelativeTo(null);

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

//Show JOptionPane that will ask user for username and password
int a=JOptionPane.showConfirmDialog(frame,panel,"Put username and password",JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE);

//Operation that will do when user click 'OK'
if(a==JOptionPane.OK_OPTION)
{
if(textField.getText().equals("jamesBond")&&new String(passwordField.getPassword()).equals("007"))
{
JOptionPane.showMessageDialog(frame,"You are James Bond","Correct",JOptionPane.INFORMATION_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(frame,"You are not James Bond","False",JOptionPane.ERROR_MESSAGE);
}
}

//Operation that will do when user click 'Cancel'
else if(a==JOptionPane.CANCEL_OPTION)
{
JOptionPane.showMessageDialog(frame,"You pressed Cancel button");
}
}
}


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

Open file using JFileChooser

Complete source code below will show you, how to open file using JFileChooser.

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


/*
*Desktop class only can get when you use JDK 6
*/
import java.awt.Desktop;

import javax.swing.JFileChooser;

import java.io.File;

public class OpenFileUsingJFileChooser
{
public static void main(String[]args)
{
//Create a file chooser
JFileChooser fileChooser=new JFileChooser();

//File chooser will appear in no windows parent
int a=fileChooser.showOpenDialog(null);

//Action that will take when user click open button
if(a==JFileChooser.APPROVE_OPTION)
{
//Get file that want to open
File fileToOpen=fileChooser.getSelectedFile();

try
{
//Open file using suitable program on computer.
//You don't need to tell what program to use.
//Java will select default program that your
//computer use to open the file.
//In windowsXP you can see what
//program that your computer use to open
//a file by right click on file,
//choose properties and see at
//Opens with : ProgramNameToOpenTheFile
Desktop.getDesktop().open(fileToOpen);
}
catch(Exception exception)
{
System.out.println("Problem occour when to open the file");
}
}
}
}


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

Open any file or folder using Desktop class

Complete source code below will show you, how to open C:\ drive using Desktop class. Desktop class is only can get when you use jdk 6.

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


/*
*If you want to use Desktop class,
*you must make sure that jdk version
*that you use is 6. This facilities
*is exist in jdk 6.
*/
import java.awt.Desktop;

import java.io.File;

public class OpenFileUsingDesktopClass
{
public static void main(String[]args)
{
try
{
//Open C:\ drive
//Make sure that you change \ to \\
//You can change to other file or folder that you want
//Example for file that you want to open:
//C:\Sunset.jpg
//So, change C:\\ below to
//C:\\Sunset.jpg
Desktop.getDesktop().open(new File("C:\\"));
}
catch(Exception exception)
{
exception.printStackTrace();
}
}
}


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


Click here to download jdk6
Click here to download jdk6 documentation

Set JButton text color using UIManager

Complete source code below will show you, how to set JButton text color using UIManager. When you set JButton text color using this style, it will make all JButton in your program will have same text color. So, if you want to set only one text color for JButton, i suggest you click here.

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


import javax.swing.UIManager;

import javax.swing.plaf.ColorUIResource;

import javax.swing.*;

import java.awt.*;

public class SetJButtonForegroundColorUseUiManager
{
public static void main(String[]args)
{
/*
*Create color base on RGB color model
*that will use as JButton foreground color.
*Red=255
*Green=0
*Blue=0
*/
Color foregroundColor=new Color(255,0,0);

//Set JButton foreground color
UIManager.put("Button.foreground",new ColorUIResource(foregroundColor));

//Create a window using JFrame with title ( Set JButton foreground color using UIManager )
JFrame frame=new JFrame("Set JButton foreground color using UIManager");

//Create two button
JButton button1=new JButton("Button 1");
JButton button2=new JButton("Button 2");

//Set JFrame layout to FlowLayout
frame.setLayout(new FlowLayout());

//Add created button into frame
frame.add(button1);
frame.add(button2);

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

//Set JFrame size
frame.setSize(400,400);

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

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


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

Open any file or folder in java

Complete source code will show you, how to open any files or folders using java in windows.What you must know is :


rundll32 SHELL32.DLL,ShellExec_RunDLL File_Or_Folder_To_Open


What is rundll32.exe And Why Is It Running?


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


public class OpenFileUsingJava
{
public static void main(String[]args)
{
try
{
//Path for folder or file that you want to open
//In my case i will open C:\ drive
//So create a String like below
//Don't forget to change \ to \\
String a="C:\\";

//Execute command
//Command that you must know : rundll32 SHELL32.DLL,ShellExec_RunDLL File_Or_Folder_To_Open
Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL \""+a+"\"");
}
catch(Exception exception)
{
exception.printStackTrace();
}
}
}


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

What is different between i++ and ++i

I want to share with you about different between i++ and ++i.

++i
**********
int i=0;
System.out.println(++i);
**********
It will print on screen 1


i++
**********
int i=0;
System.out.println(i++);
**********
It will print on screen 0

Answer :
++i will add current i value with 1 before print


i++ will add current i value with 1 after print

Draw triangle in JPanel

Complete source code below will show you, how to draw triangle in JPanel

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


import javax.swing.*;

import java.awt.*;

public class DrawTriangle extends JPanel
{
public DrawTriangle()
{
JFrame frame=new JFrame("Draw triangle in JPanel");
frame.add(this);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public void paint(Graphics g)
{
super.paint(g);

//All triangle corner x coordinate
int[]x={0,150,300};

//All triangle corner y coordinate
int[]y={200,0,200};

//Set color base on RGB
//You can get RGB value for your color at "Color picker" at above
//R=255
//G=192
//B=0
//So after this all you draw will use this color
g.setColor(new Color(255,192,0));

//Draw triangle in JPanel
g.fillPolygon(x,y,3);

//Set color base on RGB
//You can get RGB value for your color at "Color picker" at above
//R=1
//G=1
//B=1
//So after this all you draw will use this color
g.setColor(new Color(1,1,1));

//Set font that will use when draw String
g.setFont(new Font("Arial",Font.BOLD,14));

//Draw String in JPanel
g.drawString("(0,200)",10,200);
g.drawString("(150,0)",150,20);
g.drawString("(300,200)",290,200);
}

public static void main(String[]args)
{
DrawTriangle dt=new DrawTriangle();
}

}


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

RELAXING NATURE VIDEO