












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!













Overview
Component
items displayed (widgets) Container
region containing components Layout
arrangement of components Event
interactions for GUI
Java GUI Classes
Component classes Items (widgets) displayed in GUI JButton, JTextField, JDialog, JScrollBar… Container classes Hold GUI Components JFrame, JPanel, JApplet… Helper classes Describe properties of other GUI components Color, Graphics, Dimension…
How to Create a GUI
We’ll just use frames for now
Use layout manager to determine position
By adding event listeners to GUI components
Java Components
With three JButtons
JFrame Hierarchy Several super classes As well as implemented interfaces Many, many member methods Including inherited methods that al low Resizing Setting properties Adding components, Etc… Other top level containers JDialog (dialog boxes) JApplet (web applets) JWindow (stripped down JFrame, no titlebar or window buttons) Object Component Container Window Frame JFrame
GUI Elements – Layout
Arrangement of GUI components in container
Logical terms ( nd row, 1 st column, left) Preferred approach Actual coordinates (100 pixels, 5 inches) Can be too rigid, limited to certain window sizes
Entity translating layout specifications into actualcoordinates at runtime, depending on conditions
Java Layout Managers
Lays out components from left to right
Lays out components in a grid of user specified size
Designates portions of the container as North,South, East, West, and Center
Adds components one on top of another
Customizable manager that can use rows andcolumns of varying lengths
Java Components
Java Components
Java Components
Each JTable object Gets its data from an object implementingTableModel interface Displays contents of TableModel object DefaultTableModel class implements TableModel Many different ways to use JTable to display data
GUI Elements – Events
Action or condition occurring outside normal flow ofcontrol of program
Mouse clicks Keyboard input Menu selections Window actions
GUI events handled in event dispatching thread
Event Dispatching Thread
Allows current thread to execute GUI code indispatching thread createAndDisplayGUI Method that actually defines the GUI javax.swing.SwingUtilities.invokeLater(new Runnable( ) { public void run( ) { createAndDisplayGUI( ); } });
Java Support For GUIs
Appendix C of textbook Javadoc for the JDK Swing tutorial Course slides and code handouts Java Ranch