0% found this document useful (0 votes)
7 views33 pages

GUIs 2

This document outlines a lecture on advanced Java GUI components, focusing on JCheckBoxes, JLists, and JPanels. It covers their functionalities, event handling, and how to create complex layouts using these components. Additionally, it introduces basic vector drawing techniques in Java AWT for creating graphical interfaces.
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)
7 views33 pages

GUIs 2

This document outlines a lecture on advanced Java GUI components, focusing on JCheckBoxes, JLists, and JPanels. It covers their functionalities, event handling, and how to create complex layouts using these components. Additionally, it introduces basic vector drawing techniques in Java AWT for creating graphical interfaces.
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/ 33

DR EDWARD ANSTEAD

EXTENDED JAVA
GUIs Part 2: Overview
THIS WEEK’S LECTURE
• Review of last week
• JCheckBoxes component
• JList Component
• Working with JPanels
• Drawing primitives
LAST WEEK
-Basics of Swing as an API for creating desktop GUIs in JAVA
- Potted history of Java on the desktop
-JFrame
-Layout Managers
-JLabel
-JButton
-ActionListener
LAST WEEK: LAYOUT MANAGERS
LAST WEEK
LAST WEEK

FRAME
LAST WEEK

FRAME

LABELS
FRAME
LAST WEEK

FRAME

LABELS
FRAME

BUTTONS
LAST WEEK

FRAME

LABELS
FRAME

BUTTONS
TEXT EDIT
THIS WEEK:
THIS WEEK:
COMPLEX
LAYOUT WITH MULTIPLE
PANELS
THIS WEEK:
JLIST

COMPLEX
LAYOUT WITH MULTIPLE
PANELS
THIS WEEK:
JLIST

COMPLEX
LAYOUT WITH MULTIPLE
PANELS

JCHECKBOX JCHECKBOX
THIS WEEK:
JLIST

COMPLEX
LAYOUT WITH MULTIPLE
PANELS

CUSTOM
DRAWING

JCHECKBOX JCHECKBOX
DR EDWARD ANSTEAD

EXTENDED JAVA
GUIs Part 2:
JCheckBox
JCHECKBOX
2 states: checked or unchecked
JCHECKBOX
- A di erent type of button
- Inherits from AbstractButton (as does JButton)
- and JToggleButton
- De ned Using:

JCheckBox(String str)
fi
ff
JCHECKBOX: ITEMLISTENER
- When an item event occurs (box is checked or unchecked) an item event is generated
- Item events are handled by the classes that implement the ItemListener interface
- Interface speci es only one method itemStateChanged(ItemEvent ie)
fi
JCHECKBOX: ITEMLISTENER
- When an item event occurs (box is checked or unchecked) an item event is generated
- Item events are handled by the classes that implement the ItemListener interface
- Interface speci es only one method itemStateChanged(ItemEvent ie)

getItem() returns an object so we need to


cast to a JCheckBox
fi
JCHECKBOX EXAMPLE
DR EDWARD ANSTEAD

EXTENDED JAVA
GUIs Part 2:
JList
JLIST
JLIST
- Component for laying out textual list of items.
- One or more items can be selected
- Often wrapped inside a JScrollPane, to allow for lists longer than the selection.
- Can display an array of Objects or a special collection (GenericListModel which
implements ListModel.)
- JList is a Generic class (see next Lecture) de ned as follows:

JList<E> identifier = new JList(E[] itemsIdentifier)


fi
JLIST: SELECTION MODES

ListSelectionMode.SINGLE_SELECTION ListSelectionMode.SINGLE_INTERVAL_SELECTION ListSelectionMode.MULTIPLE_INTERVAL_SELECTION


JLIST: LISTSELECTIONLISTENER
- When a new item from the list is selected a ListSelectionEvent is generated.
- This is handled by the valueChanged() method of a class that implements the
ListSelectionListener interface
JLIST EXAMPLE
DR EDWARD ANSTEAD

EXTENDED JAVA
GUIs Part 2:
JPanel
JPANEL
- JPanels are container objects.
- We can use them in much the same way as frames
- Add components to them
- Set a layoutManager
- Using Arrangements of panels allows us to create complex interfaces

JPanel controls = new JPanel();


JPANEL EXAMPLE: PHONE BOOK EXAMPLE
DR EDWARD ANSTEAD

EXTENDED JAVA
GUIs Part 2:
Vector Drawing
DRAWING SHAPES
- We can do basic vector drawing using Java AWT.
- Create a new class that extends JPanel.
- Overriding the PaintComponent method we can use the Graphics content to draw
- primitive shapes (rectangles, ovals, arcs and lines)
- Filled and hollow shapes
- Draw raster images from les
- set colours and fonts.
- Not really designed for animation and complex graphics.
fi
DRAWING SHAPES EXAMPLE
BRINGING IT ALL TOGETHER:
PHONE BOOK EXAMPLE

You might also like