JOptionPane class is a class that inherits from JComponent. This class will allow us to create alerts or dialogs simple to apply, or display, user information.
The methods reviewed in this small tutorial will be:
- JOptionPane.showMessageDialog (...)
- JOptionPane.showConfirmDialog (....)
- JOptionPane.showInputDialog (...);
- JOptionPane.showOptionDialog (...);
method has a distinctive but all they show us a window pop up that will allow us to capture user information.
- showMessageDialog
JOptionPane. showMessageDialog (componentePadre Component, Object message, String title, int tipoDeMensaje)
We used to display information, for example a warning that we ask the user. Let's see what are the main arguments of the method.
componentePadre : it is for example the Frame from which the call. If we want we can put in null, which is more or less the same.
Message: do we want to read the dialog.
Title: the title.
tipoDeMensaje : are constants that java will tell what kind of message we want to display. According to these icons will be shown in the dialog box. The options are
- ERROR_MESSAGE
- INFORMATION_MESSAGE
- WARNING_MESSAGE
- QUESTION_MESSAGE
- PLAIN_MESSAGE
If we display a custom icon, we can add it to the final as an argument.
Example:
JOptionPane.showMessageDialog (null, "Access Denied", "Error",
JOptionPane.ERROR_MESSAGE)
- showConfirmDialog
This method serves to prompt the user for confirmation. For example, a departure confirmation system
Int response = JOptionPane. showMessageDialog (componentePadre Component, Object message, String title, int tipoDeOpcion)
Above is the short version of the method's arguments. The long version includes the type of message and the icon, if we want to customize.
The arguments are identical to the previous method. Except for the type of option, which is another constant and values \u200b\u200bcan be:
- DEFAULT_OPTION
- YES_NO_OPTION
- YES_NO_CANCEL_OPTION
- OK_CANCEL_OPTION
As we see the method returns an integer that will allow us to grasp what is the option chosen by the user. The values \u200b\u200bwill 0 to If , 1 to No, 2 to Cancel and -1 for closing window . So we ask what is the return value and take the action we want. Example
int i = JOptionPane.showConfirmDialog (this, "Really Quit Swing Hello?", "Confirm Exit", JOptionPane.YES_NO_OPTION)
if (i = = 0) {
System.exit (0);
}
- showInputDialog
This method gives us a window where you can insert a String. For example when we want the user to insert his name. The short version of the method is
JOptionPane.showInputDialog String response = (Object message)
This method returns a String so that it can use later. The long version of the arguments of the method is similar to previous ones.
Example:
JOptionPane.showInputDialog String name = ("Insert your Name");
this.lbl_nombre.setText (name);
We can also create a dialog box containing a combo with the default options you want to give to the user. Ahem
:
Object [] valoresPocibles = {"Pedro", "John", "Carlos"};
JOptionPane.showInputDialog Object head = (null,
"Select which is his immediate boss, "" Select Boss "
JOptionPane. QUESTION_MESSAGE, null,
valoresPocibles, valoresPocibles [0]);
this.lbl_jefe.setText ((String) chief);
The array of possible values \u200b\u200bin a combo shows which will be the leaders who can show you. The last argument of the shows which will be selected by default
showOptionDialog
With this method we can create dialog boxes with custom buttons. Is good to give a personal touch to the dialog boxes
The method has the form: Int
JOptionPane.showOptionDialog res = ( componentePadre Component, Object message, String title, int tipoDeOpcion, tipoMensaje int, Icon icon, Object [] keys, Object botonDefault )
Here all the rest varies with the array of buttons that we have, we must emphasize that we do not have to buttons, we just have to put the text which will be released in the.
Object [] keys = {"If you give will be good," "No? ... Boo"};
JOptionPane.showOptionDialog int i = (null, "You want to go dancing This Night "," Sita "
JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, buttons, buttons [0]);
System.out.println (i);
Then we take the response as it did with the confirmDialog .
Well until next time, this is the most important messages or alerts.
0 comments:
Post a Comment