0% found this document useful (0 votes)
60 views23 pages

Radford Burger

The document discusses various aspects of GUI design in Java including commonly used Swing controls, layout managers, creating menus, event handling using inner classes or anonymous classes, and sorting an ArrayList of custom objects using a Comparator implemented either in the same class or a separate class. Students are assigned practical exercises to work on adding GUI components, redesigning layouts, and sorting ArrayLists.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views23 pages

Radford Burger

The document discusses various aspects of GUI design in Java including commonly used Swing controls, layout managers, creating menus, event handling using inner classes or anonymous classes, and sorting an ArrayList of custom objects using a Comparator implemented either in the same class or a separate class. Students are assigned practical exercises to work on adding GUI components, redesigning layouts, and sorting ArrayLists.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 23

27 Feb 2023

Radford Burger
Agenda
 GUI: Re-cap
 LayoutManagers
 Menus
 Inner class for even-handling
 Sorting an Arraylist of custom objects
Practical exercise
a) Try to add some GUI
components to the
NORTH area.

b) Redesign the layout


of the entire GUI. The
current functionality
must remain but you
can provide additional.
Gui: Recap
Every user interface considers the
following three main aspects −

•UI Elements − These are the core visual


elements the user eventually sees and
interacts with.

•Layouts − They define how UI elements


should be organized on the screen and
provide a final look and feel to the GUI
(Graphical User Interface).

•Behavior − These are the events which


occur when the user interacts with UI
elements.
Gui: Recap

Every SWING controls inherits properties from the following Component class hiearchy.
Gui: Recap
list of commonly used controls while designing GUI using SWING:

Class Description
JLabel
Class Placing text in a container
Description
JButton
JLabel
JCheckBox
JButton
JRadioButton
JCheckBox
JColorChooser
JRadioButton
JTextField
JColorChooser Editing of single line of text

JTextArea Editing of multiple lines of text

JComboBox Dropdown menu of options

Jlist Scrolling list of text items

JScrollBar Allows user to scroll through a range of


values
Gui: Layouts
Layout refers to the arrangement of components within
the container. In another way, it could be said that layout
is placing the components at a particular position within
the container.

The layout manager automatically positions all the


components within the container
Gui: Layouts
list of commonly used layout managers:

Class description
BorderLayout Arranges components to fit in 5 regions
GridLayout Places components in a rectangular grid
FlowLayout Arranges components in a directional flow
CardLayout Treats each component as a card. Only 1
card is visible at a time
GridBagLayout Aligns components vertically or
horizontally, or along their baseline
Gui: Menus
As we know that every top-level window has a menu
bar associated with it.
This menu bar consists of various menu choices
available to the end user.
Further, each choice contains a list of options, which is
called drop-down menus.
Gui: Menus
list of menu controls:

Class description
JMenuBar Top-level window
JMenu Pull-down menu component displayed
from the menubar
JMenuItem Available options in the pull-down menu
Gui: Menus
Sequence of steps to create
menus
1. Create JMenuBar object and attach to frame
2. Create JMenu object
3. Create JMenuItem objects and attach to JMenu
object
4. Attach JMenu object to JMenuBar object
Sequence of steps to create
menus
1. Create JMenuBar object and attach to frame

JMenuBar mBar = new JMenuBar( );


setJMenuBar(mBar);
Sequence of steps to create
menus
1. Create JMenuBar object and attach to frame
2. Create JMenu object

JMenu fileMenu = new JMenu(“File”);


Sequence of steps to create
menus
3. Create JMenuItem objects and attach to JMenu
object
JMenuItem newItem = new JMenuItem(“New record”);
JMenuItem updateItem = new JMenuItem(“Update
record”);
fileMenu.add(newItem);
fileMenu.add(updateItem);
Sequence of steps to create
menus
4. Attach JMenu object to JMenuBar object

mBar.add(fileMenu);
Event-handling of menus

JMenuItem objects generate Action events

Register JMenuItem objects with ActionListener


Alternative syntax for
Event-handling
 Named inner class
 Anonymous inner class

Let’s look at LottoGui2.java


Sorting Arraylist of objects
with Comparator in
separate class
 We will define another class that will implement
the Comparator interface of the type of our
custom object.
Sorting Arraylist of objects
with Comparator in
separate class
 This class will override the compare method of
the Comparator class which accepts two objects
of the Employee class as parameters and returns
the comparison value according to our
requirement whether we want to sort the array in
ascending or descending order and on which
attribute, we want to sort the list.
Sorting Arraylist of objects
with static Comparator in
custom worker class
 Similar to previous way, but no need to
‘implement Comparator’.
 https://www.geeksforgeeks.org/how-to-sort-an-
arraylist-of-objects-by-property-in-java/
Practical exercises for this
week
 Complete Prac 3 – sorting of Arraylists

 Start working on Prac 5

 … and Prac 6
Conclusion

Please check BB regularly for any


announcements
If you have any questions/queries please
contact me via email:
[email protected]
Please use your cput email for all
correspondence with your lecturers

You might also like