Outline Introduction to GUI Swing Basic components Event


Outline Introduction to GUI Swing Basic components Event handling

CLI VS GUI

CLI VS GUI CLI applications GUI applications Program Flow Transactional Reactive, interactive Input Textual Asynchronous Output Textual Graphical Multi. Tasking No Yes After Execution Exit Remains active

GUI programming challenges Usability Attractiveness Multithreading Incremental execution Testing And • The algorithm

Elements of GUI programming Visualization of data: Model Widgets Visualization of commands: Buttons, pop-ups Responding of commands: Event handling Graphical layout: Containers, panels, layout managers Reactiveness: Using threads safely Separation of concerns: OOP, good design, MVC

SWING Java’s GUI framework (Java Foundation Classes) Extension of an older framework – AWT Provides: Frames, dialogs, panels, toolbars. . Graphical control widgets, from buttons to tree controls Thread-safe events dispatching Rich-text editing Pluggable Look-n’-feel(PLAF) Other goodies (accessibility, text parsing. . ) Official Swing Tutorial

Swing Elements Components - Graphical widgets Containers - Components that contain sub-components Layout - Arrangement of components inside containers Events - Interaction with the user Event listeners – Objects that responds to events asynchronously Event dispatcher – A special thread that dispatches events and re-paints the GUI Models - Objects that hold the data and handle events for components

Swing components - hierarchy Each component is a Java class with a fairly extensive inheritency hierarchy: Object Component Container javax. swing java. awt java. lang JComponent Window JPanel Frame JFrame

Using Swing Components Very simple, just create object from appropriate class – examples: JButton but = new JButton(); JText. Field text = new JText. Field(); JText. Area text = new JText. Area(); JLabel lab = new JLabel(); Many more classes. Don’t need to know every one to get started.

Adding components Once a component is created, it can be added to a container by calling the container’s add method: Container cp = get. Content. Pane(); cp. add(new JButton(“cancel”)); cp. add(new JButton(“go”)); How these are laid out is determined by the layout manager (next lesson).

JFrame A frame contains: Title bar Menu bar Content pane The content pane is a container that represent the main area of the frame.

Example 1 Hello. jar

Jhello World! public class Hello extends JFrame { public Hello(){ super("Title Bar"); set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); JButton click = new JButton("This is a button"); Container cp = get. Content. Pane(); cp. add(click); pack(); set. Visible(true); } public static void main(String args[]){ Hello frame = new Hello(); } }

Event handling

Events and Listeners in Swing Input from the user in Swing is represented by Events come from a Source: a component or a model Related kinds of inputs are grouped into Listener interfaces A listener interface contains several messages that passes events to the receiver Each message represent a different scenario of receiving this input from the user An Event Listener is an object whose class implements a listener interface, so it can receive events Attaching a listener of type X to a source is done by the call : source. add. XListener(listener)

Example 2 Jumper. jar

Add Action Listener public class Jumper extends JFrame implements Action. Listener{ public Jumper(){ super(“Jumper"); set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); JButton btn = new JButton(“Catch me!"); btn. add. Action. Listener(this); //source. add. XListener(listener) Container cp = get. Content. Pane(); cp. add(btn); pack(); set. Visible(true); } @Override public void action. Performed(Action. Event e) { Random rand = new Random(); int x = rand. next. Int(300); int y = rand. next. Int(300); set. Location(x, y); } }

The Event object All event classes in Swing extends the Event. Object class. This class contains the method get. Source( ) that returns the object which was the source of the event Events sub-classes usually also contain data related to specific event.

Event Listener Example: Action Listener

Event Listener Example: Mouse Listener

Listening to an event

Example 3 Convertor. jar

public class Convertor extends JFrame implements Action. Listener { private static double DOLLAR_RATE = 3. 943; private JButton convert. Button; private JButton reset. Button; private JLabel result. Label; private JText. Field value. Input; private double value; public Convertor(){ super("Convertor"); set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); value = 0; // Initiate label and text field. value. Input = new JText. Field(); value. Input. set. Columns(10); value. Input. set. Text( value + ""); result. Label = new JLabel(value + " $ is "+ convert() + " Shekel"); // Create buttons convert. Button = new JButton("Convert"); reset. Button = new JButton("Reset");

// Add action listeners convert. Button. add. Action. Listener(this); reset. Button. add. Action. Listener(this); // Add all objects to Content Pane get. Content. Pane(). set. Layout(new Flow. Layout()); get. Content. Pane(). add(reset. Button); get. Content. Pane(). add(value. Input); get. Content. Pane(). add(convert. Button); get. Content. Pane(). add(result. Label); pack(); set. Visible(true); }

public void action. Performed(Action. Event e) { if (e. get. Source(). equals(convert. Button)){ update. Value(); } if (e. get. Source(). equals(reset. Button)){ set. Value(0); } update. View(); } private void set. Value(double v) { this. value = v; } private double convert() { return value*DOLLAR_RATE; } private void update. Value(){ this. value = Double. parse. Double(value. Input. get. Text()); } private void update. View(){ result. Label. set. Text(value +" $ is " + convert() + " Shekel " ); value. Input. set. Text(value +""); pack(); }
- Slides: 26