You are on page 1of 18

Republic of the Philippines

University of Cabuyao
(PAMANTASAN NG CABUYAO)
COLLEGE OF COMPUTING STUDIES
Katapatan Mutual Homes, Brgy. Banay-banay, City of Cabuyao, Laguna 4025

FINALS PRACTICE SET CCS103


COMPUTER PROGRAMMING 2

Score:
Name (LN,
SULLA , LEONARD F.
FN MI.):
Date: 02/05/24

Sem./
Section: 1-BSIT-C Instructor: Prof. Janus Raymond C. Tan 2nd/ 23-24
A.Y.

Project Link:

LEARNING OUTCOMES:

At the end of the laboratory task, the students should be able to:
• Implement Java Swing library as a tool for creating Graphical User Interface (GUI)
• Implement commonly used constructors and methods in creating a JList object

RESOURCES NEEDED:

For this laboratory task, you would need the following resources:
• Laptop or Personal Computer
• Any Java IDE (Eclipse IDE, NetBeans IDE etc.)

DURATION:

The laboratory task is expected to be done in 3 hours.

PRE-REQUISITES:

Knowledge and understanding of Java Swing library and its advanced controls such as JList and
DefaultListModel.

PROCEDURE FOR HANDS-ON ACTIVITY:

Create a Java Swing Application that will allow a user to enter in a JTextField component a string to be
stored as an element in either the JList for Fruits or JList for Vegetables. Two JRadioButton components
will signify each JList labeled as Fruits and Vegetables to be used to allow the user for entry selection. The
JRadioButton component for Fruits will be the default selection. An ADD Button will be used to execute the
saving of element each time the user clicks the button. The user is allowed to manipulate the contents of
both JList by selecting multiple elements either from each JList and clicking the MOVE LEFT or MOVE
RIGHT Button.

For example, if the user selects multiple items in the JList for Fruits, all those selected items will be
transferred in the JList for Vegetables by clicking the MOVE RIGHT Button. But if the user selects multiple
items in the JList for Vegetables, all those selected items will be transferred in the JList for Fruits by clicking
the MOVE LEFT Button. At the same time, two JLabel components will always display the number of items
in each JList component whenever the user did some changes in its contents.

Validate the user input so that empty string will not be allowed. Additionally, do not allow the user to
perform moving of elements in the JList if there is/ are no selected item/s in the JList. Always clear the
selection of items in the JList for every successful transfer of items by calling the clearSelection() method
of the JList class. You can also do your own initiative on how to handle possible user errors while
transferring items using the JList and JButton components to make your application more user-friendly.
Include JSrollpane for each JList ecomponent.

ASSESSMENT:

Students will be assessed according to the submitted program code that satisfies the given program requirements.
Score will be based on the following rubric:

PROGRAM CODE
This Java program utilizes Swing, a graphical user interface toolkit for Java, to create a windowed
application. It enables users to interactively input food items, categorize them as either fruits or vegetables,
and dynamically manage these lists. The interface design includes radio buttons for selecting the food
category, text fields for input, and buttons for adding items and moving them between lists. Through event
handling, such as button clicks, the program updates the lists accordingly and provides visual feedback by
updating the item counts displayed on the interface. By encapsulating these functionalities within a GUI, the
program simplifies the process of organizing and manipulating lists of fruits and vegetables, offering users an
intuitive and efficient tool for managing their food inventory.

Imagine you're using this program to plan your grocery shopping. You start by typing "mango" into the
text field and clicking the "ADD" button. Since apples are fruits, you select the "FRUITS" radio button, and the
program adds "apple" to the fruits list. Then, you decide you also need some vegetables, so you type "carrot"
into the text field and click "ADD" again, but this time you select the "VEGETABLES" radio button. Now,
"carrot" appears in the vegetables list. Later, while reviewing your list, you realize you actually need more
fruits than vegetables this time. You select "carrot" from the vegetables list and click the ">" button to move
it to the fruits list. The program updates the counts accordingly, showing you have one less vegetable and one
more fruit. In this way, the program helps you categorize and manage your grocery items efficiently, ensuring
you're well-prepared for your shopping trip.

THE USER INTERFACE

SOME IMPORTANT PART OF THE PROGRAM CODE

This part of the code handles what happens when the user clicks the "ADD" button. First, it gets the
text from the input field where the user typed the name of the food. Then, it checks if the input is not empty.
If it's not empty, it checks which radio button is selected: if the "Vegetables" radio button is selected, it adds
the food to the vegetables list; if the "Fruits" radio button is selected, it adds the food to the fruits list. If neither
radio button is selected, it shows a warning message asking the user to select a food category. After adding
the food to the appropriate list, it clears the input field for the next entry. If the input is empty, it shows a
warning message asking the user to enter a food. Finally, it sets the focus back to the input field for the user's
convenience.

This part of the code handles what happens when the user clicks the left arrow ("<") or right arrow
(">") button to move items between the fruits and vegetables lists. When the left arrow button is clicked, it
calls a method called "moveSelectionCheck" with parameters indicating the fruits list, the model for fruits, the
vegetables list, the model for vegetables, and a message to display if no item is selected from the fruits list.
Similarly, when the right arrow button is clicked, it calls the same method but with parameters swapped to
indicate the vegetables list, the model for vegetables, the fruits list, the model for fruits, and a message for
when no item is selected from the vegetables list. This method ensures that items are moved only if there's a
selection in the source list, preventing errors and guiding the user on proper interaction with the interface.

addList() method

This method is responsible for adding a new item to either the fruits or vegetables list, depending on
which one is currently selected by the user. It takes two parameters: the model of the list (either for fruits or
vegetables) and the item to be added. Inside the method, the "addElement" function is called on the model,
adding the new item to the list. After the addition, it calls another method called "itemCountCheck()" to update
the count of items displayed on the interface for the respective list. This method ensures that whenever an
item is added, the interface reflects the updated count of items in the list.

moveSelectionCheck() method

This method facilitates the movement of items between the fruits and vegetables lists while ensuring
that there is a selection made in the source list. It takes five parameters: the source list (from which items are
being moved), the model for the source list, the destination list (where items are being moved to), the model
for the destination list, and a message to display if no item is selected from the source list. Inside the method,
it checks if the source list has any selected items using the "isSelectionEmpty()" method. If there are selected
items, it calls another method called "MovingItem()" to perform the actual moving of items. If no items are
selected, it displays a warning message using a JOptionPane indicating that no item has been selected. This
method ensures that items can only be moved if there is a valid selection, preventing unexpected behavior.

itemCountCheck() method

This method is responsible for updating the displayed item counts for both the fruits and vegetables
lists whenever an item is added or removed. It doesn't take any parameters. Inside the method, it sets the text
of the labels representing the item counts for vegetables and fruits lists to show the current size of their
respective models (which hold the list data). By calling this method after adding or removing items, the
program ensures that the interface always reflects the accurate count of items in each list, providing users
with real-time feedback on the contents of their lists.
MovingItem() method

This method handles the actual movement of selected items from one list to another. It takes four
parameters: the source list (from which items are being moved), the model for the source list, the destination
list (where items are being moved to), and the model for the destination list. Inside the method, it retrieves
the indices of the selected items in the source list. If there are selected items (i.e., the length of the selected
indices array is greater than 0), it iterates through these indices in reverse order. For each selected item, it
removes it from the source model and adds it to the destination model. After moving all selected items, it
clears the selection in both the source and destination lists, ensuring a clean interface. Finally, it calls the
"itemCountCheck()" method to update the item counts displayed for both lists, reflecting the changes made
by moving items. This method effectively manages the transfer of items between lists while keeping the
interface updated with accurate item counts.

PROJECT SAMPLE RUN SCREENSHOT/S WITH DESCRIPTIONS:

You might also like