JOptionPane (english)
Hi Guys. So far, we have checked the basics
for Graphical User Interfaces and can roughly comprehend with the knowledge of
how to program a simple application.
Today we will deal with the messages in the
programming language Java. The following messages are used in the Java
programming language: error message, warning, decision.
For
example, we would like to sign up for a computer at the university. After we
have entered our username, we still have to enter the personal secret number.
Imagine that we entered a character in the password field incorrectly. Then the
program will give us the error message. This error message may look like this:
In this case, the user is only informed
that his statement is wrong and not what he should do. In most cases, the user
is offered after repeated incorrect information if they want to reset their
password.
The form of a message looks like this:
The form of a message looks like this:
A message in Java looks like in the picture
we can see it with the respective structure. Only this is not so nice colorful.
The red area symbolizes the icon which in the above illustration is called a violet
colored circle. The message is the yellow area and will give the user the error
message right there. In doing so, the user can input what not the case above is
but the user can select one or more options.
There are several methods in the Java programming language which are listed as follows.
There are several methods in the Java programming language which are listed as follows.
Constant:
ERROR_MESSAGE
INFORMATION_MESSAGE
WARNING_MESSAGE
QUESTION_MESSAGE
PLAIN_MESSAGE
DEFAULT_OPTION
YES_NO_OPTION
YES_NO_CANCEL_OPTION
OK_CANCEL_OPTION
Which types we use depends on the
situation. It would not make much sense if we chose the question option as
message type. Therefore, I would like to share some examples here.
The JOptionPane Java class provides four
static methods for quickly creating and displaying a dialog without first
having to create an object.
Diese statischen
Methoden sind:
static int
showConfirmDialog(Component parentComponent, Object message)
static String
showInputDialog(Component parentComponent, Object message)
static void showMessageDialog(Component
parentComponent, Object message)
static int showOptionDialog(Component
parentComponent, Object message,
static int String
title, int optionType, int
messageType, Icon icon,
static int Object[]
options, Object initialValue)
The static methods presented above are also
available in extended form, i. with more parameters.
Examples of using the methods for creating
a JOptionPanes:
JOptionPane.showInputDialog("This is not an Input dialog");
JOptionPane.showConfirmDialog(null,
"This is confirm Dialog");
JOptionPane.showMessageDialog(null,
"This is a message
Dialog");
JOptionPane.showOptionDialog(null,
"This is a Option
dialog","Option
dialog",JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.WARNING_MESSAGE, null, new String[]{"A", "B",
"C"}, "B");
In the first example, we create a dialog that requires
text input from the user. The
parameter specifies the message to be displayed. This
dialog internally uses the option type OK_CANCEL_OPTION.
The second dialog is a dialog that displays a message and offers the options "Yes", "No" and "Cancel". The first parameter is simply set to the null reference, so an internal standard frame is used for the display. The second parameter specifies the message to be displayed. This dialog internally has the option type YES_NO_CANCEL_OPTION.
The third dialogue is a simple information dialog that only has an "ok" button. We also set the first parameter to zero here because we have not defined a frame. This dialog internally has the option type DEFAULT_OPTION.
The function showOptionDialog offers the possibility to put together its own JOptionPane and therefore expects more parameters. The third parameter is a title, that is the title of the dialog. The fourth parameter can be used to specify the option type, the fifth message type. As the sixth parameter we can set our own icon. Since we have not yet covered icons, we pass here the null reference so that the default icon associated with the message type is selected. The seventh parameter is an array of self-defined button texts, in which case we create buttons labeled "A", "B" and "C". The last parameter indicates which of the previously defined buttons should be selected by default. In our case the button "B" is preselected.
Looking again at the static method overview, we see that the showInputDialog method returns a string. The return value contains the text from the entry field of the JOptionPanes. This can then be used for further processing.
The methods showConfirmDialog and showOptionDialog return an integer value. This indicates which button has been pressed. These integer values are again constants of the class JOptionPane. The following overview shows the constants:
The second dialog is a dialog that displays a message and offers the options "Yes", "No" and "Cancel". The first parameter is simply set to the null reference, so an internal standard frame is used for the display. The second parameter specifies the message to be displayed. This dialog internally has the option type YES_NO_CANCEL_OPTION.
The third dialogue is a simple information dialog that only has an "ok" button. We also set the first parameter to zero here because we have not defined a frame. This dialog internally has the option type DEFAULT_OPTION.
The function showOptionDialog offers the possibility to put together its own JOptionPane and therefore expects more parameters. The third parameter is a title, that is the title of the dialog. The fourth parameter can be used to specify the option type, the fifth message type. As the sixth parameter we can set our own icon. Since we have not yet covered icons, we pass here the null reference so that the default icon associated with the message type is selected. The seventh parameter is an array of self-defined button texts, in which case we create buttons labeled "A", "B" and "C". The last parameter indicates which of the previously defined buttons should be selected by default. In our case the button "B" is preselected.
Looking again at the static method overview, we see that the showInputDialog method returns a string. The return value contains the text from the entry field of the JOptionPanes. This can then be used for further processing.
The methods showConfirmDialog and showOptionDialog return an integer value. This indicates which button has been pressed. These integer values are again constants of the class JOptionPane. The following overview shows the constants:
YES_OPTION
NO_OPTION
CANCEL_OPTION
OK_OPTION
CLOSED_OPTION
In the next few days, we will also program a sample
application with a message and also upload its source file.
Kommentare
Kommentar veröffentlichen