[go: up one dir, main page]

0% found this document useful (0 votes)
328 views10 pages

Exercise No 3 Java Swing

1. The document discusses using radio buttons, button groups, and JOptionPane message dialog boxes in Java GUI applications. Radio buttons allow single selection from options and are grouped using button groups to ensure only one can be selected. 2. JOptionPane includes methods like showMessageDialog() to display different types of messages specified by parameters like ERROR_MESSAGE. These methods accept parameters for the message, title, and icon. 3. The procedures provide steps to create a sample application with radio buttons and button groups to select options, and display the selection using JOptionPane message dialogs. Running the program demonstrates the radio button and message display functionality.

Uploaded by

Jayson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
328 views10 pages

Exercise No 3 Java Swing

1. The document discusses using radio buttons, button groups, and JOptionPane message dialog boxes in Java GUI applications. Radio buttons allow single selection from options and are grouped using button groups to ensure only one can be selected. 2. JOptionPane includes methods like showMessageDialog() to display different types of messages specified by parameters like ERROR_MESSAGE. These methods accept parameters for the message, title, and icon. 3. The procedures provide steps to create a sample application with radio buttons and button groups to select options, and display the selection using JOptionPane message dialogs. Running the program demonstrates the radio button and message display functionality.

Uploaded by

Jayson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Name: Score:

Student No. Date:


Computer No.

Exercise No. 5 JOption Pane Messages , Radio Buttons and Button Group in Java

I. Objectives

At the end of this activity, the students are expected to:

1. Differentiate the use of radio buttons and button group class


2. Create GUI using radio buttons and button group
3. Develop programs using radio buttons and button group
4. Show the different message dialog boxes and icons
5. Develop programs using JOptionPane class message dialog boxes and icons

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)

Figure 1. The use of button group class

 The JFormDesigner displays lines connecting the grouped buttons


 They are non-visual beans. They appear at the bottom of the Structure view and in
the Design view, figure 2.
Figure 2. The buttongroup

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:

JOptionPane.method(frame container, “message to be


displayed”, “title of the dialog box”,
JOptionPane.parameter icon);

 Table 1 shows some of the use of JOptionPane class

Table 1. Use of JOptionPane Class

Java Code Message


showMessageDialog

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 
OtherSwing GUI Forms  JFrame Form
3.2 Enter RadioButtons the class name and my.radio_buttons as the package and click
Finish.

4. Adding radio buttons


4.1 Create the GUI as shown in figure 3.
Figure 3. The GUI for radio buttons demonstration

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.

4.3 Run the program. What happened?

5. Creating a ButtonGroup. Create another project named “ButtonGroup”,


“buttongroup” as the class name and “my.buttongroup” as the package.
5.1 Drag a ButtonGroup from the pallete and drop it into your GUI. It will show
up under the Other Components in the Inspector panel (as shown in
figure4)
Figure 4. The Inspector panel

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.

Figure 7. Button Group demonstration


6. Now, develop the codes that will display a dialog box based on user’s choices.

7. Run your program. What happened?

8. When finished, shut down our computers.

You might also like