Systems Programming Graphical User Interfaces

Similar documents
Graphical User Interfaces. Swing. Jose Jesus García Rueda

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing

Window Interfaces Using Swing Objects

Window Interfaces Using Swing Objects

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

Graphical User Interfaces. Comp 152

Graphic User Interfaces. - GUI concepts - Swing - AWT

Introduction to the JAVA UI classes Advanced HCI IAT351

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

Java. GUI building with the AWT

Graphics programming. COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson)

Introduction. Introduction

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

Graphical User Interfaces (GUIs)

CSE 331. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

Agenda. Container and Component

Java Graphical User Interfaces AWT (Abstract Window Toolkit) & Swing

SD Module-1 Advanced JAVA

SD Module-1 Advanced JAVA. Assignment No. 4

CSE 331. Event- driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

COMPSCI 230. Software Design and Construction. Swing

User interfaces and Swing

Programming Language Concepts: Lecture 8

Calculator Class. /** * Create a new calculator and show it. */ public Calculator() { engine = new CalcEngine(); gui = new UserInterface(engine); }

KF5008 Program Design & Development. Lecture 1 Usability GUI Design and Implementation

Programming graphics

Programming Languages and Techniques (CIS120e)

Chapter 8. Java continued. CS Hugh Anderson s notes. Page number: 264 ALERT. MCQ test next week. This time. This place.

More Swing. CS180 Recitation 12/(04,05)/08

Java continued. Chapter 8 ALERT ALERT. Last week. MCQ test next week. This time. This place. Closed book. Assignment #2 is for groups of 3

Graphical User Interfaces in Java - SWING

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver

Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008

Jonathan Aldrich Charlie Garrod

Window Interfaces Using Swing. Chapter 12

2110: GUIS: Graphical User Interfaces

All the Swing components start with J. The hierarchy diagram is shown below. JComponent is the base class.

Example Programs. COSC 3461 User Interfaces. GUI Program Organization. Outline. DemoHelloWorld.java DemoHelloWorld2.java DemoSwing.

Handout 14 Graphical User Interface (GUI) with Swing, Event Handling

Swing from A to Z Some Simple Components. Preface

Dr. Hikmat A. M. AbdelJaber

GUI Program Organization. Sequential vs. Event-driven Programming. Sequential Programming. Outline

Java Swing. Recitation 11/(20,21)/2008. CS 180 Department of Computer Science, Purdue University

Parts of a Contract. Contract Example. Interface as a Contract. Wednesday, January 30, 13. Postcondition. Preconditions.

1005ICT Object Oriented Programming Lecture Notes

Introduction to GUIs. Principles of Software Construction: Objects, Design, and Concurrency. Jonathan Aldrich and Charlie Garrod Fall 2014

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread

Datenbank-Praktikum. Universität zu Lübeck Sommersemester 2006 Lecture: Swing. Ho Ngoc Duc 1

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

Dr. Hikmat A. M. AbdelJaber

Swing UI. Powered by Pentalog. by Vlad Costel Ungureanu for Learn Stuff

PIC 20A GUI with swing

Swing. By Iqtidar Ali

Lab 4. D0010E Object-Oriented Programming and Design. Today s lecture. GUI programming in

CS 2113 Software Engineering

Part I: Learn Common Graphics Components

Course Status Networking GUI Wrap-up. CS Java. Introduction to Java. Andy Mroczkowski

Swing from A to Z Using Focus in Swing, Part 2. Preface

CS 251 Intermediate Programming GUIs: Components and Layout

Java & Graphical User Interface II. Wang Yang wyang AT njnet.edu.cn

Together, the appearance and how user interacts with the program are known as the program look and feel.

GUI Basics. Object Orientated Programming in Java. Benjamin Kenwright

Java Swing. based on slides by: Walter Milner. Java Swing Walter Milner 2005: Slide 1

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Chapter 12 Advanced GUIs and Graphics

Packages: Putting Classes Together

Chapter 7: A First Look at GUI Applications

CHAPTER 2. Java Overview

Java Programming Lecture 6

Graphical User Interfaces in Java

Graphical User Interface (GUI) components in Java Applets. With Abstract Window Toolkit (AWT) we can build an applet that has the basic GUI

Chapter 1 GUI Applications

Java IDE Programming-I

An array is a type of variable that is able to hold more than one piece of information under a single variable name.

CSE Lab 8 Assignment Note: This is the last lab for CSE 1341

MIT AITI Swing Event Model Lecture 17

11/6/15. Objec&ves. RouleQe. Assign 8: Understanding Code. Assign 8: Bug. Assignment 8 Ques&ons? PROGRAMMING PARADIGMS

Part 3: Graphical User Interface (GUI) & Java Applets

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

Graphical User Interface (GUI)

Java: Graphical User Interfaces (GUI)

Chapter 6: Graphical User Interfaces

DM503 Programming B. Peter Schneider-Kamp.

Points Missed on Page page 1 of 8

Swing - JLabel. Adding a text (and HTML) labels to a GUI

Graphical interfaces & event-driven programming

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

H212 Introduction to Software Systems Honors

Java Swing. Lists Trees Tables Styled Text Components Progress Indicators Component Organizers

Programmierpraktikum

No SVN checkout today. Object-Oriented Design

The Abstract Windowing Toolkit. Java Foundation Classes. Swing. In April 1997, JavaSoft announced the Java Foundation Classes (JFC).

Introduction to concurrency and GUIs

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

COMP-202 Unit 10: Basics of GUI Programming (Non examinable) (Caveat: Dan is not an expert in GUI programming, so don't take this for gospel :) )

Transcription:

Systems Programming Graphical User Interfaces Julio Villena Román (LECTURER) <jvillena@it.uc3m.es> CONTENTS ARE MOSTLY BASED ON THE WORK BY: José Jesús García Rueda

Systems Programming GUIs based on Java Swing Julio Villena Román (LECTURER) <jvillena@it.uc3m.es> CONTENTS ARE MOSTLY BASED ON THE WORK BY: José Jesús García Rueda 1

Introduction What are the GUIs? Well known examples 2

Basic concepts Graphical application Containers Actions Events Graphical elements: Menu bar Title bar Minimize and maximize buttons Closing button Scroll Window frame Icons Buttons Text areas 3

The wall metaphor 4

The wall metaphor Frame contentpane Panel 5

Creating the wall How to create a window in Java? Window A window is a high level container 6

How to create a window in Java? import javax.swing.* ; public class Example extends JFrame { The classes needed /* This to methods starts everything*/ build GUIs are public included static void main (String argv[]) { in the Swing package Example window= new Example(); window.setsize (400, 400); window.setvisible(true); A window in Java is just a class extending Jframe, the generic window. } } The window must be made visible explicitly 7

Covering the wall with cork Every high level container in Swing (windows, for instance) will be covered with a contentpane The rest of the graphical components will be placed on it Including other containers Window window.getcontentpane() 8

Adding it to the code import javax.swing.* ; public class Example extends JFrame { /* This methods starts everything*/ public static void main (String argv[]) { Example window = new Example(); window.getcontentpane().add( ); window.setsize (400, 400); window.setvisible(true); } } 9

What elements can I attach to the cork? In the contentpane you can put elements from the Swing package: Labels: JLabel Buttons: JButton Text boxes: JTextField, JTextArea Checkboxes: JCheckBox Option buttons: JRadioButton Lists: JList Scroll bars: JScrollBar All the Swing components extend JComponent 10

And how can I attach them? JButton button; JLabel label; public Example() { } label = new JLabel( A label"); button = new JButton( A button"); button.setsize(100, 70); getcontentpane().add(button); getcontentpane().add(label); 11

LITTLE PAUSE... A good time to take a look at the Java API, in order to get to know where to find information on the different graphical components and how to use them... 12

And how can I attach corks to the cork? We will use CONTENT PANELS: JPanel They are medium level containers: They simplify the window organization A panel may contain other panels 13

Panel hierarchy 14

Example of panel JButton button; JLabel label; JPanel panel; public Example() { panel = new JPanel(); getcontentpane().add(panel); label = new JLabel( A label"); button = new JButton( A button"); button.setsize(100, 70); panel.add(button); panel.add(label); } 15

How can I put together all those components? You can use either coordinates label.setbounds(100, 70, 50, 50); or LAYOUTS: These are like templates to organize graphical components. They are associated to panels. We ll see three types here. To use coordinates you have to neutralize the layout first: panel.setlayout(null) You need to import java.awt.* in order to use layouts! 16

Example using FlowLayout JButton button; JLabel label; JButton otherbutton; JPanel panel; public Example() { panel = new JPanel(); getcontentpane().add(panel); It places the elements in a row, one after the other label = new JLabel( A label"); button = new JButton( A button"); otherbutton = new JButton( Other button"); panel.add(button); panel.add(label); panel.add(otherbutton); } FlowLayout is the default! (in panels) 17

Example using GridLayout public Example() { panel = new JPanel(); panel.setlayout(new GridLayout(2, 2)); getcontentpane().add(panel); label = new JLabel( A label"); button = new JButton( A button"); otherbutton = new JButton( Other button"); panel.add(button); panel.add(label); panel.add(otherbutton); } It places the elements in a grid 18

Example with BorderLayout public Example() { It is the default in high level containers panel = new JPanel(); panel.setlayout(new BorderLayout()); getcontentpane().add(panel); label = new JLabel( A label"); button = new JButton( A button"); otherbutton = new JButton( Other button"); panel.add(button, BorderLayout.SOUTH); panel.add(label, BorderLayout.WEST); panel.add(otrobutton, BorderLayout.NORTH); } It divides the container in five sections: North, south, east, west and center 19

Systems Programming Events Julio Villena Román (LECTURER) <jvillena@it.uc3m.es> CONTENTS ARE MOSTLY BASED ON THE WORK BY: José Jesús García Rueda 20

Session objectives Being able to add behaviour to the graphical elements in the interface modifying those elements as a result of the actions on them In other words, to cover the whole cycle: 1. Receiving events that take place on the graphical elements 2. Processing them 3. Showing feedback on the screen 21

Graphical application architecture Interface Processing Persistence How is this link created? 22

Anybody listening? When users act on the interface, something should happen. For being so, we will have to program events managers (listeners) Component Event Listener Method (reply to the event) 23

Examples of listeners WindowListener For managing window events ActionListener For managing buttons and other simple components events You will have to consult the API constantly! 24

Active waiting Once the GUI is painted on the screen the program stays in a stand-by mode, not running any active code When something happens on the interface, the associated listener wakes up 25

And translated into code? import java.awt.event.*; This package includes the listeners Listeners are interfaces, usually public class ListenerExample implements ActionListener { public void actionperformed (ActionEvent e) { System.out.println( Inside the listener"); } } This method is awaken automatically 26

Who listens to whom? If we have several graphical components and we can create as many listeners as we wish Who listens to whom? We ll have to associate, explicitly, the listeners to the components Multiple combinations are possible: Several listeners associated to the same component. One listener associated to several components. 27

How to set up the association? import javax.swing.*; Import java.awt.event.*; public class Example2 extends JFrame { JButton mybutton = new JButton ( Click here"); ListenerExample mylistener = new ListenerExample(); Creating an instance of the corresponding listener } public Example2 () { getcontentpane().add(mybutton); mybutton.addactionlistener(mylistener); } public static void main (String[] arg) { Example2 window = new Example2(); } window.setsize(200, 200); window.setvisible(true); Associating the listener to the component 28

Which part of the listener is awaken? Listeners have different methods to listen to different events. Java automatically invokes the suitable method, depending on the event. The body of these methods will be programmed by us. We can invoke other methods from these. When the method running is over, the program moves on to stand-by again, awaiting for new events. These methods receive an event object as argument. 29

Example: WindowListener Among its methods we find: void windowclosing (WindowEvent evt) void windowopened (WindowEvent evt) void windowclosed (WindowEvent evt) void windowiconified (WindowEvent evt) void windowdeiconified (WindowEvent evt) void windowactivated (WindowEvent evt) void windowdeactivated (WindowEvent evt) 30

May I get more information about an event? The event received as an argument by the listeners methods is provided automatically by Java Asking to that event object we can find out more things about what really happened Asking, as always, is done by invoking methods of the event object 31

Example import java.awt.event.*; Argument provided by Java automatically public class ListenerExample implements ActionListener { public void actionperformed (ActionEvent e) { String source = e.getactioncommand(); System.out.println( Button: " + source); } } It gives back the label of the component that started the event 32

Event oriented programming GUIs in Java is just an example of a more general and very important programming technique: Events Oriented Programming In a program everything is sequential: the time when each action is going to happen is predictable How can we take into account those events in the world outside our program that we don t know exactly when will happen? When will that door open? When will this pot of water boil? When will the user push this button? Programs have mechanisms to react ( wake up ) when specific events take place outside the program 33

Code organization Everything explained about GUIs is under the principles and rules of the OO programming paradigm so everything we know about OO up to now is perfectly valid here We have just added new pieces to the meccano that can be mixed with the rest in the way we consider most suitable. Examples: Creating the listeners as independent classes Creating the listeners as inner classes Making the graphical components themselves act as listeners Associating a listener to more than one graphical component 34

Adapters Some listeners interfaces have lots of methods and we will have to implement them all (listeners are interfaces) Adapters are classes that implement all the methods of a specific listener Being classes, we just have to extend them rewriting the methods we need For every Listener interface, there is an Adapter class: WindowListener WindowAdapter KeyListener KeyAdapter MouseListener MouseAdapter 35

36