Cs201 swing
-
Upload
hariprasanna-v -
Category
Documents
-
view
590 -
download
2
description
Transcript of Cs201 swing
GUIs in JavaSwing, Events
CS201, SW Development Methods
2
Why Study GUIs in CS201
• First, why not?– Complex topic, complex library– Many classes, methods– Hard to do this well initially
• Reasons we study GUIs– Again, example of inheritance in a
framework, software reuse, etc.– Event-driven programming
– An important form of program-control
3
Swing
• Swing is a Java library (framework) for creating GUIs– Part of a larger JFC (Java Foundation
classes)– Replaces but uses an older library, AWT– Another, newer alternative: SWT
– Used in Eclipse
• Swing apps will use look-and-feel of the system they’re running on– Or can be set by the program
4
Learning Swing
• Important things to learn– Swing components
– E.g. buttons, text-fields, frames, etc.
– How to organize and arrange them– Containers, layout managers
– How to make things change when something happens
– Event-based programming
5
Containment Hierarchy
• Top-level container:– place for other Swing components to paint
themselves– e.g., JFrame, JDialog, Japplet
• Intermediate container:– simplify positioning of atomic components– e.g., JPanel, JSplitPane, JTabbedPane
6
Components and Containers
• See pages 810-816 in text• All Swing GUI objects are Components• Some are also Containers
– Example: JFrame, JPanel, etc
• You place other Components inside Containers– Example: a JFrame has buttons, text-fields, etc.– Example: a JPanel is part of a window, in which
we organize GUI components
7
What We Do with Containers
• Add components to them
• Determine how these items will be arranged on the screen– Layout control– We associate a Swing layout-manager with
each container
• Layout is very hard to do at the beginning– So we won’t sweat it in CS201
8
Non-Containers
• Atomic components:– self-sufficient components that present
information to and get input from the user– e.g., JButton, JLabel, JList, JComboBox,
JTextField, JTable
9
Swing
• Componentsand containers:– superclasses
and interfaces– extends
and implements
© O’Reilly 1999
10
Top-Level Containers
• JFrame example:– contains a single component JRootPane, which
has a JMenuBar (optional) and a content pane
– add non-menu components to its content panel– theFrame.add( aButton )
– Pre Java 5.0– theFrame.getContentPane().add( aButton )
11
Events
• Two approaches to event handling– read-evaluation loop (client-written loop)– notification-based (callbacks)
• Swing uses the 2nd approach
12
Events
• Swing:– objects communicate by “firing” and
“handling” events (event objects)– (conventional method call)
– events are sent from a single source object to one or more registered listener objects
13
Events
• Swing:– different event sources produce different
kinds of events
e.g., a JButton object, when clicked, generates an ActionEvent object, which is handled by an ActionListener (an object whose class implements this interface)
14
Events
• Handling:– create a component
– e.g., a JButton
– add it to the GUI– e.g., to a JPanel
– register a listener to be notified when the component generates an event
– e.g., interface ActionListener
– define the callback method– e.g., actionPerformed()
15
Event Handling
• class MyListener implements ActionListener { … public void actionPerformed( ActionEvent event ) { // react to event … }}
• …// instantiate event listenerActionListener listener = new MyListener();…// instantiate event sourceJButton button = new JButton( “Hello” );…// register event listener with event sourcebutton.addActionListener( listener );