JOptionPane in Java

JOptionPane in Java is used to provide dialog boxes such as message dialog box, input dialog box and confirm dialog box. These dialog boxes are used to get input from user or just to display information.

there are 3 common methods of the JOptionPane which is :

  • showMessageDialog()
  • showInputDialog()
  • showConfirmDialog()

showMessageDialog()

import javax.swing.*;
public class OptionPaneExample {
JFrame f;
OptionPaneExample(){
f=new JFrame();
JOptionPane.showMessageDialog(f,”Hello WORLD!”);
}
public static void main(String[] args) {
new OptionPaneExample();
}
}

joptionpane in java

import javax.swing.*;
public class OptionPaneExample {
JFrame f;
OptionPaneExample(){
f=new JFrame();
JOptionPane.showMessageDialog(f,”Successfully Updated.”,”Alert”,JOptionPane.WARNING_MESSAGE);
}
public static void main(String[] args) {
new OptionPaneExample();
}
}

joptionpane in java

showInputDialog()

import javax.swing.*;
public class OptionPaneExample {
JFrame f;
OptionPaneExample(){
f=new JFrame();
String name=JOptionPane.showInputDialog(f,”Enter Name”);
System.out.println(name);
}
public static void main(String[] args) {
new OptionPaneExample();
}
}

joptionpane in java

showConfirmDialog()

import javax.swing.;
import java.awt.event.;
import javax.swing.;
import java.awt.event.;
public class OptionPaneExample extends WindowAdapter{
JFrame f;
OptionPaneExample(){
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 a=JOptionPane.showConfirmDialog(f,”Are you sure?”);
if(a==JOptionPane.YES_OPTION){
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public static void main(String[] args) {
new OptionPaneExample();
}
}

joptionpane in java

Published by

Rafif Santika

The programming language that I specialize in is Java, because I think Java programming language is more universal and of course because I like it regardless of any reason.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.