Java Tutorial : JOptionPane

JOptionPane is a java Class used to display dialog boxes. There are three types of JOptionPanes : message dialog box, confirm dialog box and input dialog box.

Message Dialog Box
import javax.swing.*;  
public class op {  
JFrame f;  
op(){  
    f=new JFrame();  
    JOptionPane.showMessageDialog(f,"www.codeit.blog");  
}  
public static void main(String[] args) {  
    new op();  
}  
}  
Confirm Dialog Box
import javax.swing.*;  
import java.awt.event.*;  
public class op extends WindowAdapter{  
JFrame f;  
op(){  
    f=new JFrame();   
    f.addWindowListener(this);  
    f.setSize(300, 300);  
    f.setLayout(null);  
    f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);  
    f.setVisible(true);  
}  
public void windowClosing(WindowEvent e) {  
    int i=JOptionPane.showConfirmDialog(f,"Confirm Payment");  
if(i==JOptionPane.YES_OPTION){  
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
}else {
	System.out.print("Nope this is not cool");
}
}  
public static void main(String[] args) {  
    new  op();  
}     
}
Input Dialog Box
import javax.swing.*;  
public class op {  
JFrame f;  
op(){  
    f=new JFrame();   
    String Code=JOptionPane.showInputDialog(f,"Enter Verification Code");      
}  
public static void main(String[] args) {  
    new op();  
}  
}

Warning Dialog Box

import javax.swing.*;  
public class op {  
JFrame f;  
op(){  
    f=new JFrame();  
    JOptionPane.showMessageDialog(f,"Uh Oh something may be wrong","Warning",JOptionPane.WARNING_MESSAGE);     
}  
public static void main(String[] args) {  
    new op();  
}  
}  

Error Dialog Box
import javax.swing.*;  
public class op {  
JFrame f;  
op(){  
    f=new JFrame();  
    JOptionPane.showMessageDialog(f,"Something is fucked up!","Error",JOptionPane.ERROR_MESSAGE);     
}  
public static void main(String[] args) {  
    new op();  
}  
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s