JOptionPane showInputDialog Java Examples
I thought is a good Idea i share about JOptionPane showInputDialog examples.First we will start it being simple and as we progress we will add more complexity to them.We will setLookAndFeel to windows type.
Simple JOptionPane showInputDialog
[
import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.UIManager; public class ShowInputDialog { public static void main(String args[]) { JFrame f=new JFrame("Input Dialog Example"); f.setSize(400, 400); f.setLocationRelativeTo(null); f.setVisible(true); try { UIManager.setLookAndFeel
("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel"); } catch(Exception e){e.printStackTrace();} String firstname=JOptionPane.showInputDialog(f,"Whats your firstname"); JOptionPane.showMessageDialog(f, "Your firstname is "+firstname); } }
]
showInputDialog Code Analysis
Above code has two JOptionPanes i.e showMessageDialog and showInputDialog.
showInputDialog gets string firstname which is passed to the second JOptionPane.showMessageDialog to be displayed.
UIManager.setLookAndFeel() sets look and feel to windows type.
showInputDialog JOptionPanes takes care of ActionListener triggered by the user
Now, let's add one more thing to our JOptionPane input dialog.
JOptionPane showInputDialog with Tittle and Message Types
Just like JOptionPane showMessageDialog, inputDialogs also have different.They include.
- ERROR_MESSAGE
- INFORMATION_MESSAGE
- PLAIN_MESSAGE
- QUESTION_MESSAGE
- WARNING_MESSAGE
Below code adds Tittle and Message type to inputDialog.
[
/** * * @author Eric */import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.UIManager; public class ShowInputDialog { public static void main(String args[]) { JFrame f=new JFrame("Input Dialog Example"); f.setSize(300, 300); f.setLocationRelativeTo(null); f.setVisible(true); try { UIManager.setLookAndFeel
("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel"); } catch(Exception e){e.printStackTrace();} String firstname=JOptionPane.showInputDialog(f,"Enter your firstname","Input Dialog",JOptionPane.WARNING_MESSAGE); JOptionPane.showMessageDialog(f, "Your firstname is "+firstname,"Message Box",JOptionPane.INFORMATION_MESSAGE); } }
]
Result
showInputDialog with Combo Box
This last example is going to add a combo box to inputDialog Box.Combo box will allow the user to select one time that will be returned to the system.Run below code.
[
[
import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.UIManager; public class ShowInputDialog { public static final String[] fruits={"Bananas","Mango","Lemon","WaterMelon","Oranges"}; public static void main(String args[]) { JFrame f=new JFrame("Input Dialog Example"); f.setSize(300, 300); f.setLocationRelativeTo(null); f.setVisible(true); try { UIManager.setLookAndFeel
("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel"); } catch(Exception e){e.printStackTrace();} String type=(String)JOptionPane.showInputDialog(f,"Select Your Favourite Fruit",
"Fruit Combo Box",JOptionPane.QUESTION_MESSAGE,null,fruits,fruits[0]); JOptionPane.showMessageDialog(f, "Your firstname is "+type,"Message Box",
JOptionPane.INFORMATION_MESSAGE); } }]