Showing posts with label joptionpane. Show all posts
Showing posts with label joptionpane. Show all posts

Set JOptionPane background color

After i make some review over internet and java forum lastly i understand how to change JOptionPane background color.You can try compile and execute simple code below and see it's result.

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

import javax.swing.JOptionPane;
import javax.swing.UIManager;

import javax.swing.plaf.ColorUIResource;

public class SetJOptionPaneBackgroundColor
{
public static void main(String[]args)
{
UIManager uim=new UIManager();
uim.put("OptionPane.background",new ColorUIResource(255,0,0));
uim.put("Panel.background",new ColorUIResource(255,0,0));

JOptionPane.showMessageDialog(null,"Hello world","HELLO WORLD",JOptionPane.INFORMATION_MESSAGE);
}
}

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

Set JOptionPane warning icon

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
**************************************************************************

Set JOptionPane error icon

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
**************************************************************************

Set JOptionPane information icon

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
**************************************************************************

Set JOptionPane question icon

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
**************************************************************************

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
****************************************************************

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
***********************************************************************

RELAXING NATURE VIDEO