Saturday, May 16, 2020

Java Syntax Building an Input Dialog Box

Message dialog boxes are great when you want to inform the user of a message and get a simple response (i.e., a YES or OK click) but there are times when you want the user to give a little bit of data. Maybe your program wants a pop-up window to grab their name or star sign. This can be achieved easily by using the showInputDialog method of the JOptionPane class. The JOptionPane Class To use the JOptionPaneclass you dont need to make an instance of aJOptionPane because it creates dialog boxes through the use of static methods and ​static fields. It only creates modal dialog boxes which is fine for input dialog boxes because generally, you want the user to input something before your application carries on running. The showInputDialog method is overloaded several times to give you a few options about how the input dialog box appears. It can have a text field, a combo box or a list. Each of these components can have a default value selected. Input Dialog With a Text Field The most commonest input dialog simply has a message, a text field for the user to input their response and an OK button: The showInputDialogmethod takes care of building the dialog window, the text field and OK button. All you have to do is provide the parent component for the dialog and the message to the user. For the parent component Im using the this keyword to point to the JFrame the dialog is created from. You can use null or specify a name of another container (e.g., JPanel) as the parent. Defining a parent component enables the dialog to position itself on the screen in relation to its parent. If it is set to null the dialog will appear in the center of the screen.Theinput variable captures the text the user enters into the text field. Input Dialog With a Combo Box To give the user a selection of choices from a combo box you need to use a String array: //Options for the combo box dialogString[] choices {Monday, Tuesday ,Wednesday, Thursday, Friday}; //Input dialog with a combo box String picked (String)JOptionPane.showInputDialog(this, Pick a Day: , ComboBox Dialog, JOptionPane.QUESTION_MESSAGE ï » ¿ , null, choices, choices[0]); As I am passing a String array for the selection values the method decides a combo box is the best way to present those values to the user. This showInputDialog method returns an Object and because I want to get the text value of the combo box selection Ive defined the return value to be a ( String ). Also note that you can use one of OptionPanes message types to give the dialog box a certain feel. This can be overridden if you pass an icon of your own choosing. Input Dialog With a List If the String array you pass to the showInputDialog method has 20 or more entries then instead of using a combo box it will decide to show the selection values in a list box. A full Java code example can be viewed in Input Dialog Box Program. If youre interested in seeing the other dialog boxes the JOptionPane class can create then have a look at the JOptionPane Option Chooser Program.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.