CCS103 - Module-Student-Copy-Comprog2 (20250119172915)
CCS103 - Module-Student-Copy-Comprog2 (20250119172915)
Introduction to
Array
Jeruz Elises Claudel
Course Instructor
PAMANTASAN NG CABUYAO |Introduction to Array 2
LEARNING OUTCOMES
RESOURCES NEEDED
• Links to videos
• Links to websites
• Reference materials, tools, and equipment
PAMANTASAN NG CABUYAO |Introduction to Array 3
________________________________________
4. These are individual values that are in the arrays.
4 Pre-Activity Exercise
________________________________________
5. What are the two possible ways in declaring an
array?
5 Introduction to Array
________________________________________
6 Array Declaration
9 Activity
Summary
10
11 Posttest
PAMANTASAN NG CABUYAO |Introduction to Array 4
Pre-Activity Exercise
Using Scanner create a program that will get the sum of a 5 length array.
Example:
.
PAMANTASAN NG CABUYAO |Introduction to Array 5
What is an Array?
Example:
byte[ ] exampleByte;
short[ ] exampleShort;
long[ ] exampleLong;
float[ ] exampleFloat;
double[ ] exampleDouble;
boolean[ ] exampleBoolean;
char[ ] exampleChar;
int[ ] exampleInt;
Example:
int exampleInt[];
PAMANTASAN NG CABUYAO |Introduction to Array 7
Example:
Identifier [index];
name [2];
//0 1 2 3 4 5
String name[] = {"Joey","Ross","Chandler","Monica", "Phoebe","Rachel"};
System.out.println(name[2]);
PAMANTASAN NG CABUYAO |Introduction to Array 8
We can also use for loop & for each loop to access all our arrays elements.
Example 1:
Example 2:
int num[] = new int [5];
num[0]= 10;
num[1]= 20;
num[2]= 30;
num[3]= 40;
num[4]= 50;
Example 3:
Activity 1
Task: Write a program that will determine the common elements between two array
integers.
Example:
PAMANTASAN NG CABUYAO |Introduction to Array 10
Group Activity 1
SUMMARY
Elements are the individual values in an Array. And Index are numbers that
represents a position in a collection.
KEY TERMS
POSTTEST
Directions: Fill in the blank with the letter corresponding to your answer.
_______ 1. If you declare an integer array as follow, what is the value of num[3]?
int num[] ={101,202,303,404,505,606};
a. 101 c. 303
b. 202 d. 404
_______ 5. If you declare a String Array as follows what is the value of name[0] ?
int name[] = {“Ross”, “Chandler”, ”Monica”, “Phoebe”, ”Joey”, “Rachel”};
a. Ross c. Monica
b. Chandler d. Program Error
_______ 6. If the array length is 10 what is the last index number of the array?
a. 1 c. 10
b. 0 d. 9
_______ 7. For the array: float stats[3]; What is the range of the index?
a. 0 to 2 c. 0 to 3
b. 1 to 2 d. 1 to 4
_______ 8. int nums[ ] ={2, 3, 5, 8, 9, 11}; How would you access the fourth element in nums?
a. Nums[0] c. Nums[3]
b. Nums[4] d. Nums[5]
PAMANTASAN NG CABUYAO |Introduction to Array 13
REFERENCES
[1] Arrays in Java from https://www.geeksforgeeks.org/arrays-in-java/
[2] Java Array Retrieved from https://www.javatpoint.com › array-in-java
[2] Java Arrays from https://www.programiz.com/java-programming/arrays
PAMANTASAN NG CABUYAO |Introduction to Array 1
One Dimensional
Array
Jeruz Elises Claudel
Course Instructor
PAMANTASAN NG CABUYAO |Introduction to Array 2
LEARNING OUTCOMES
RESOURCES NEEDED
• Links to videos
• Links to websites
• Reference materials, tools, and equipment
PAMANTASAN NG CABUYAO |Introduction to Array 3
12 Group Activity
Summary
13
14 Posttest
PAMANTASAN NG CABUYAO |Introduction to Array 4
Introduction
One-Dimensional Array from the name itself one-dimensional array
we know that this means it must deal with only one parameter. Entities of
similar types can be stored together using one-dimensional arrays. It can store
primitive data types (int, float, char, etc.) or objects.
A one-dimensional array can be visualized as a single row or a column of array elements that are
represented by a variable name and whose elements are accessed by index values.
PAMANTASAN NG CABUYAO |Introduction to Array 5
Datatype Identifier[ ];
Or
Datatype[] Identifier;
Data-type: This determines the data type of each element present in the array. (Char,int float,
objects etc.).
[ ]: It is called subscript
Example:
int exampleInt[ ];
long exampleLong[ ] ;
float exampleFloat[ ] ;
char exampleChar[ ] ;
The new int[10] initializes and creates an object referenced as number and assigns memory to the
object in heap segment.
By default, the new operator initializes the elements in the array to zero (for numeric types), false
(for Boolean).
PAMANTASAN NG CABUYAO |Introduction to Array 7
Remember in our previous lessons that Scanner is used to get user input and it is found in the
java.util packages.
Example 4:
public static void main(String[] args) {
System.out.println(Arrays.toString(array));
}
}
PAMANTASAN NG CABUYAO |Introduction to Array 9
Example 6:
Activity 2
Task: Write a program that will find the number based on your selected Array Length. The
program should do the following:
• The program can read at least 100 numbers.
• The program can determine the average length of an Array.
• The program finds the number of the items greater than the average.
• The program can enter an output.
Example:
PAMANTASAN NG CABUYAO |Introduction to Array 11
SUMMARY
Array in java is index-based, the first element of the array is stored at the 0th
index, the second element at the 1st index, and so on.
One dimensional arrays in Java are static in size. This means, once created,
the size of one dimensional arrays cannot be changed.
KEY TERMS
POSTTEST
Directions: Fill in the blank with the letter corresponding to your answer.
_______ 1. for(int i=0; i<10; i++). Which index holds the value of 5?
a. 1
b. 0
c. 2
d. 4
_______ 5. What is the correct way to initialize an integer array with the length of 10?
a. int num = new int[10];
b. String num[ ] = new int[10];
c. int num[] = new int[10];
d. None of the Above
PAMANTASAN NG CABUYAO |Introduction to Array 13
REFERENCES
[1] Arrays in Java from https://www.geeksforgeeks.org/arrays-in-java/
[2] Java Array Retrieved from https://www.javatpoint.com › array-in-java
[2] Java Arrays from https://www.programiz.com/java-programming/arrays
PAMANTASAN NG CABUYAO |Introduction to Array 1
Two Dimensional
Array
Kenneth Dynielle Lawas
Course Instructor
PAMANTASAN NG CABUYAO |Introduction to Array 2
LEARNING OUTCOMES
RESOURCES NEEDED
• Links to videos
• Links to websites
• Reference materials, tools, and equipment
PAMANTASAN NG CABUYAO |Introduction to Array 3
TRUE or FALSE
3 Pre-Test
12 Group Activity
13 Summary
14 Posttest
PAMANTASAN NG CABUYAO |Introduction to Array 4
Introduction
A 2D array in Java is fundamentally an array of arrays. It is a data structure
that enables the storage of information in a grid-like format with rows and
columns. Row and column indexes uniquely identify each constituent in the
2D array.
To designate a 2D array in Java, you must specify the data type and
dimensions of the array's elements. Here is the basic syntax:.
For example, let's say we want to create a 2D array to store integers with 3 rows and 4 columns.
We can declare and initialize it like this:
With this declaration, a three-by-four-element array with the name myArray is created. Initially,
the array will have the default values for each element, which in this case are integer values of 0.
With this we declare a 5 by 5 element array with the name sampleArray is created. We call this
as array with declaration.
PAMANTASAN NG CABUYAO |Introduction to Array 5
Once again 2D arrays are created in rows and columns and each of them represents its indexes.
Example:
In our first example we call num[0][0] which has the value of 1 and num[0][1] which has the
value of 2.
Example 2:
To iterate through a 2D array in Java we can use a for loop where in we can set the condition
base on the size of the array.
Or we can also use .length wherein it gets the size of our array.
PAMANTASAN NG CABUYAO |Introduction to Array 7
Example 3:
Activity 2
Task: Imagine you are organizing a seating arrangement for a dinner party. You have a 2D
array representing the available seats, where each element represents a seat. The
value 0 represents an empty seat, and 1 represents an occupied seat. Your task is to
find an empty seat for a late-arriving guest. How would you approach this problem
using a 2D array and Java?
PAMANTASAN NG CABUYAO |Introduction to Array 10
SUMMARY
To declare and initialize a 2D array, you need to specify the data type of the
elements and the dimensions of the array using square brackets. For
example, int[][] array = new int[3][4]; creates a 2D array with 3 rows and 4
columns.
You can access and modify elements in a 2D array using the row and column
indices. The indices are zero-based, meaning the first row or column has an
index of 0. For example, int element = array[1][2]; retrieves the value at the
second row and third column.
2D arrays can also be jagged arrays, where each row can have a different
number of columns. This allows for flexibility in representing irregular data
structures.
KEY TERMS
POSTTEST
Directions: Fill in the blank with the letter corresponding to your answer.
_______ 1. for(int i=0; i<10; i++). Which index holds the value of 5?
a. 1
b. 0
c. 2
d. 4
_______ 5. What is the correct way to initialize an integer array with the length of 10?
a. int num = new int[10];
b. String num[ ] = new int[10];
c. int num[] = new int[10];
d. None of the Above
PAMANTASAN NG CABUYAO |Introduction to Array 12
REFERENCES
[1] https://docs.oracle.com/en/java/
[2] https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
[3] https://introcs.cs.princeton.edu/java/14array/
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2 1
Multidimensional Array
Part 2
Jeruz E. Claudel
Course Instructor
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2 2
LEARNING OUTCOMES
RESOURCES NEEDED
Example of 3D Array
6 Definition
Summary
11 Key Term
References
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2 4
WORD SEARCH
Find the following words in the puzzle. Words are hidden in different directions
DATATYPE
THREEDIMENSIONAL
COMPLEX
ARRAY
PURPOSE OF 3D ARRAY
Three dimensional array is useful when we want to handle a group of
elements belonging to another group. For example, suppose a college has three
departments: Electronics, Computer Science, and Information Technology.
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2 5
3D ARRAY DECLARATION
Declaration Syntax:
Initialization Syntax:
array_name[array_index][row_index][column_index] = value;
REMEMBER!
Example:
• The first index represents tables/arrays. This indicates how many tables
or arrays a 3d array will have.
• The second index represent the number of Rows. It signifies the total
number of rows an array will have.
• The third index represents the number of Columns. It indicates the total
columns in the 3D Array.
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2 7
Example #1:
Example #2:
Example #3:
Asking the user to input a 3x2x2 elements in the array using the scanner and printing the user input
element into a 3x2x2 table
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2 9
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2 10
ACTIVITY
SUMMARY
KEY TERMS
Array
3D Array
2D Array
Datatype
Identifier
POST-TEST
REFERENCES
[1] Arrays in Java from https://www.geeksforgeeks.org/arrays-in-java/
[2] Java Array Retrieved from https://www.javatpoint.com › array-in-java
[2] Java Arrays from https://www.programiz.com/java-programming/arrays
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2 1
Jagged Array
Jeruz E. Claudel
Course Instructor
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2 2
LEARNING OUTCOMES
RESOURCES NEEDED
Significance of Jagged
9 Array
CRUD in Array
10
Summary
13 Key Term
References
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2 4
WORD SEARCH
Scramble the provided letters to find the hidden words.
A jagged array in Java is a collection of arrays where each array may contain a
varied number of elements. A two-dimensional array, in contrast, requires all
rows and columns to have the same length.
Jagged arrays are also known as "ragged arrays" or "irregular arrays". They can
be created by specifying the size of each array in the declaration. For example,
a jagged array with three rows can have the first row with three elements, the
second with two elements, and the third with four elements.
Array Recap:
• Arrays in Java are collections of similar types of elements. They are
objects, and the elements are stored in contiguous memory locations.
• Indexing of Java starts from 0 and goes up to the length of the array
minus 1 (0 to length-1).
• Arrays are stored in the heap memory.
• We have two types of Array. (Single Dimensional Array &
Multidimensional Array).
new int[] { 1, 2, 3, 4 },
};
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2 6
int arraySample[][] ={
new int[] { 1, 2, 3, 4 },
int arraySample[][] ={
{ 1, 2, 3, 4 },
{ 4, 5},
{ 6, 7, 8},
};
Example #1:
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2 8
Example #2:
Asking the user to input the size of rows and column of the Jagged Array.
Exaample #3:
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2 9
The performance of a program can be improved as here we don't need to store unwanted elements
and have flexibility over the number of elements in the row.
Jagged arrays have a greater speed than multidimensional arrays and single-dimensional arrays as
traversal is faster in jagged arrays.
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2 10
CRUD in Array
ACTIVITY
• Write a Java program to enter the size of rows and columns of an array and ask user to
input the number of elements in each array. Print the array in a table format and print the
sum of all the elements
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2 13
SUMMARY
KEY TERMS
Jagged Array
3D Array
2D Array
Datatype
Identifier
POST-TEST
REFERENCES
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2 14
LEARNING OUTCOMES
RESOURCES NEEDED
WORD SEARCH
Find the following words in the puzzle. Words are hidden in different directions
APPLICATION
ECLIPSE
JAVA SWING
USER INTERFACE
WINDOW BUILDER
The Swing component set was originally created because the basic
AWT components that came with the original version of the Java libraries were
insufficient for real-world, forms-based applications. All the basic components
were there, but the existing set was too small and far too restrictive. For
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2 5
instance, you couldn’t even put an image on a button. To alleviate this situation,
the Swing component set offers replacements for each of the AWT
components. The Swing components support all the capabilities of the original
set and offer a whole lot more besides. As such, you should never need to deal
with any of the basic Abstract Windowing Toolkit component.
The javax.swing package offers classes for java swing API such as
JTextArea, JTextField, JButton, JRadioButton, JMenu, JColorChooser,
JCheckbox,etc.
Notice that the Component class is the class that the Swing classes
inherit from. Because of the functions of the object-oriented programming, you
can call methods from the Component class in a JLabel, a JTable, or JFrame.
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2 6
Every components in the java swing such as the JLabel,, JList, JButton,
are from the JComponent class which can be included to the container class.
The containers are the windows that are like the frame and messageboxes or
dialog boxes. The basic swing components are known as the basic building
blocks of any Graphical User Interface (GUI) application.
The Eclipse platform, which serves as the basis for the Eclipse IDE, is made up
of plug-ins and may be expanded by adding new plug-ins. The Eclipse platform
was created using Java, and it may be used to create integrated development
environments, rich application programs, and other tools. Every programming
for which a plug-in is available can be used as an IDE with Eclipse.
Note : To install the Eclipse IDE, click or copy the following web site
provided below and follow the instructions for installation.
http://www.eclipse.org/downloads/
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2 7
When Eclipse launches for the first time, it asks you where the workspace
folder is located. The workspace folder will house all of your data. Either
accept the default or select a different place.
• Views
• Editors (all appear in one editor area)
• Menu Bar
• Toolbar
Step 2: On the Menu bar, select the Help menu, click the "Install new Software"
> “Add” > “Archive” button and locate your downloaded Window Builder
zipped file.
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2 9
Step 3: Check all the radio buttons shown in the figure below and select on the
radio button that says "Agree" and click on the finish button.
Note: File menu > New > Package is how to create a new Java package.
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2 12
Step 4: Next, choose the package, choose the new option, click Other in the
toolbar, choose Swing Designer > JFrame, give the class a name, and press the
Finish button.
Step 5: The class is used to create an existing piece of code. Click on the
"Design tab" located at the editor's bottom to access the Window builder.
Step 6: A panel selection window for the user interface will appear. You can
freely drag and drop the UI objects on the provided sample screen and you can
click on the "Source Tab" to alter some UI related codes.
Step 7: Simply choose the components in the palette and drag them into the
sample window to add elements like the JButton and the JTextArea to the
produced sample Java swing application.
Step 8: Absolute layout is used for this application.
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2 13
Right-click on the window frame, select Set Layout from the menu that appears,
then select the proper layout for the application to set the layout to "Absolute.".
Step 9: On the palette toolbar, drag and drop a button and a text area onto the
JFrame. In the following screenshot, JButton and JTextArea have been selected
and adjusted the size of the elements.
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2 14
Step 10: By simply double clicking on each element, we may edit its attributes.
This will open the source tab and place the pointer on the selected code
elements. You can modify the source code or the properties window in the left-
side corner to change things like the backdrop, background color, and font.
Note: There are two ways for handling event on the button like button clicking
events:
• Double-click the JFrame sample's elements.
• Right-click the element and choose Add Event Handler > Action >
Action Performed.
Step 11: Simply right-click the project folder in the left navigation and select
Run As > Java Application to run the sample application.
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2 15
1) Run Eclipse IDE. Select File > New > Java Project from the menu.
2) Type the Project Name (SampleJavaProject) and leave the rest as default
values. Click Finish button at the bottom part of the window.
3) A dialog box will appear asking for module creation. In this lesson, we
will not create a module so just click “Don’t Create” button.
4) From the Project Explorer (usually at the left side of the Eclipse
window), right click on your project, in this case, SampleJavaProject >
New > Class.
5) Next step is to enter values for the creation of our Class that will contain
the main() method which serves as an entry point of our program for
execution. From the dialog box, type the name of the class (frameMain)
and put a check in public static void main(String[] args) so that the
main() method of our program will be automatically generated by
Eclipse IDE. Click the Finish button.
6) The Editor Area will automatically display the code for the class we
created (frameMain) with a given code structure for the class with its
default main() method. In the Project Explorer, you will see
frameMain.java, the java file of the class we created, saved under the
SampleJavaProject we previously created.
7) Type the following code and run the program by clicking the Run button
(CTRL+F11) in the Eclipse toolbar and observe what will happen.
8) A frame (window) with a button will appear on user’s screen upon
running the program if and only if the program does not return an error.
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2 16
Task:
1. Follow the steps in installing the ECLIPSE IDE and the WINDOWBUILDER PLUGIN from above.
2. Create a new Swing Application and copy the codes below.
3. Run the code and analyze the codes below.
4. Change the Title of the JFrame into “My Example GUI Application”.
5. Change the Button into “Click Me”.
6. Add your name in the blank label.
7. Try to play with the given codes above, change the parameters, change true to false, add buttons
or labels so that you can be familiarize with the codes.
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2 21
SUMMARY
KEY TERMS
Java
Java Swing
Eclipse
Graphical User Interface
POST-TEST
_______ 4. A graphical user interface (GUI) written in lightweight Java that may be used to build different
applications.
_______ 5. A graphical user interface (GUI) written in heavyweight Java that may be used to build different
applications.
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2 22
REFERENCES
Conrod P. & Tylee L. (2015). Learn Java™ GUI Applications A JFC Swing NetBeans Tutorial
8th Edition
Waseem M. (2022). Swing In Java : Know How To Create GUI With Examples.
https://www.edureka.co/blog/java-swing/
PAMANTASAN NG CABUYAO |YOUR COURSE TITLE HERE 1
LEARNING OUTCOMES
RESOURCES NEEDED
12 JButton in Java
JLabel in Java
19
JTextField in Java
21
PAMANTASAN NG CABUYAO |YOUR COURSE TITLE HERE 4
WORD SEARCH
Fill in the white squares with the letters, forming words with the provide
clues that may lead to the correct answer
The methods of the Component class, which are listed below, are often
used in Java Swing.
PAMANTASAN NG CABUYAO |YOUR COURSE TITLE HERE 5
JFrame in Java
import javax.swing.JTextFrame;
There are numerous constructors in the JFrame class that are used to
build new JFrames. You can make a JFrame using the following techniques:
CONSTUCTOR DESCRIPTION
You must now provide the frame’s size and the location after
establishing the JFrame. Let's look at some possible solutions.
4. Next step is to create our form by selecting JFrame. JFrame works like
the main window where components like panels, textfields, and buttons
are added to create a GUI. After selecting JFrame, click Next button.
5. Type the name of the JFrame. In our example, we set its name to
frameMain. Click Finish button.
PAMANTASAN NG CABUYAO |YOUR COURSE TITLE HERE 9
6. Click Design to view the JFrame and other components in the Palette.
The Design view is only available if WindowBuilder component is
installed.
Your digital drawing board is the Design View. You may change layout
settings, direct edit labels, add or delete components, and view the overall
development of your design. You may modify a component's properties in
the Property Pane by selecting it in the Design View. Moreover, this will
PAMANTASAN NG CABUYAO |YOUR COURSE TITLE HERE 10
make the component's selection handles active. The layout properties of the
chosen control and, for some layout managers, the layout properties of
related controls, may both be changed using selection handles. The
Property Pane offers access to all the properties that may be modified in the
Design View. Row and column headers may be discernible in the header
section, depending on the layout manager in use.
Note that when you create a JFrame object, the WindowBuilder will
automatically create a JPanel object and set its name to contentPane by default.
A JPanel is a container that allows the programmer to store component
(buttons, textfields, etc.). A class of the java.swing package that extends the
Java AWT library is the JPanel. It is renowned for being the most
straightforward type of container when developing lightweight window apps.
PAMANTASAN NG CABUYAO |YOUR COURSE TITLE HERE 11
Labels, buttons, and other panels are among the many components that are
included, and they can be combined. Most importantly, it is depicted as a
window without a title bar or border. As a result, it is essentially the most basic
container, using Flow Layout as the default layout.
In the Structure window, you can see the items which have been added
in the application window. If you click on any item, you can see the properties
of that item in the Properties window. Now if you save your changes and go to
the Source tab you will see that Eclipse has updated the source code to reflect
the changes.
The Component Tree shows the hierarchical relationship between all
of the components in the Design View. Each component in the tree shows is
icon (with potential decorators), its variable name and text label. Components
may be represented as local variables or fields, and variable names must be
unique within the scope in which the component is defined. Icon decorators are
use.
The Property Pane displays properties and events of the selected
components and provides editable text fields, lists and other controls to allow
you to edit properties and events. Select the control in the Component Tree or
on the Design View to display its properties in the Property Pane. Once visible
in the Property Pane, these values can be edited. When multiple controls are
selected, all of their shared properties are listed in the Property Pane. If not all
the selected controls have the same value for a property, the property editor in
the Property Pane will be blank.
8. Now we have created a JFrame that will be the container of our
components for our GUI.
Keep in mind that whenever we change property values of components
which are already in our JFrame, WindowBuilder is responsible for updating
its source code. At the same time, always check what specific component from
the Component Tree is selected (highlighted) whenever you change property
values to avoid confusion.
PAMANTASAN NG CABUYAO |YOUR COURSE TITLE HERE 12
JButton in Java
import javax.swing.JButton;
A JButton can be initialized by
Constructors
Methods
PAMANTASAN NG CABUYAO |YOUR COURSE TITLE HERE 13
Button Properties
Button Methods
1. From the previous discussion on how to create a JFrame with its same
settings and code, click the JButton component from the Palette >
Components Category then click inside the contentPane. This will
enable the programmer to add a JButton to the contentPane (JPanel).
Note that when you add a control from the Palette to the contentPane,
the WindowBuilder will automatically provide the corresponding code
to create the added control.
Your output should be like the above given screenshot. If you notice,
clicking the button does not do anything since we did not capture the click
event from the user. The way we can respond to user actions is through an
ActionListener that listens to user’s actions or events.
Source - An item where the event takes place is its source. The source
must tell the event's handler of what happened, according to the source.
Listener - Event handler is another name for it. It is the listener's
responsibility to respond to an occurrence. The listener is an object from
the perspective of Java implementation. The listener watches for an
event before responding. When the event has been received, the listener
processes it before returning.
PAMANTASAN NG CABUYAO |YOUR COURSE TITLE HERE 17
In order to perform Event Handling, you need to register the component with
the listener.
4. Run the program using the Run Menu or by pressing CTRL+F11. The
output should be like this when you clicked the JButton.
JLabel in Java
The JLabel, which is the most basic true Swing component, is the first
to be carefully examined. The JLabel can perform a lot more tasks than the
AWT Label, which it replaces.
Text that cannot be edited directly by the user is shown using a JLabel.
A JLabel's text can be altered in reaction to events.
JLabel Constructors
PAMANTASAN NG CABUYAO |YOUR COURSE TITLE HERE 20
JLabel Methods
The idea behind this example to display the contents of two JLabels,
lblName for displaying “JUAN DELA CRUZ” and lblCourse for displaying
“BS INFORMATION TECHNOLOGY”. We need to use a JLabel method
named setText() which accepts a String as a parameter that sets the text to be
displayed in the JLabel. We will add a JButton that will capture the Click Event
of the user and we will put the code for setting the text of the JLabels inside the
JButton’s actioPerformed() method.
PAMANTASAN NG CABUYAO |YOUR COURSE TITLE HERE 21
JTextField in Java
Constructors
Methods
The idea behind this example is to allow the user to type values for first
name and last name in the JTextFields. After entering values, the combined
values of the text entered by the user will be displayed in a JLabel following
Last Name, First Name Format upon clicking the JButton. Additionally, since
we need to capture the user’s Click Event, we will use an ActionListener for
the JButton.
PAMANTASAN NG CABUYAO |YOUR COURSE TITLE HERE 23
The above code for the JButton’s actionPerformed() method has the following
logic:
1) Get the text entered by the user from txtFirstName using the JTextField
method named getText() which returns a String (the text entered by the
user) and store temporarily in a String variable named firstName.
2) Get the text entered by the user from txtLastName using the JTextField
method named getText() which returns a String (the text entered by the
user) and store temporarily in a String variable named lastName.
3) Combine the contents of String variables lastName and firstName using +
operator to join the Strings with a “, “ in between to produce the
LastName, FirstName format and store temporarily in a String variable
named fullName.
4) Display the content of the String variable fullName in lblFullName using the
JLabel’s setText() method which accepts a String value as a parameter.
PAMANTASAN NG CABUYAO |YOUR COURSE TITLE HERE 24
LABORATORY ACTIVITY
Task:
Create a simple calculator with the basic mathematical operations that will ask the user to input two
integer number using the Window Builder designer. Provided your code below:
Program Code
PAMANTASAN NG CABUYAO |YOUR COURSE TITLE HERE 25
SUMMARY
KEY TERMS
Java
JLabel
JButton
JFrame
JTextField
JPanel
POST-TEST
____________________________ 2. AWT
____________________________ 3. JFC
____________________________ 4. WYSIWYG
PAMANTASAN NG CABUYAO |YOUR COURSE TITLE HERE 26
REFERENCES
Conrod P. & Tylee L. (2015). Learn Java™ GUI Applications A JFC Swing NetBeans Tutorial
8th Edition
LEARNING OUTCOMES
RESOURCES NEEDED
12 JOptionPane in Java
15 SUMMARY
15 Key Terms
Post-Test
References
17
PAMANTASAN NG CABUYAO |YOUR COURSE TITLE HERE 4
WORD SEARCH
JTextArea in Java
JTextField's text field control can only show a single line of text. The
JTextArea is a similar control that supports several lines of text (in a single
font). This control, like the JTextField, can be used to show data that has been
initialized when the frame is built, input by a user during run-time, or assigned
by code. The text that displays can be modified.
import javax.swing.JTextArea;
In the given example, notice that the JTextArea component was not able to
provide vertical and horizontal scrolling by default which makes the text as
highlighted below appears to be cut.
To solve the problem, we need to use a container component that will provide
the automatic vertical and horizontal scrolling within the JTextArea that will
be explained by the next example.
At the same time, in the sample execution, the first word entered in txtHobby
(JTextField) is the word “Read”. Clicking the ADD button appends the text
from the txtHobby (JTextField) to txtAHobbies.(JTextArea). This is through
adding an ActionListener in the JButton since we need to capture the user’s
click event and then adding the code for appending the text in the txtAHobbies.
We have a small issue from the given example. That is, every time we will
append a new text in txtAHobbies (JTextArea), the text will just appear at the
end of the text existing in txtAHobbies. What if we want to always append a
new text beginning with a new line to make it more presentable? The answer
PAMANTASAN NG CABUYAO |YOUR COURSE TITLE HERE 9
is to add a “\n” or a new line character at the end of the text entered in txtHobby
(JTextField).
JPasswordField in Java
A text component designed just for entering passwords is the object of
a JPasswordField class. One line of text may be edited with it. It comes from
the class JTextField.
PAMANTASAN NG CABUYAO |YOUR COURSE TITLE HERE 10
Methods of JPasswordField
import javax.swing.JPasswordField;
PAMANTASAN NG CABUYAO |YOUR COURSE TITLE HERE 11
JPasswordField Example
PAMANTASAN NG CABUYAO |YOUR COURSE TITLE HERE 12
JOptionPane in Java
A class called JOptionPane is used to create dialog boxes. JOptionPane
is a component of Java Swing, which is used to develop applications using
windows. JOptionPane is a Java Swing component that focused only on dialog
boxes. Any sort of dialog box may be used, including input, message, and
PAMANTASAN NG CABUYAO |YOUR COURSE TITLE HERE 13
confirm dialog boxes. These dialog windows can be used to provide the user
information or to ask for input.
Methods
showInputDialog It requests input from the user.
import javax.swing.JOptionPane;
showMessageDialog()
PAMANTASAN NG CABUYAO |YOUR COURSE TITLE HERE 14
showInputDialog()
showConfirmDialog()
PAMANTASAN NG CABUYAO |YOUR COURSE TITLE HERE 15
1. Create a simple login application program where a user will enter his name and password. The
application will then preview the username in a message dialog. Be creative with the design by
changing the properties of each components
Screen record your code and output and send it to our LMS
PAMANTASAN NG CABUYAO |YOUR COURSE TITLE HERE 16
SUMMARY
Just one line of text may be displayed in the text field control of JTextField. A
comparable control that accommodates many lines of text is the JTextArea (in
a single font). You can change the wording that appears on screen.
The object of a JPasswordField class is a text field intended just for password
entry. It allows for editing of one line of text. It is derived from the JTextField
class.
KEY TERMS
Java
Java Swing
JTextArea
JPasswordField
JOptionPane
POST-TEST
REFERENCES
Conrod P. & Tylee L. (2015). Learn Java™ GUI Applications A JFC Swing NetBeans Tutorial
8th Edition
LEARNING OUTCOMES
RESOURCES NEEDED
Summary
15 Key Terms
16 Reference
PAMANTASAN NG CABUYAO |YOUR COURSE TITLE HERE 4
WORD SCRAMBLE
Scramble the letters and provide the correct answer on the blank spaces
below.
JCheckbox in Java
Methods
import javax.swing.JCheckbox;
PAMANTASAN NG CABUYAO |YOUR COURSE TITLE HERE 6
Example 1
This example shows how to use a JCheckBox component in a Swing
Application which allows a user to check or uncheck the JCheckBoxes
In the given example, notice that the even without the code for the click
event in the JCheckBoxes, the program was able to show the appearance of
checking and unchecking the JCheckBoxes. Event Listener is not needed by
default if you want to show the process of toggling the JCheckBoxes
Example 2
e) method. The “e” from the method parameter allows us to access the
getStateChange() method of the ItemEvent Class which returns 1 if the status
of the JCheckBox is checked, 2 if unchecked. By using a ternary operator (?),
we are able to conditionally check the value returned by the getStateChange()
method and return the Strings “Checked” and “Uncheked” in one single line
including the lblResult.setText() method.
Example 3
JRadioButton in Java
Methods
Example
This example shows how to use a JRadioButton component in a Swing Application which allows
a user to select or unselect the JRadioButtons. A ButtonGroup class is used so that only one single
radio button can be selected at a time and this is the difference of JRadioButton from JCheckBox
components. If in case you will need to create more than one radio button group for selection, lets
us say, one button group for the SEX selection, another for CIVIL STATUS selection, then two
separate ButtonGroup objects will needed.
PAMANTASAN NG CABUYAO |YOUR COURSE TITLE HERE 12
Make sure that the javax.swing.ButtonGroup is imported on top of the code to avoid errors in
creating a new ButtonGroup object.
PAMANTASAN NG CABUYAO |YOUR COURSE TITLE HERE 13
Create a simple pizza menu option using the JRadioButton and JCheckBox. Add a button that
will show if no order has been created or will show the summary of your order in a Message Dialogbox.
Example Output:
PAMANTASAN NG CABUYAO |YOUR COURSE TITLE HERE 14
SUMMARY
JCheckBox has two options: chosen and deselected. It informs the user of its
status. JCheckBox is a checkbox implementation. JCheckBox inherits the
class of JToggleButton.
KEY TERMS
Java
Java Swing
JCheckbox
JRadioButton
POST-TEST
Directions: In the space provided, write TRUE the statement is correct, otherwise write FALSE.
REFERENCES
Conrod P. & Tylee L. (2015). Learn Java™ GUI Applications A JFC Swing NetBeans Tutorial
8th Edition
Tutorialspoint, “What are the differences between JRadioButton and JCheckBox in Java?”
LEARNING OUTCOMES
RESOURCES NEEDED
Pre-Activity Title
2. It is used to create a swing component that 3
displays a list of objects that allows user
to represent list of items.
What is JComboBox?
4
3. It is used to create a vertical and horizontal
scrollbar of a swing component. 28 What is JList?
48 Pos-Test
WORD SEARCH
Find the following words in the puzzle. Words are hidden in different directions
JCOMBOX
JLIST
JSCROLLBAR
USER INTERFACE
OBJECT
WHAT IS JComboBox?
If you make the combo box editable, then the combo box includes an
editable field into which the user can type a value depending on the choice of
the programmer.
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
10
JComboBox Examples
cboCountries
contentPane
JPanel Specifications
Property Value
Variable contentPane
Layout absolute
JComboBox Specifications
Property Value
Variable cboCountries
X-axis location 174
Bounds Y-axis location 13
Width 126
Height 31
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
Java JComboBox Program Code: 10
cboCountries
contentPane
JPanel Specifications
Property Value
Variable contentPane
Layout absolute
JComboBox Specifications
Property Value
Variable cboCountries
X-axis location 174
Bounds Y-axis location 13
Width 126
Height 31
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
10
Java JComboBox Program Code:
Note: This example simplifies the way on how to put items inside the
JComboBox. We first define an Array of Strings named countries that
will hold the items for {“USA”, “PH”, “AUS”, “CAN”, “IND”}. By
doing this, we can easily identify each item through index values
(starting from 0). And by simply adding the Array of Strings to the
constructor of the JComboBox, it will be automatically available for
selection upon running the program.
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
Java JComboBox Example 3: 10
cboCountries
btnShow
contentPane
JPanel
Specifications
Property Value
Variable contentPane
Layout absolute
JComboBox
Specifications
Property Value
Variable cboCountries
X-axis location 174
Bounds Y-axis location 13
Width 126
Height 31
JButton Specifications
Property Value
Variable btnShow
X-axis location 112
Bounds Y-axis location 95
Width 85
Height 21
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
Java JComboBox Program Code: 10
cboCountries
lblCoun
try
contentPane
JPanel Specifications
Property Value
Variable contentPane
Layout absolute
JComboBox Specifications
Property Value
Variable cboCountries
X-axis location 174
Bounds Y-axis location 13
Width 126
Height 31
JLabel Specifications
Property Value
Variable lblCountry
Text
X-axis location 20
Bounds Y-axis location 97
Width 280
Height 31
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
Java JComboBox Program Code: 10
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
Note: You can also change the previous code for identifying the 10
currently selected item by using the getSelectedIndex() method
instead of using the getSelectedItem() method. The use of the
getSelectedIndex() method is much safer because he condition will
be based on the index number of the currently selected item therefore
eliminating the comparison of Strings in the if condition. Consider
the updated code below:
cboCountries
lblCountry
contentPanE
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
Specifications of the JComboBox: 10
JPanel Specifications
Property Value
Variable contentPane
Layout absolute
JComboBox Specifications
Property Value
Variable cboCountries
X-axis location 174
Bounds Y-axis location 13
Width 126
Height 31
JLabel Specifications
Property Value
Variable lblCountry
Text
X-axis location 20
Bounds Y-axis location 97
Width 280
Height 31
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
Java JComboBox Program Code: 10
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
10
cboCountries
txtNewCountry
btnAdd
contentPane
lblItemCount
JPanel Specifications
Property Value
Variable contentPane
Layout absolute
JComboBox Specifications
Property Value
Variable cboCountries
X-axis location 174
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
Y-axis location 13 10
Bounds Width 162
Height 31
JTextField Specifications
Property Value
Variable txtNewCountry
Text
X-axis location 204
Bounds Y-axis location 72
Width 132
Height 34
JButton Specifications
Property Value
Variable btnAdd
Text ADD
X-axis location 137
Bounds Y-axis location 120
Width 85
Height 31
JLabel Specifications
Property Value
Variable lblItemCount
Text Item Count :
X-axis location 89
Bounds Y-axis location 188
Width 178
Height 13
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
Java JComboBox Program Code: 10
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
10
btnRemoveSelectedItem btnRemoveAllItems
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
Sample output after clicking the REMOVE SELECTED ITEM 10
BUTTON while the 3rd item in the JComboBox is selected:
Note: Notice that the 3rd item from the list (“AUS”) is immediately
deleted after clicking the REMOVE SELECTED ITEM Button. At
the same time, the item count displayed is updated.
JPanel Specifications
Property Value
Variable contentPane
Layout absolute
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
10
JComboBox Specifications
Property Value
Variable cboCountries
X-axis location 174
Bounds Y-axis location 13
Width 162
Height 31
JTextField Specifications
Property Value
Variable txtNewCountry
Text
X-axis location 204
Bounds Y-axis location 72
Width 132
Height 34
JButton Specifications
Property Value
Variable btnAdd
Text ADD
X-axis location 137
Bounds Y-axis location 120
Width 85
Height 31
JLabel Specifications
Property Value
Variable lblItemCount
Text Item Count :
X-axis location 89
Bounds Y-axis location 188
Width 178
Height 13
JButton Specifications
Property Value
Variable btnRemoveSelectedItem
Text REMOVE SELECTED ITEM
X-axis location 378
Bounds Y-axis location 13
Width 234
Height 31
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
JButton Specifications 10
Property Value
Variable btnRemoveAllItems
Text REMOVE ALL ITEMS
X-axis location 378
Bounds Y-axis location 72
Width 234
Height 31
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
Java JComboBox Program Code: 10
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
10
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
10
WHAT IS JList?
JList Examples
import javax.swing.DefaultListModel;
listFruits
contentPane
lblItemCount
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
Specifications of the JList: 10
JPanel Specifications
Property Value
Variable contentPane
Layout absolute
JList Specifications
Property Value
Variable listFruits
selectionMode SINGLE_SELECTION
X-axis location 132
Bounds Y-axis location 20
Width 172
Height 79
JLabel Specifications
Property Value
Variable lblItemCount
Text
X-axis location 105
Bounds Y-axis location 120
Width 218
Height 21
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
Java JList Program Code: 10
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
10
Sample output upon clicking the SHOW button while the “Orange”
item is selected.
btnShow
JPanel Specifications
Property Value
Variable contentPane
Layout absolute
JList Specifications
Property Value
Variable listFruits
selectionMode SINGLE_SELECTION
X-axis location 132
Bounds Y-axis location 20
Width 172
Height 79
JLabel Specifications
Property Value
Variable lblItemCount
Text
X-axis location 105
Bounds Y-axis location 120
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
Width 218 10
Height 21
JButton Specifications
Property Value
Variable btnShow
Text SHOW
X-axis location 172
Bounds Y-axis location 161
Width 85
Height 21
JButton Specifications
Property Value
Variable btnShow
Text SHOW
X-axis location 172
Bounds Y-axis location 161
Width 85
Height 21
Note: Add the given lines of code to provide functionality for the
SHOW Button. The getElementAt(int index) is a built in method of
the ListModel used to retrieve an element in the model using a valid
index position. Since in this example we need to retrieve the currently
selected (highlighted) item from the JList, we get the index of the
currently selected item in the JList using the getSelectedIndex()
method which returns the valid index of the currently selected item
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
in the JList (returns 2 since “Orange” is the the 3rd position within 10
the list).
Sample output upon adding 4 new items in the JList using the ListModel.
JPanel Specifications
Property Value
Variable contentPane
Layout absolute
JScrollPane Specifications
Property Value
Variable scrollPane
X-axis location 219
Bounds Y-axis location 23
Width 190
Height 198
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
10
JList Specifications
Property Value
Variable listFruits
selectionMode SINGLE_SELECTION
JLabel Specifications
Property Value
Variable lblItemCount
Text
X-axis location 219
Bounds Y-axis location 161
Width 191
Height 21
JButton Specifications
Property Value
Variable btnAdd
Text ADD
X-axis location 10
Bounds Y-axis location 85
Width 85
Height 28
JButton Specifications
Property Value
Variable btnShow
Text SHOW
X-axis location 125
Bounds Y-axis location 85
Width 85
Height 28
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
Java JList Program Code: 10
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
10
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
Note: Modify the code to include a REMOVE Button using the GE
10
removeElementAt(int index) method of the ListModel to remove the currently
selected item from the JList. Additionally, update the number of items
displayed in the JLabel every time the user clicks the REMOVE Button.
Sample output upon clicking the SHOW Button while multiple items are
selected:
JPanel Specifications
Property Value
Variable contentPane
Layout absolute
JScrollPane Specifications
Property Value
Variable scrollPane
X-axis location 219
Bounds Y-axis location 23
Width 190
Height 198
JList Specifications
Property Value
Variable listFruits
selectionMode MULTIPLE_INTERVAL_SELEC
TION
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
JLabel Specifications GE
Property Value 10
Variable lblItemCount
Text
X-axis location 219
Bounds Y-axis location 161
Width 191
Height 21
JButton Specifications
Property Value
Variable btnAdd
Text ADD
X-axis location 10
Bounds Y-axis location 85
Width 85
Height 28
JButton Specifications
Property Value
Variable btnShow
Text SHOW
X-axis location 125
Bounds Y-axis location 85
Width 85
Height 28
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
Java JList Program Code: GE
10
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
10
Note: Modify the code to include a REMOVE MULTIPLE Button using the
removeElementAt(int index) method of the ListModel to remove the all items
selected from the JList in a single click. Additionally, update the number of
items displayed in the JLabel every time the user clicks the REMOVE
MULTIPLE Button.
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
10
WHAT IS JScrollBar?
JScrollBar Examples
scrollBarVertical
scrollBarHorizontal
lblScrollBarVertical
lblScrollBarHorizontal
contentPane
JPanel Specifications
Property Value
Variable contentPane
Layout absolute
JScrollBar Specifications
Property Value
Variable scrollBarVertical
X-axis location 93
Bounds Y-axis location 20
Width 31
Height 124
maximum 100
minimum 0
orientation VERTICAL
JScrollBar Specifications
Property Value
Variable scrollBarHorizontal
X-axis location 231
Bounds Y-axis location 72
Width 195
Height 29
maximum 100
minimum 0
orientation HORIZONTAL
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
JLabel Specifications GE
Property Value 10
Variable lblScrollBarVertical
Text
X-axis location 21
Bounds Y-axis location 179
Width 185
Height 23
JLabel Specifications
Property Value
Variable lblScrollBarHorizontal
Text
X-axis location 241
Bounds Y-axis location 179
Width 185
Height 23
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
Java JList Program Code: 10
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
10
Task:
1. Create a vertical and horizontal scrollbar. The position of the slider will be updated whenever the
position of the slider changed.
2. Create a new Swing Application and copy the codes below.
3. Run the code and analyze the codes below.
4. Change the Title of the JScrollBar into “This is ScrollBar”.
5. Change the value of the label.setSize into 450,150.
6. Add your name in the blank label.
import javax.swing.*;
import java.awt.event.*;
class ScrollBarExample
ScrollBarExample(){
label.setHorizontalAlignment(JLabel.CENTER);
label.setSize(400,100);
s.setBounds(100,100, 50,100);
f.add(s); f.add(label);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
s.addAdjustmentListener(new AdjustmentListener() {
});
new ScrollBarExample();
}}
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
10
SUMMARY
JList class represents a list of text items. The list of text items can be set up
so that the user can choose either one item or multiple items. It inherits
JComponent class.
JComboBox is the object of Choice class that is used to show popup menu of
choices. Choice selected by user is shown on the top of a menu. It inherits
JComponent class.
KEY TERMS
Java
JComboBox
JList
JScrollBar
Object
POST-TEST
2. Creates a scrollbar with the specified orientation and the initial values.
Conrod P. & Tylee L. (2015). Learn Java™ GUI Applications A JFC Swing NetBeans Tutorial
8th Edition
Waseem M. (2022). Swing In Java : Know How To Create GUI With Examples.
https://www.edureka.co/blog/java-swing/
PAMANTASAN NG CABUYAO |YOUR COURSE TITLE HERE
PA
GE
10
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2 1
14
LEARNING OUTCOMES
RESOURCES NEEDED
Pre-Activity Title
3
2. It is used to create a swing component that
provides a scrollable view in java.
What is JTable?
4
3. It is used to create an empty cell table that is
created and initialized with the default data 5 Examples of JTable
model.
19 What is JScrollPane?
4. It is used to create a scroll pane with the specified
component.
21 Pos-Test
WORD SCRAMBLE
Scramble the letters and provide the correct answer on the blank spaces below.
BTLEAJ
SORLJNLEPAC
ATELB
OBUNTT
NENPOCOTM
WHAT IS JTable?
The JTable class is a part of Java Swing Package and is generally used
to display or edit two-dimensional data that is having both rows and columns.
It is similar to a spreadsheet. This arranges data in a tabular form.
JTable Examples
contentPane
scrollPane
tblStudent
lblRowCount
JPanel Specifications
Property Value
Variable contentPane
Layout absolute
JScrollPane Specifications
Property Value
Variable scrollPane
X-axis location 25
Bounds Y-axis location 29
Width 387
Height 89
JTable Specifications
Property Value
Variable tblStudent
JLabel Specifications
Property Value
Variable lblRowCount
Text
X-axis location 298
Bounds Y-axis location 128
Width 114
Height 17
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
Java JTable Program Code: 10
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
10
Note: In the given example, notice that the JScrollPane automatically adds a
vertical scrollbar depending on the size of the JScrollPane (if it can display all
the contents by default) and the number of rows from the array of data.
Sample output upon entering values in txtName and txtAge while performing
the NEW BUTTON operation:
Sample output upon clicking the SAVE BUTTON while performing NEW
BUTTON Operation:
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
Sample output upon selecting a row in tblPersonalInfo while performing the GE
10
UPDATE BUTTON operation:
Sample output upon changing the content of txtName and txtAge while
performing the UPDATE BUTTON operation:
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
Sample output upon clicking the SAVE BUTTON while performing UPDATE GE
10
BUTTON Operation:
Sample output upon clicking the SAVE BUTTON while performing DELETE
BUTTON Operation:
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
Specifications of the JTable: 10
JPanel Specifications
Property Value
Variable contentPane
Layout absolute
JLabel Specifications
Property Value
Variable lblId
Text
X-axis location 124
Bounds Y-axis location 33
Width 46
Height 13
JTextField Specifications
Property Value
Variable txtName
Text
X-axis location 125
Bounds Y-axis location 66
Width 395
Height 19
JTextField Specifications
Property Value
Variable txtAge
Text
X-axis location 125
Bounds Y-axis location 95
Width 96
Height 19
JScrollPane Specifications
Property Value
Variable scrollPane
X-axis location 30
Bounds Y-axis location 154
Width 490
Height 210
JTable Specifications
Property Value
Variable tblPersonalInfo
JLabel Specifications
Property Value
Variable lblRecordCount
Text
X-axis location 417
Bounds Y-axis location 374
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
Width 103 PA
Height 13 GE
10
JButton Specifications
Property Value
Variable btnNew
Text NEW
X-axis location 30
Bounds Y-axis location 393
Width 85
Height 21
JButton Specifications
Property Value
Variable btnUpdate
Text UPDATE
X-axis location 30
Bounds Y-axis location 422
Width 85
Height 21
JButton Specifications
Property Value
Variable btnDelete
Text DELETE
X-axis location 30
Bounds Y-axis location 449
Width 85
Height 21
JButton Specifications
Property Value
Variable btnSave
Text SAVE
X-axis location 153
Bounds Y-axis location 393
Width 85
Height 21
JButton Specifications
Property Value
Variable btnCancel
Text SAVE
X-axis location 153
Bounds Y-axis location 422
Width 85
Height 21
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
Java JTable Program Code: 10
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
10
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
10
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
10
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
10
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
Note: Try to modify the code so that the program should be more user-friendly 10
by not allowing the user to click NEW, UPDATE, and DELETE Button when
the user is currently in the process of performing a selected operation. Only the
SAVE and CANCEL Button should be enabled if the user is not yet done in a
particular operation. Additionally, consider disabling the txtName and txtAge
while the user is in the DELETE operation.
WHAT IS JScrollPane?
JScrollPane Example
Task:
1. Create a JScrollPane with JTable displaying the Top 10 Countries with Most Forested Area. Your table
must include Rank, County, and Forested Area (km sq). Any values are acceptable in the table.
Example Output:
2. Try to play with the given codes above, change the parameters, change true to false, setSize and
BorderLayout so that you can be familiarize with the codes.
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
10
SUMMARY
The JTable is used to display and edit regular two-dimensional tables of cells.
The JTable has many facilities that make it possible to customize its rendering
and editing but provides defaults for these features so that simple tables can
be set up easily.
KEY TERMS
JTable
JScrollPane
contentPane
DefaultTableModel Class
JLabel
addColumns()
POST-TEST
1. Creates a scroll pane with a component parameter, when present, sets the scroll pane's client.
15
LEARNING OUTCOMES
RESOURCES NEEDED
Pre-Activity Title
3
2. It is used to create an object that adds a simple
labeled menu item.
What is Java JMenuBar,
4 JMenu and JMenuItem?
WORD SEARCH
Find the following words in the puzzle. Words are hidden in different directions.
JMENUITEM
JPOPUPMENU
JMENU
JSEPARATOR
JTOOLBAR
The object of JMenuItem class adds a simple labeled menu item. The
items used in a menu must belong to the JMenuItem or any of its subclass.
Sample output upon loading FRAME MAIN and clicking the ACCOUNT Menu:
mnAccount
menuBar
mntmLogIn
mntmLogOut
mntmExit
separator
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
Specifications of the JMenuBar, JMenu and JMenuItem Object: 10
frameMain Class
JFrame Specifications
Property Value
DefaultCloseOperation EXIT_ON_CLOSE
Title SIMPLE CRUD APPLICATION
JPanel Specifications
Property Value
Variable contentPane
Layout absolute
JMenuBar Specifications
Property Value
Variable menuBar
JMenu Specifications
Property Value
Variable mnAccount
JMenuItem Specifications
Property Value
Variable mntmLogIn
JMenuItem Specifications
Property Value
Variable mntmLogOut
JMenuItem Specifications
Property Value
Variable mntmExit
JSeparator Specifications
Property Value
Variable separator
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
Java JMenuBar, JMenu and JMenuItem Program Code: 10
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
Note: Notice that in the given example, the JMenuBar object served as a 10
container for the JMenu for Account then the JMenu Account served as the
menu to be clicked so that the drop down of JMenuItem objects for Log In, Log
Out and Exit will appear an be available as choices for the user to select. A
JSeparator object is included to demonstrate its use which is to separate
JMenuItem objects within a JMenu. Additionally, the frameMain is loaded as
a maximized window.
Sample output upon loading FRAME MAIN and clicking the ACCOUNT Menu:
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
Sample output upon clicking the LOG IN Menu Item, frameLogIn will appear: GE
10
Sample output upon entering correct username [admin] and password [admin] in
frameLogIn:
Sample output upon clicking the Log In Button in frameLogIn with correct username
[admin] and password [admin] entered:
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
Sample output upon successful Log In, the LOG IN Menu Item is disabled, while LOG GE
10
OUT Menu Item is enabled:
Sample output upon clicking the LOG OUT Menu Item, a confirmation message will
appear:
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
Sample output upon successful Log Out, a messagedialog will appear: GE
10
Sample output upon successful Log Out, the LOG IN Menu Item is enabled, while
LOG OUT Menu Item is disabled:
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
Sample output upon clicking the EXIT Menu Item, a confirmation message will GE
10
appear:
frameMain Class
JFrame Specifications
Property Value
DefaultCloseOperation EXIT_ON_CLOSE
Title SIMPLE CRUD APPLICATION
JPanel Specifications
Property Value
Variable contentPane
Layout absolute
JMenuBar Specifications
Property Value
Variable menuBar
JMenu Specifications
Property Value
Variable mnAccount
JMenuItem Specifications
Property Value
Variable mntmLogIn
JMenuItem Specifications
Property Value
Variable mntmLogOut
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
JMenuItem Specifications GE
Property Value 10
Variable mntmExit
JSeparator Specifications
Property Value
Variable separator
FrameChild Class
JFrame Specifications
Property Value
DefaultCloseOperation DISPOSE_ON_CLOSE
Title LOG IN
JPanel Specifications
Property Value
Variable contentPane
Layout absolute
JTextField Specifications
Property Value
Variable txtUserName
Text
X-axis location 111
Bounds Y-axis location 34
Width 157
Height 20
JPassword Specifications
Property Value
Variable txtPassword
Text
X-axis location 111
Bounds Y-axis location 70
Width 157
Height 23
JButton Specifications
Property Value
Variable btnLogIn
Text Log In
X-axis location 26
Bounds Y-axis location 110
Width 89
Height 23
JButton Specifications
Property Value
Variable btnCancel
Text Cancel
X-axis location 179
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
Bounds Y-axis location 110 PA
Width 89 GE
Height 23 10
WHAT IS JPopupMenu?
Sample output upon loading FRAME MAIN and clicking the FORM using the RIGHT
MOUSE BUTTON:
popupMenu
mntmCut
mntmCopy
mntmPaste
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
Sample output upon clicking the COPY JMenuItem: GE
10
lblOperation
JFrame Specifications
Property Value
Title JPopupMenu Example
JPanel Specifications
Property Value
Variable contentPane
Layout absolute
JPopupMenu Specifications
Property Value
Variable popupMenu
JMenuItem Specifications
Property Value
Variable mntmCut
JMenuItem Specifications
Property Value
Variable mntmCopy
JMenuItem Specifications
Property Value
Variable mntmPaste
JLabel Specifications
Property Value
Variable lblOperation
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
Java frameMain Class Program Code: GE
10
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
10
Note: Notice that in the given example, the JLabel will display text depending on
the selected JMenuItem of the JPopupMenu.
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
10
WHAT IS JCheckBoxMenuItem?
Sample output upon loading FRAME MAIN and clicking the FILE MENU:
mnFile
menuBar
mntmOpen
chckbxmntmOption
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
Specifications of the JCheckBoxMenuItem: GE
10
frameCheckBoxMenuItem Class
JFrame Specifications
Property Value
Title JCheckBoxMenuItem Example
JPanel Specifications
Property Value
Variable contentPane
Layout absolute
JMenuBar Specifications
Property Value
Variable menuBar
JMenu Specifications
Property Value
Variable mnFile
JMenuItem Specifications
Property Value
Variable mntmOpen
JCheckBoxMenuItem Specifications
Property Value
Variable chckbxmntmOption
JLabel Specifications
Property Value
Variable lblOperation
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
Java frameCheckBoxMenuItem Class Program Code: 10
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
WHAT IS JToolBar? 10
JToolBar Examples
This example shows how use and implement a JToolbar with JButtons
and a JComboBox with items as its sample options for users to select.
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
Sample output upon loading FRAME MAIN: GE
10
toolBar
btnHelp
cboItems
btnEdit
btnFile
frameToolBar Class
JFrame Specifications
Property Value
Title JToolBar Example
JPanel Specifications
Property Value
Variable contentPane
Layout absolute
JToolBar Specifications
Property Value
Floatable false
Variable toolBar
JButton Specifications
Property Value
Text FILE
Variable btnFile
JButton Specifications
Property Value
Text EDIT
Variable btnEdit
JComboBox Specifications
Property Value
Variable cboItems
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
10
JButton Specifications
Property Value
Text HELP
Variable btnHelp
Task:
1. Create a Java JMenuBar, JMenu, and JMenuItem displaying the Menu Bar.
2. In create a menu, you have to construct a menu bar and attach it to the frame. Then add a menu
named file to the menu bar.
3. Lastly, create a menu item named new and add to the file menu.
Example Output:
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
SUMMARY 10
A pull-down menu component that is presented from the menu bar is the
JMenu class's object.
The MenuBar on the window or frame is displayed using the JMenuBar class.
There could be multiple menus.
A CheckBoxMenuItem may be accompanied by text, a visual icon, or both.
You can pick or deselect a menu item.
Anywhere else a menu should appear and you can use a JPopupMenu.
You can arrange additional elements, most frequently buttons with icons in a
row or column, using the JToolBar container. A handy component for
displaying frequently used actions or controls is offered by JToolBar.
KEY TERMS
JMenu
JMenuItem
JToolBar
JPopupMenu
JCheckBoxMenuItem
JSeparator
POST-TEST
1. Creates a container that allows to group other components, usually buttons with icons in a row or column.
16
LEARNING OUTCOMES
RESOURCES NEEDED
Pre-Activity Title
3
2. It is used to create an empty spinner with an
What is Java
initial value set to zero and has no
constraints.
4 JProgressBar?
SPRSOGRAJBER
RSEJNNIP
IDERLJS
JRETE
NELPAJ
JProgressBar Examples
frameSplashScreen Class
lblImage
lblLoadingText
progressBar
frameSplashScreen Class
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
10
Sample Project Folder:
JFrame Specifications
Property Value
Title SAMPLE APPLICATION
JPanel Specifications
Property Value
Variable contentPane
Layout absolute
JFrame Specifications
Property Value
Undecorated true
JPanel Specifications
Property Value
Variable contentPane
Layout absolute
JLabel Specifications
Property Value
Variable lblLoadingText
Text Loading…
JProgressBar Specifications
Property Value
Variable progressBar
Maximum 100
Minimum 0
Orientation Horizontal
StringPainted true
Value 0
JLabel Specifications
Property Value
Variable lblImage
Text
VerticalAlign Center
x 0
Bounds y 0
width 800
height 600
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
Java frameMain Class Program Code: 10
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
Java frameSplashScreen Class Program Code: 10
WHAT IS JSpinner?
?
A single line input field serves as the object of the JSpinner class and
enables the user to choose a number or object value from an ordered list. This
object can be type manually in a legal data going into text field of the class.
The JSpinner is ideal object because it does not need a drop-down list
and has an element of an upward and a downward arrow.
Sample output upon loading FRAME MAIN and clicking the INCREASE
button of the JSpinner:
spinner
lblDisplay
JFrame Specifications
Property Value
Title JSpinner Example
JPanel Specifications
Property Value
Variable contentPane
Layout absolute
JSpinner Specifications
Property Value
Variable spinner
Number type:
Initial Value: 0
Model Minimum: 0
Maximum: 100
Step Size: 1
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
JLabel Specifications 10
Property Value
Variable lblDisplay
Note: Notice that in the given example, the JLabel will display text whenever
the user updates the value displayed in the JSpinner by clicking its up and
down arrow buttons.
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
10
WHAT IS Java JSlider?
?
The JSlider Class is used to create a slider and by using this the user
can choose a value from a predetermined range by using this class and only
those locations are permitted for the knob.
This example shows how use and implement a JSlider object by setting
the default orientation, minimum value, maximum value and the initial value.
A Change Listener is used to update the text displayed in a JLabel based on
the value set in the JSlider.
slider
lblDisplay
JFrame Specifications
Property Value
Title JSlider Example
JPanel Specifications
Property Value
Variable contentPane
Layout absolute
JSlider Specifications
Property Value
Variable Slider
MajorTickSpacing 10
Maximum 50
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
Minimum 0 PA
MinorTickSpacing 2 GE
Orientation Horizontal 10
PaintLabels true
PaintTicks true
PaintTrack true
Value 25
JLabel Specifications
Property Value
Variable lblDisplay
Note: Notice that in the given example, the JLabel will display text whenever
the user updates the value displayed in the JSlider by sliding the slider button.
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
10
WHAT IS JTree?
JTree Examples
This example shows how use and implement the JTree class in a simple
Java Swing Application.
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
Sample output upon loading FRAME MAIN: GE
10
tree
JFrame Specifications
Property Value
Title JTree Example
JPanel Specifications
Property Value
Variable contentPane
Layout absolute
JTree Specifications
Property Value
Variable tree
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
Java frameJTree Class Program Code: GE
10
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
10
WHAT IS JTabbedPane?
The JTabbePane lets you create a user switch between a certain page by
simply clicking the tab and also for users to input data into the application.
Multiple components, such as panels, can share the same space when using the
JTabbedPane class.
JTabbedPane Examples
tabbedPane
panelCollege
lblCollege
tabbedPane
panelCourse
lblCourse
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
Sample output upon clicking the Student Management tab: GE
10
tabbedPane
panelStudent
lblStudent
JFrame Specifications
Property Value
Title JTabbedPane Example
JPanel Specifications
Property Value
Variable contentPane
Layout absolute
JTabbedPane Specifications
Property Value
Variable tabbedPane
Tab Layout Policy WRAP_TAB_LAYOUT
Tab Placement Top
JPanel Specifications
Property Value
Variable panelCollege
Layout absolute
Tab Title College Management
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
JPanel Specifications 10
Property Value
Variable panelCourse
Layout absolute
Tab Title Course Management
JPanel Specifications
Property Value
Variable panelStudent
Layout absolute
Tab Title Student Management
JLabel Specifications
Property Value
Variable lblCollege
Text COLLEGE
JLabel Specifications
Property Value
Variable lblCourse
Text COURSE
JLabel Specifications
Property Value
Variable lblStudent
Text STUDENT
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
Java frameMain Class Program Code: GE
10
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
10
WHAT IS JDialog?
JDialog Examples
This example shows how use and implement a Custom JDialog Class
by inheriting the JDialog Class. A frameMain Class object (inherits the JFrame
Class) is used as the main window with a JButton object that will open the
object of the Custom Dialog Class to be created. The Custom JDialog object
will be set as a MODAL of the frameMain Class object so that the user will not
be allowed to interact with the main window except when the user already
closes the Custom JDialog object.
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
Sample output upon loading FRAME MAIN: GE
10
btnOpen
frameMain
Sample output upon clicking the Open Dialog Button of the FRAME MAIN:
dialogSample
contentPanel
lblDialog
btnOk
btnCancel
buttonPane
JPanel Specifications
Property Value
Variable contentPane
Layout absolute
JButton Specifications
Property Value
Variable btnOpen
x 25
Bounds y 22
width 113
height 29
Text Open Dialog
JDialog Specifications
Property Value
DefaultCloseOperation HIDE_ON_CLOSE
Modal true
Resizable false
Title SAMPLE DIALOG BOX
JPanel Specifications
Property Value
Variable contentPanel
Layout absolute
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
JPanel Specifications PA
Property Value GE
Variable buttonPane 10
Layout FlowLayout
Note: Notice that in the given example, clicking outside the Custom Dialog
object will not be possible since we set the dialogSample Class as a MODAL
of the frameMain Class object upon user’s click on the Open Dialog Button.
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
10
WHAT IS JToggleButton?
To describe buttons that can be toggled ON and OFF, you can use a
JToggleButton, which is an extension of AbstractButton. The toggle button
toggles between being pressed and being unpressed when the user presses it.
JToggleButton Examples
Sample output upon loading FRAME MAIN, the JToggleButton is not selected, that
is why “OFF” is the text displayed:
tglbtnOnOff
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
Sample output upon clicking the JToggleButton, the JToggleButton becomes selected, GE
10
that is why “ON” is the text displayed:
JFrame Specifications
Property Value
Title JToggleButton Example
JPanel Specifications
Property Value
Variable contentPane
Layout absolute
JToggleButton Specifications
Property Value
Variable tglbtnOnOff
x 162
Bounds y 114
width 104
height 140
Selected False
Text OFF
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
Java JToggleBar Class Program Code: GE
10
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
10
Task:
1. Create a Java JTabbedPane, add the Tab pane in JFrame then add components on each tab
2. Create a Java Class named AddJTabbedPane.java in which I describe the user interface.
3. In the created JFrame add two tabs named 'Fruit' and 'Vegetable' onto the JFrame.
4. Lastly, make sure that whenever you switch to Vegetable tab then the vegetable's name will be
displayed.
Example Output:
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
SUMMARY 10
A horizontal progress bar with the supplied minimum and maximum value is
created using JProgressBar.
A component of the class JSpinner allows the user to choose a number or
object value from an ordered sequence via an input field.
JSlider is used to allow the user to visually select a value by turning a knob
within a defined range.
To show tree-structured or hierarchical data, you can use the JTree class.
By simply clicking on one of the tabs, JTabbedPane gives the user the freedom
to navigate between various groupings of components he wants to see.
In order to create a window-based application, this serves as a top-level
container on which various lightweight JAVA swing components can be
added.
Toggle buttons, which may be turned on or off, are made using the
JToggleButton class.
KEY TERMS
JProgressBar
JSpinner
JSlider
JTree
JTabbedPane
JDialog
JToggleButton
PAMANTASAN NG CABUYAO |COMPUTER PROGRAMMING 2
PA
GE
POST-TEST 10
2. This creates a type of graphic interface used to show how a prolonged computer activity
is progressing.
4. This java class allows the user to visually select a value by turning a knob within a defined range.
5. It is used to t enables the user to transition between a series of features by selecting a tab
with a certain title and/or icon.
REFERENCES