JOptionPane showInputDialog Java Examples

JOptionPane showInputDialog Java Examples

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);

    }

}
]


Output

showInputDialog  with Combo Box

COMMENTS

Name

android android Apps android emulators antivirus blogger bluestack Break Statement in Java constructors download downloader drivers emulators java java control statements java do-while loop java for loop java infinitive do-while loop java system methods java tutorials Java While Loop java.awt java.swing javalookandfeel javascript JButton JCheckBox JColorChooser JComboBox JDialog JFileChooser JFrame JLayeredPane JList JMenuBar joomla JOptionPane JPanel JPasswordField JPopupMenu JProgressBar JRadioButton JScrollBar JSlider JSpinner JTabbedPane JTable JTextArea JTextField JToolBar mysql php plugins Polymorphism setToolTiptext skype for windows software swing class themes Visual Studio Code WeChat whatsapp wordpress
false
ltr
item
Java Codes: JOptionPane showInputDialog Java Examples
JOptionPane showInputDialog Java Examples
JOptionPane showInputDialog Java Examples
https://2.bp.blogspot.com/-FobbMaAzo34/WKHsRRHe4SI/AAAAAAAAG4s/KIhIb_z-9xUbipDrVB2P2MFnNWfkvYdigCLcB/s200/JOptionPane-showInputDialog-Java-Examples-min.PNG
https://2.bp.blogspot.com/-FobbMaAzo34/WKHsRRHe4SI/AAAAAAAAG4s/KIhIb_z-9xUbipDrVB2P2MFnNWfkvYdigCLcB/s72-c/JOptionPane-showInputDialog-Java-Examples-min.PNG
Java Codes
http://www.download-all.net/2017/02/joptionpane-showinputdialog-java.html
http://www.download-all.net/
http://www.download-all.net/
http://www.download-all.net/2017/02/joptionpane-showinputdialog-java.html
true
7826648292785071009
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy