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