Exercise No 3 Java Swing
Exercise No 3 Java Swing
Exercise No. 5 JOption Pane Messages , Radio Buttons and Button Group in Java
I. Objectives
II. Discussion
Radio Buttons ()
Used to allow users to make a single choice from a set of options
Uses the JRadioButton Class
It can be found at the NetBeans Palette, under the Swing Container
ButtonGroup (javax.swing.ButtonGroup)
Allows grouping of buttons under one (1) group (as shown in figure 1)
JOptionPane Class
It includes the showMessageDialog(), showConfirmDialog(),
showInputDialog(), and showOptionDialog() methods
Each method accepts a message type parameter that determines the look and feel
of the dialog box, along with the default icon displayed (as shown in figure 3).
The parameters can be any of the following :
1. ERROR_MESSAGE
2. INFORMATION_MESSAGE
3. WARNING_MESSAGE
4. QUESTION_MESSAGE
5. PLAIN_MESSAGE
The proper on how to use the JOptionPane is:
Warning Message
JOptionPane.
showMessageDialog (this,
“you are running low on
memory”, “Memory
Warning”,
JOptionPane.WARNING_MESSA
GE);
JOptionPane.showMessageDi
Java Code Message
alog(this, "Are you Question Message
sure???","Quit",
JOptionPane.QUESTION_MESS
AGE);
Error Message
JOptionPane.showMessageDi
alog(this, "Wrong
Choice@$%@#","Choices",
JOptionPane.ERROR_MESSAGE
);
Information Message
JOptionPane.showMessageDi
alog(this, "you choose
Java Applet","Favorite
Java Application",
JOptionPane.INFORMATION_M
ESSAGE);
JOptionPane.showMessageDi
alog(this, "you choose Plain Message
Java Applet","Favorite
Java Application",
JOptionPane.PLAIN_MESSAGE
);
Java Code Message
showConfirmDialog
Information Message
JOptionPane.showConfirmDi
alog(this, "you choose
Java Applet","Favorite
Java Application",
JOptionPane.INFORMATION_M
ESSAGE);
Plain Message
JOptionPane.showConfirmDi
alog(this, "you choose
Java Applet","Favorite
Java Application",
JOptionPane.PLAIN_MESSAGE
);
Warning Message
JOptionPane.showConfirmDi
alog(this, "you choose
Java Applet","Favorite
Java Application",
JOptionPane.WARNING_MESSA
GE);
JOptionPane.showConfirmDi
alog(this, "you choose Error Message
Java Applet","Favorite
Java Application",
JOptionPane.ERROR_MESSAGE
);
Java Code Message
III. Procedure
1. Open NetBeans. Create a new project, name it as “RadioButtons”. Click File New
Project. Choose File > New Project. In the Categories pane, select the Java mode. In the
Projects pane, choose Java Application. Click Next. Type RadioBbuttons in the Project
Name field.
2. Deselect the Create Main Class checkbox if it is selected. Click Finish.
3. Adding a JFrame container
3.1 In the Project Windows, right-click the form_controls node and choose New
JFrame Form. Alternately, you can find a JFrame form by choosing New
OtherSwing GUI Forms JFrame Form
3.2 Enter RadioButtons the class name and my.radio_buttons as the package and click
Finish.
4.2 Now, we need to display a message showing the user’s choice. To do this, we will be
needed the JOptionPane Class. With table 1 as your reference guide, develop the Java
code that will display the user’s choice through message dialog box.
5.2 For now, leave the variable name as it is. Now, select a radio button in your
GUI. In the Properties panel look for the buttonGroup property, click the combo
next to it and select your button group as shown in figure 5.
Figure 5. The jRadioButton properties
5.3 Add two (2) more radio buttons in your button group, as shown in figure 6.
Figure 6. The Button Group demonstration
5.4 Now, add another button groups that will be act as the place holders for gender
and age groups, as shown in figure 7.