Cs201 swing

15
GUIs in Java Swing, Events CS201, SW Development Methods

description

Hariprasanna V (9843824677)

Transcript of Cs201 swing

Page 1: Cs201 swing

GUIs in JavaSwing, Events

CS201, SW Development Methods

Page 2: Cs201 swing

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

Page 3: Cs201 swing

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

Page 4: Cs201 swing

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

Page 5: Cs201 swing

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

Page 6: Cs201 swing

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

Page 7: Cs201 swing

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

Page 8: Cs201 swing

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

Page 9: Cs201 swing

9

Swing

• Componentsand containers:– superclasses

and interfaces– extends

and implements

© O’Reilly 1999

Page 10: Cs201 swing

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 )

Page 11: Cs201 swing

11

Events

• Two approaches to event handling– read-evaluation loop (client-written loop)– notification-based (callbacks)

• Swing uses the 2nd approach

Page 12: Cs201 swing

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

Page 13: Cs201 swing

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)

Page 14: Cs201 swing

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()

Page 15: Cs201 swing

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 );