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

Size: px
Start display at page:

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

Transcription

1 Java Graphical User Interfaces AWT (Abstract Window Toolkit) & Swing Rui Moreira Some useful links: Graphical User Interfaces n Java provides a good integration with interactive web-based applications - Applets, JSP, Servlets n The Java Foundation Classes (JFC) include packages (cf. AWT & Swing J2SE) with built-in components and functionality for assembling GUIs n Both packages provide mechanisms for building GUI interfaces (e.g., windows, buttons, text fields, etc.) Rui Moreira 2 1

2 AWT vs Swing n No big architecture differences between AWT and Swing since both define an euivalent class hierarchy, but using different packages, e.g.: java.awt.frame javax.swing.jframe java.awt.panel javax.swing.jpanel java.awt.dialog javax.swing.jdialog java.awt.button javax.swing.jbutton java.awt.textfield javax.swing.jtextfield java.awt.label javax.swing.jlabel java.awt.checkbox javax.swing.jcheckbox java.awt.combobox javax.swing.jcombobox java.awt.list java.swing.jlist n Major difference is related with rendering (how components are drawn/displayed on screen) n We cannot use/combine components from both packages in the same GUI (container) although they share Layout Managers Rui Moreira 3 AWT n The first Java package (java.awt) for building GUI components was AWT (Abstract Window Toolkit) - JDK 1.0 & JDK 1.1. n Graphical components look different in different Operating System (OS) n Defines/uses heavy-weight components each owning its own viewport which sends output info (via OS) to the screen n Each component has less code (than counterpart Swing) because it uses a peer component (Native Screen Source) provided by the OS which renders the component to screen Rui Moreira 4 2

3 Swing n Since J2SE (JDK1.2) Java includes a new package (javax.swing) which is a pure Java release n Graphical components look the same in different Operating System (OS) n Defines/uses light-weight components each redirects drawing to the component it build on n Each component has more code (than counterpart AWT) because it provides a richer and OS-independent functionality natively implemented in the component n All components use the same OS peer component for rendering Rui Moreira 5 Major elements n GUI Components (e.g., Button, Label, TextField) Graphical elements responsible for the interaction and that may live inside containers n Containers (e.g., Frame, Panel, Dialog) Components that group/assemble several graphical elements or even other containers n Layout Managers (e.g., FlowLayout, BorderLayout, GridLayout) Elements responsible for managing/structuring the way components are added/organized inside a container n Events (e.g., ActionEvent, MouseEvent, WindowEvent) Objects that describe an action performed by the user on interface elements n Listeners/Handlers (e.g., ActionListener, MouseListener) Elements responsible for detecting/receiving interaction events and select/execute associated action code Rui Moreira 6 3

4 GUI Components n There are several graphical elements/components that may be instantiated and added to a container to be displayed on screen: // Using AWT Button b = new Button( Ok ); Label l = new Label( Press ok ); TextBox t = new TextBox(); // Using Swing JButton jb = new JButton( Ok ); JLabel jl = new JLabel( Press ok ); JTextBox jt = new JTextBox(20); Rui Moreira 7 Containers n There are several types of containers which are used to group together several GUI components n Each container may include several GUI components and may also include other inner containers (to mix together different layouts) // Using AWT Frame f = new Frame( AWT Frame Example ); Panel p = new Panel(); // Using Swing JFrame jf = new JFrame( Swing Frame Example ); JPanel jp = new JPanel(); Rui Moreira 8 4

5 AWT GUI Components hierarchy Legend: Rui Moreira 9 Swing hierarchy Legend: Rui Moreira 10 5

6 Add Component to Container import java.awt.container; import javax.swing.*; /** TestButton: class for testing a Button inside Frame */ public class TestButton { public static void void main(string args[]){ // CREATE JFrame (container) & JButton (component) JFrame jf = new JFrame( Frame Test button ); JButton jb = new JButton( Ok ); // GET Container reference to ADD JButton Container c = g.getcontentpane(); c.add( Center, b); // Pack and Show f.pack(); f.setvisible(true) //Deprecated: f.show(); Rui Moreira 11 Layout Managers n Layout Managers are responsible for adding/organizing GUI elements inside containers n Layout Managers control the position and size of each GUI component inside the container n Each container has a reference to its Layout Manager and whenever we add a GUI component to the container it invokes the respective Layout Manager to do it (deciding the size and position of the component to be added) n Each container has a default Layout Manager (that we may change) Rui Moreira 12 6

7 AWT Layout Managers n FlowLayout: positions GUI components in lines (centered) like words in a text page (default for Panel and Applet) n BorderLayout: positions GUI components on 5 different predefined areas - North, South, West, East and Center (default for Frame, Window and Dialog) n CardLayout: organizes interfaces like layers; we may only see a layer/card at a time n GridLayout: positions GUI components over a pre-defined grid/matrix; number of lines & columns are specified by developer n GridBagLayout: positions GUI components over a pre-defined grid/matrix but columns may have different sizes and GUI components may overlap several columns Rui Moreira 13 Layout Managers hierarchy NB: Swing containers (e.g., JFrame, JPanel) use these AWT Layout Managers Rui Moreira 14 7

8 Set JFrame Layout import java.awt.flowlayout; import javax.swing.*; public class TestButton { public static void void main(string args[]){ // CREATE JFrame (container) & JButton (component) JFrame jf = new JFrame( Frame Test button ); JButton jb = new JButton( Ok ); // GET Container reference to ADD JButton Container c = g.getcontentpane(); // SET JFrame LAYOUT (by default is BorderLayout) c.setlayout(new FlowLayout()); c.add( Center, b); // Pack and Show f.pack(); f.setvisible(true) //Deprecated: f.show(); Rui Moreira 15 Events n When users act over GUI components one/several events may be triggered n Events are described by pre-defined classes/objects which characterize the event and its source component, e.g., ActionEvent: action events on buttons/menus, e.g., action performed MouseEvent: mouse events, e.g., moved, pressed, entered, etc. WindowEvent: window events, e.g., closing, iconified, etc. ItemEvent: list box events, e.g., item state changed TextEvent: text box events, e.g., text value changed Rui Moreira 16 8

9 AWT Events hierarchy NB: Swing components (e.g., JFrame, JButton, JTextField) also detect/delegate these events to listeners/handlers Rui Moreira 17 Swing Events hierarchy Legend: Rui Moreira 18 9

10 Listeners/Handlers n Java provides/implements a Delegation Event Model: each GUI component detects its own events - triggered by specific user-interactions (e.g., press button, move mouse, etc.) to detect these events developers must register specific eventlisteners with each GUI component GUI components then delegate/forward event handling to these pre-registered methods - handlers n Event Listeners are specific Java interfaces (e.g., ActionListener, MouseListener) that group/define methods for handling particular sets of events n Event-Handlers are classes (built by developers) that implement specific Event-Listeners, i.e., provide methods/behavior that will receive the event objects and act accordingly Rui Moreira 19 AWT Listeners hierarchy Rui Moreira 20 10

11 Swing Listeners hierarchy Rui Moreira 21 Implement an Event-Handler import java.awt.event.*; /** ButtonHandler: class that we implement for handling events related with buttons pressed */ public class ButtonHandler implements ActionListener { // The ActionListener interface defines only // the actionperformed() method public void actionperformed(actionevent ae){ JButton jb = (JButton )ae.getsource(); String sb = ae.getactioncommand(); // sb=jb.gettext(); // Just print out the button name System.out.println( Someone pressed button +sb); Rui Moreira 22 11

12 Register an Event-Handler import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TestButton { public static void void main(string args[]){ JFrame jf = new JFrame( Frame Test button ); JButton jb = new JButton( Ok ); // REGISTER event-handler with Button b.addactionlistener(new ButtonHandler()); // Add Button to Frame ( Center of BorderLayout) Container c = g.getcontentpane(); c.add( Center, b); f.pack(); f.setvisible(true) //Deprecated: f.show(); Rui Moreira 23 Listeners/Handlers n The Button class has a specific method - addactionlistener() - for registering an ActionListener Handler with the button n Buttons trigger/detect ActionEvents which are handled by a specific method - actionperformed() - defined in the ActionListener interface n Developers must implement specific ActionListener Handlers for dealing with the ActionEvents of each button Rui Moreira 24 12

13 Listeners/Handlers n Each GUI component provides specific methods for registering different types of Listeners/Handlers, e.g., addmouselistener() is provided by buttons for registering MouseListener Handlers; addwindowlistener() is provided by containers (e.g., Frame) for registering WindowListener Hanlders n Each event category is detected by specific listeners which define particular sets of methods for handling those events n Methods defined by Event Listener interfaces are intuitively named according to the handled events Rui Moreira 25 Examples of Events Categories Event Listener Methods ActionEvent ActionListener actionperformed() MouseEvent MouseMotionListener mousedragged(), mousemoved() MouseEvent MouseListener mousepressed(), mousereleased(), etc. WindowEvent WindowListener windowclosing(), windowopened(), etc. TextEvent TextListener textvaluechanged() ItemEvent ItemListener itemsatechanged() Rui Moreira 26 13

14 Adapters n For each Listener interface Java defines a correspondent Adapter, e.g., MouseListener interface MouseAdapter class ActionListener interface ActionAdapter class n Adapters are classes which implement Listener interfaces but with empty methods n Developers may use (via extends) Adapters to code event handlers without having to implement all the methods defined in Listener interfaces Rui Moreira 27 Delegation Model Advantages n The Delegation Model has some advantages in comparison with the hierarchical model previously used by JDK1.0: Events are no longer accidentally resolved (handled) by containers (in the hierarchical model events could propagate to upper-level containers and then unexpectedly handled by them) Developers my create several filter classes (Adapters) to classify/select event actions The delegation model is more suitable for distributing work among different event-handler classes The delegation model provides support for Java Beans Rui Moreira 28 14

15 Exercises n Implement the following GUIs: ColoredFrame: JFrame with 2 JButtons (blue, yellow) that change the background color of the frame; LoginFrame: JFrame with JLabels, JTextFields, JPasswordFields and JButtons to perform a login (just print out the content of the text and password fields); ChatFrame: JFrame with JTextArea, JTextField and JButton to implement the GUI of a chat room; detect/handle ActionEvent (detect send button action cut text field to text area) and KeyEvent (detect \n key pressed on the text field cut text field to text area) by implementing ActionListener and KeyListener; use reuestfocus() method to set the focus on the text field; MenuFrame: JFrame with a menu bar that has the following menus -File, Edit, Help; File menu has options - Save, Load and Quit; Selecting the Quit menu option triggers an ActionEvent that should exit the program; Calculator: GUI calculator for basic arithmetic operations (+, -, * and /); use 3 JPanels; North panel shows output JTextField; Center JPanel uses GridLayout with all number and operation JButtons; South panel shows clear JButton. Rui Moreira 29 CardLayout Example (1/5) public class CardLayoutFrame extends JFrame implements MouseListener, WindowListener, KeyListener { // Declare/Create JPanels, JLabels, JButtons and CardLayout JPanel jp1 = new JPanel(), jp2 = new JPanel(), jp3 = new JPanel(), jp4 = new JPanel(); JLabel jl1 = new JLabel( Panel 1 ), jl2 = new JLabel( Panel 2 ), jl3 = new JLabel( Panel 3 ), jl4 = new JLabel( Panel 4 ); JButton jb1 = new JButton( Go 2 ), jb1 = new JButton( Go 3 ), jb3 = new JButton( Go 4 ), jb1 = new JButton( Go 1 ); CardLayout cl = new CardLayout(); Container c = null; // Container will be set latter // To be continue... Rui Moreira 30 15

16 //... Continue (2/5) public class CardLayoutFrame extends JFrame implements MouseListener, WindowListener { // Implement Constructor public CardLayoutFrame (String frametitle){ super(frametitle); // Call Constructor of JFrame c = this.getcontentpane(); // Get JFrame Container c.setlayout(this.cl); // Set layout manager of JFrame Container // Add each JPanel to a Named-Card of the JFrame container c.add(this.jp1, First ); // 1st JPanel added to card named First c.add(this.jp1, Second ); // 2nd JPanel added to card named Second... // Add each JLabel and JButton to the respective JPanel this.jp1.add(this.jl1); this.jp1.add(this.jb1);... // Set Listener/Handler (himself) for each JButton this.jb1.addactionlistener(this);... this.cl.show(c, First ); // Show First Card of JFrame Container this.setsize(200, 200); // Set JFrame size this.setvisible(true); // Set JFrame visible // To be continue... Rui Moreira 31 //... Continue (3/5) public class CardLayoutFrame extends JFrame implements MouseListener, WindowListener { //... // Implementing MouseListener public void actionperformed(actionevent ae){ String str = ae.getactioncommand(); // Check/Select which Card to Show if (str.eualsignorecase( Go 2 )){ this.cl.show(this.c, Second ); else if (str.eualsignorecase( Go 3 )){ this.cl.show(this.c, Third );... else if (str.eualsignorecase( Go 1 )){ this.cl.show(this.c, First ); // To be continue... Rui Moreira 32 16

17 //... Continue (4/5) public class CardLayoutFrame extends JFrame implements MouseListener, WindowListener { // Implementing WindowListener public void windowactivated(windowevent e) { public void windowclosed(windowevent e) { // Implement only the Close button of JFrame - Exit public void windowclosing(windowevent e) { this.c.dispose(); // Dispose JFrame System.exit(0); // Exit Application public void windowdeactivated(windowevent e) { public void windowdeiconified(windowevent e) { public void windowiconified(windowevent e) { public void windowopened(windowevent e) { // To be continue... Rui Moreira 33 //... Continue (5/5) public class CardLayoutFrame extends JFrame implements MouseListener, WindowListener { //... // Run Application public static void main(string args[]){ CardLayoutFrame cf = new CardLayoutFrame( Test CardLayout ); cf.setsize(200, 200); cf.setvisible(true); // The End Rui Moreira 34 17

The AWT Event Model 9

The AWT Event Model 9 The AWT Event Model 9 Course Map This module covers the event-based GUI user input mechanism. Getting Started The Java Programming Language Basics Identifiers, Keywords, and Types Expressions and Flow

More information

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

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User CSE3461 Control Flow Paradigms: Reacting to the User Control Flow: Overview Definition of control flow: The sequence of execution of instructions in a program. Control flow is determined at run time by

More information

Dr. Hikmat A. M. AbdelJaber

Dr. Hikmat A. M. AbdelJaber Dr. Hikmat A. M. AbdelJaber GUI are event driven (i.e. when user interacts with a GUI component, the interaction (event) derives the program to perform a task). Event: click button, type in text field,

More information

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

Example Programs. COSC 3461 User Interfaces. GUI Program Organization. Outline. DemoHelloWorld.java DemoHelloWorld2.java DemoSwing. COSC User Interfaces Module 3 Sequential vs. Event-driven Programming Example Programs DemoLargestConsole.java DemoLargestGUI.java Demo programs will be available on the course web page. GUI Program Organization

More information

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

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT CSE 143 Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia http://www.cs.washington.edu/331/

More information

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

GUI Program Organization. Sequential vs. Event-driven Programming. Sequential Programming. Outline Sequential vs. Event-driven Programming Reacting to the user GUI Program Organization Let s digress briefly to examine the organization of our GUI programs We ll do this in stages, by examining three example

More information

Graphical Interfaces

Graphical Interfaces Weeks 9&11 Graphical Interfaces All the programs that you have created until now used a simple command line interface, which is not user friendly, so a Graphical User Interface (GUI) should be used. The

More information

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

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing CSEN401 Computer Programming Lab Topics: Graphical User Interface Window Interfaces using Swing Prof. Dr. Slim Abdennadher 22.3.2015 c S. Abdennadher 1 Swing c S. Abdennadher 2 AWT versus Swing Two basic

More information

Graphical Interfaces

Graphical Interfaces Weeks 11&12 Graphical Interfaces All the programs that you have created until now used a simple command line interface, which is not user friendly, so a Graphical User Interface (GUI) should be used. The

More information

Programming Languages and Techniques (CIS120e)

Programming Languages and Techniques (CIS120e) Programming Languages and Techniques (CIS120e) Lecture 29 Nov. 19, 2010 Swing I Event- driven programming Passive: ApplicaHon waits for an event to happen in the environment When an event occurs, the applicahon

More information

Graphical User Interfaces in Java - SWING

Graphical User Interfaces in Java - SWING Graphical User Interfaces in Java - SWING Graphical User Interfaces (GUI) Each graphical component that the user can see on the screen corresponds to an object of a class Component: Window Button Menu...

More information

Window Interfaces Using Swing Objects

Window Interfaces Using Swing Objects Chapter 12 Window Interfaces Using Swing Objects Event-Driven Programming and GUIs Swing Basics and a Simple Demo Program Layout Managers Buttons and Action Listeners Container Classes Text I/O for GUIs

More information

SD Module-1 Advanced JAVA

SD Module-1 Advanced JAVA Assignment No. 4 SD Module-1 Advanced JAVA R C (4) V T Total (10) Dated Sign Title: Transform the above system from command line system to GUI based application Problem Definition: Write a Java program

More information

Interacción con GUIs

Interacción con GUIs Interacción con GUIs Delegation Event Model Fuente EVENTO Oyente suscripción Fuente Oyente suscripción EVENTO Adaptador EVENTO java.lang.object java.util.eventobject java.awt.awtevent java.awt.event. ActionEvent

More information

SD Module-1 Advanced JAVA. Assignment No. 4

SD Module-1 Advanced JAVA. Assignment No. 4 SD Module-1 Advanced JAVA Assignment No. 4 Title :- Transform the above system from command line system to GUI based application Problem Definition: Write a Java program with the help of GUI based Application

More information

Systems Programming Graphical User Interfaces

Systems Programming Graphical User Interfaces Systems Programming Graphical User Interfaces Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: José Jesús García Rueda Systems Programming GUIs based on Java

More information

BM214E Object Oriented Programming Lecture 13

BM214E Object Oriented Programming Lecture 13 BM214E Object Oriented Programming Lecture 13 Events To understand how events work in Java, we have to look closely at how we use GUIs. When you interact with a GUI, there are many events taking place

More information

Chapter 1 GUI Applications

Chapter 1 GUI Applications Chapter 1 GUI Applications 1. GUI Applications So far we've seen GUI programs only in the context of Applets. But we can have GUI applications too. A GUI application will not have any of the security limitations

More information

Packages: Putting Classes Together

Packages: Putting Classes Together Packages: Putting Classes Together 1 Introduction 2 The main feature of OOP is its ability to support the reuse of code: Extending the classes (via inheritance) Extending interfaces The features in basic

More information

11/7/12. Discussion of Roulette Assignment. Objectives. Compiler s Names of Classes. GUI Review. Window Events

11/7/12. Discussion of Roulette Assignment. Objectives. Compiler s Names of Classes. GUI Review. Window Events Objectives Event Handling Animation Discussion of Roulette Assignment How easy/difficult to refactor for extensibility? Was it easier to add to your refactored code? Ø What would your refactored classes

More information

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

Handout 14 Graphical User Interface (GUI) with Swing, Event Handling Handout 12 CS603 Object-Oriented Programming Fall 15 Page 1 of 12 Handout 14 Graphical User Interface (GUI) with Swing, Event Handling The Swing library (javax.swing.*) Contains classes that implement

More information

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

Datenbank-Praktikum. Universität zu Lübeck Sommersemester 2006 Lecture: Swing. Ho Ngoc Duc 1 Datenbank-Praktikum Universität zu Lübeck Sommersemester 2006 Lecture: Swing Ho Ngoc Duc 1 Learning objectives GUI applications Font, Color, Image Running Applets as applications Swing Components q q Text

More information

(listener)... MouseListener, ActionLister. (adapter)... MouseAdapter, ActionAdapter. java.awt AWT Abstract Window Toolkit GUI

(listener)... MouseListener, ActionLister. (adapter)... MouseAdapter, ActionAdapter. java.awt AWT Abstract Window Toolkit GUI 51 6!! GUI(Graphical User Interface) java.awt javax.swing (component) GUI... (container) (listener)... MouseListener, ActionLister (adapter)... MouseAdapter, ActionAdapter 6.1 GUI(Graphics User Interface

More information

Programming Mobile Devices J2SE GUI

Programming Mobile Devices J2SE GUI Programming Mobile Devices J2SE GUI University of Innsbruck WS 2009/2010 thomas.strang@sti2.at Graphical User Interface (GUI) Why is there more than one Java GUI toolkit? AWT write once, test everywhere

More information

Introduction to the JAVA UI classes Advanced HCI IAT351

Introduction to the JAVA UI classes Advanced HCI IAT351 Introduction to the JAVA UI classes Advanced HCI IAT351 Week 3 Lecture 1 17.09.2012 Lyn Bartram lyn@sfu.ca About JFC and Swing JFC Java TM Foundation Classes Encompass a group of features for constructing

More information

GUI in Java TalentHome Solutions

GUI in Java TalentHome Solutions GUI in Java TalentHome Solutions AWT Stands for Abstract Window Toolkit API to develop GUI in java Has some predefined components Platform Dependent Heavy weight To use AWT, import java.awt.* Calculator

More information

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

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics Topic 9: Swing Outline Swing = Java's GUI library Swing is a BIG library Goal: cover basics give you concepts & tools for learning more Assignment 7: Expand moving shapes from Assignment 4 into game. "Programming

More information

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

The Abstract Windowing Toolkit. Java Foundation Classes. Swing. In April 1997, JavaSoft announced the Java Foundation Classes (JFC). The Abstract Windowing Toolkit Since Java was first released, its user interface facilities have been a significant weakness The Abstract Windowing Toolkit (AWT) was part of the JDK form the beginning,

More information

Graphical User Interfaces (GUIs)

Graphical User Interfaces (GUIs) CMSC 132: Object-Oriented Programming II Graphical User Interfaces (GUIs) Department of Computer Science University of Maryland, College Park Model-View-Controller (MVC) Model for GUI programming (Xerox

More information

Swing from A to Z Some Simple Components. Preface

Swing from A to Z Some Simple Components. Preface By Richard G. Baldwin baldwin.richard@iname.com Java Programming, Lecture Notes # 1005 July 31, 2000 Swing from A to Z Some Simple Components Preface Introduction Sample Program Interesting Code Fragments

More information

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

CSE 331. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT CSE 331 Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia http://www.cs.washington.edu/331/

More information

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

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver Building a GUI in Java with Swing CITS1001 extension notes Rachel Cardell-Oliver Lecture Outline 1. Swing components 2. Building a GUI 3. Animating the GUI 2 Swing A collection of classes of GUI components

More information

Window Interfaces Using Swing Objects

Window Interfaces Using Swing Objects Chapter 12 Window Interfaces Using Swing Objects Event-Driven Programming and GUIs Swing Basics and a Simple Demo Program Layout Managers Buttons and Action Listeners Container Classes Text I/O for GUIs

More information

GUI Design. Overview of Part 1 of the Course. Overview of Java GUI Programming

GUI Design. Overview of Part 1 of the Course. Overview of Java GUI Programming GUI Design Michael B. Spring Department of Information Science and Telecommunications University of Pittsburgh spring@imap.pitt.edu http://www.sis.pitt.edu /~spring Overview of Part 1 of the Course Demystifying

More information

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

Java & Graphical User Interface II. Wang Yang wyang AT njnet.edu.cn Java & Graphical User Interface II Wang Yang wyang AT njnet.edu.cn Outline Review of GUI (first part) What is Event Basic Elements of Event Programming Secret Weapon - Inner Class Full version of Event

More information

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

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more Swing = Java's GUI library Topic 9: Swing Swing is a BIG library Goal: cover basics give you concepts & tools for learning more Assignment 5: Will be an open-ended Swing project. "Programming Contest"

More information

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!

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! Swing = Java's GUI library Topic 9: Swing Swing is a BIG library Goal: cover basics give you concepts & tools for learning more Why are we studying Swing? 1. Useful & fun! 2. Good application of OOP techniques

More information

Lecture 5: Java Graphics

Lecture 5: Java Graphics Lecture 5: Java Graphics CS 62 Spring 2019 William Devanny & Alexandra Papoutsaki 1 New Unit Overview Graphical User Interfaces (GUI) Components, e.g., JButton, JTextField, JSlider, JChooser, Containers,

More information

GUI Programming: Swing and Event Handling

GUI Programming: Swing and Event Handling GUI Programming: Swing and Event Handling Sara Sprenkle 1 Announcements No class next Tuesday My Fourth of July present to you: No quiz! Assignment 3 due today Review Collections: List, Set, Map Inner

More information

Basicsof. JavaGUI and SWING

Basicsof. JavaGUI and SWING Basicsof programming3 JavaGUI and SWING GUI basics Basics of programming 3 BME IIT, Goldschmidt Balázs 2 GUI basics Mostly window-based applications Typically based on widgets small parts (buttons, scrollbars,

More information

Advanced Java Programming (17625) Event Handling. 20 Marks

Advanced Java Programming (17625) Event Handling. 20 Marks Advanced Java Programming (17625) Event Handling 20 Marks Specific Objectives To write event driven programs using the delegation event model. To write programs using adapter classes & the inner classes.

More information

Java. GUI building with the AWT

Java. GUI building with the AWT Java GUI building with the AWT AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many applications Uses the controls defined by your OS therefore

More information

PIC 20A GUI with swing

PIC 20A GUI with swing PIC 20A GUI with swing Ernest Ryu UCLA Mathematics Last edited: November 22, 2017 Hello swing Let s create a JFrame. import javax. swing.*; public class Test { public static void main ( String [] args

More information

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

All the Swing components start with J. The hierarchy diagram is shown below. JComponent is the base class. Q1. If you add a component to the CENTER of a border layout, which directions will the component stretch? A1. The component will stretch both horizontally and vertically. It will occupy the whole space

More information

กล ม API ท ใช. Programming Graphical User Interface (GUI) Containers and Components 22/05/60

กล ม API ท ใช. Programming Graphical User Interface (GUI) Containers and Components 22/05/60 กล ม API ท ใช Programming Graphical User Interface (GUI) AWT (Abstract Windowing Toolkit) และ Swing. AWT ม ต งต งแต JDK 1.0. ส วนมากจะเล กใช และแทนท โดยr Swing components. Swing API ปร บปร งความสามารถเพ

More information

Graphic User Interfaces. - GUI concepts - Swing - AWT

Graphic User Interfaces. - GUI concepts - Swing - AWT Graphic User Interfaces - GUI concepts - Swing - AWT 1 What is GUI Graphic User Interfaces are used in programs to communicate more efficiently with computer users MacOS MS Windows X Windows etc 2 Considerations

More information

Programming Language Concepts: Lecture 8

Programming Language Concepts: Lecture 8 Programming Language Concepts: Lecture 8 Madhavan Mukund Chennai Mathematical Institute madhavan@cmi.ac.in http://www.cmi.ac.in/~madhavan/courses/pl2009 PLC 2009, Lecture 8, 11 February 2009 GUIs and event

More information

COMPSCI 230. Software Design and Construction. Swing

COMPSCI 230. Software Design and Construction. Swing COMPSCI 230 Software Design and Construction Swing 1 2013-04-17 Recap: SWING DESIGN PRINCIPLES 1. GUI is built as containment hierarchy of widgets (i.e. the parent-child nesting relation between them)

More information

Table of Contents. Chapter 1 Getting Started with Java SE 7 1. Chapter 2 Exploring Class Members in Java 15. iii. Introduction of Java SE 7...

Table of Contents. Chapter 1 Getting Started with Java SE 7 1. Chapter 2 Exploring Class Members in Java 15. iii. Introduction of Java SE 7... Table of Contents Chapter 1 Getting Started with Java SE 7 1 Introduction of Java SE 7... 2 Exploring the Features of Java... 3 Exploring Features of Java SE 7... 4 Introducing Java Environment... 5 Explaining

More information

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

Starting Out with Java: From Control Structures Through Objects Sixth Edition Starting Out with Java: From Control Structures Through Objects Sixth Edition Chapter 12 A First Look at GUI Applications Chapter Topics 12.1 Introduction 12.2 Creating Windows 12.3 Equipping GUI Classes

More information

Event Driven Programming

Event Driven Programming Event Driven Programming 1. Objectives... 2 2. Definitions... 2 3. Event-Driven Style of Programming... 2 4. Event Polling Model... 3 5. Java's Event Delegation Model... 5 6. How to Implement an Event

More information

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7 PROGRAMMING DESIGN USING JAVA (ITT 303) Graphical User Interface Unit 7 Learning Objectives At the end of this unit students should be able to: Build graphical user interfaces Create and manipulate buttons,

More information

MIT AITI Swing Event Model Lecture 17

MIT AITI Swing Event Model Lecture 17 MIT AITI 2004 Swing Event Model Lecture 17 The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the user. But how do GUIs interact with users? How do applications

More information

G51PRG: Introduction to Programming Second semester Applets and graphics

G51PRG: Introduction to Programming Second semester Applets and graphics G51PRG: Introduction to Programming Second semester Applets and graphics Natasha Alechina School of Computer Science & IT nza@cs.nott.ac.uk Previous two lectures AWT and Swing Creating components and putting

More information

Java GUI Design: the Basics

Java GUI Design: the Basics Java GUI Design: the Basics Daniel Brady July 25, 2014 What is a GUI? A GUI is a graphical user interface, of course. What is a graphical user interface? A graphical user interface is simply a visual way

More information

CS 251 Intermediate Programming GUIs: Event Listeners

CS 251 Intermediate Programming GUIs: Event Listeners CS 251 Intermediate Programming GUIs: Event Listeners Brooke Chenoweth University of New Mexico Fall 2017 What is an Event Listener? A small class that implements a particular listener interface. Listener

More information

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

CSE 331. Event- driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT CSE 331 Event- driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT Lecturer: Michael Hotan slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer,

More information

Java Programming Lecture 6

Java Programming Lecture 6 Java Programming Lecture 6 Alice E. Fischer Feb 15, 2013 Java Programming - L6... 1/32 Dialog Boxes Class Derivation The First Swing Programs: Snow and Moving The Second Swing Program: Smile Swing Components

More information

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming Objectives: Last revised 1/15/10 1. To introduce the notion of a component and some basic Swing components (JLabel, JTextField, JTextArea,

More information

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

Course Status Networking GUI Wrap-up. CS Java. Introduction to Java. Andy Mroczkowski CS 190 - Java Introduction to Java Andy Mroczkowski uamroczk@cs.drexel.edu Department of Computer Science Drexel University March 10, 2008 / Lecture 8 Outline Course Status Course Information & Schedule

More information

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming Objectives: Last revised March 2, 2017 1. To introduce the notion of a component and some basic Swing components (JLabel, JTextField,

More information

14.2 Java s New Nimbus Look-and-Feel 551 Sample GUI: The SwingSet3 Demo Application As an example of a GUI, consider Fig. 14.1, which shows the SwingS

14.2 Java s New Nimbus Look-and-Feel 551 Sample GUI: The SwingSet3 Demo Application As an example of a GUI, consider Fig. 14.1, which shows the SwingS 550 Chapter 14 GUI Components: Part 1 14.1 Introduction 14.2 Java s New Nimbus Look-and-Feel 14.3 Simple GUI-Based Input/Output with JOptionPane 14.4 Overview of Swing Components 14.5 Displaying Text and

More information

GUI Event Handling 11. GUI Event Handling. Objectives. What is an Event? Hierarchical Model (JDK1.0) Delegation Model (JDK1.1)

GUI Event Handling 11. GUI Event Handling. Objectives. What is an Event? Hierarchical Model (JDK1.0) Delegation Model (JDK1.1) Objectives Write code to handle events that occur in a GUI 11 GUI Event Handling Describe the concept of adapter classes, including how and when to use them Determine the user action that originated the

More information

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

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling Frames, GUI and events Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling Introduction to Swing The Java AWT (Abstract Window Toolkit)

More information

AP CS Unit 11: Graphics and Events

AP CS Unit 11: Graphics and Events AP CS Unit 11: Graphics and Events This packet shows how to create programs with a graphical interface in a way that is consistent with the approach used in the Elevens program. Copy the following two

More information

Agenda. Container and Component

Agenda. Container and Component Agenda Types of GUI classes/objects Step-by-step guide to create a graphic user interface Step-by-step guide to event-handling PS5 Problem 1 PS5 Problem 2 Container and Component There are two types of

More information

CS11 Java. Fall Lecture 4

CS11 Java. Fall Lecture 4 CS11 Java Fall 2006-2007 Lecture 4 Today s Topics Interfaces The Swing API Event Handlers Inner Classes Arrays Java Interfaces Classes can only have one parent class No multiple inheritance in Java! By

More information

CONTENTS. Chapter 1 Getting Started with Java SE 6 1. Chapter 2 Exploring Variables, Data Types, Operators and Arrays 13

CONTENTS. Chapter 1 Getting Started with Java SE 6 1. Chapter 2 Exploring Variables, Data Types, Operators and Arrays 13 CONTENTS Chapter 1 Getting Started with Java SE 6 1 Introduction of Java SE 6... 3 Desktop Improvements... 3 Core Improvements... 4 Getting and Installing Java... 5 A Simple Java Program... 10 Compiling

More information

Java: Graphical User Interfaces (GUI)

Java: Graphical User Interfaces (GUI) Chair of Software Engineering Carlo A. Furia, Marco Piccioni, and Bertrand Meyer Java: Graphical User Interfaces (GUI) With material from Christoph Angerer The essence of the Java Graphics API Application

More information

GUI Basics. Object Orientated Programming in Java. Benjamin Kenwright

GUI Basics. Object Orientated Programming in Java. Benjamin Kenwright GUI Basics Object Orientated Programming in Java Benjamin Kenwright Outline Essential Graphical User Interface (GUI) Concepts Libraries, Implementation, Mechanics,.. Abstract Windowing Toolkit (AWT) Java

More information

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

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread G51PGP Programming Paradigms Lecture 008 Inner classes, anonymous classes, Swing worker thread 1 Reminder subtype polymorphism public class TestAnimals public static void main(string[] args) Animal[] animals

More information

Java - Applications. The following code sets up an application with a drop down menu, as yet the menu does not do anything.

Java - Applications. The following code sets up an application with a drop down menu, as yet the menu does not do anything. Java - Applications C&G Criteria: 5.3.2, 5.5.2, 5.5.3 Java applets require a web browser to run independently of the Java IDE. The Dos based console applications will run outside the IDE but have no access

More information

Lecture 3: Java Graphics & Events

Lecture 3: Java Graphics & Events Lecture 3: Java Graphics & Events CS 62 Fall 2017 Kim Bruce & Alexandra Papoutsaki Text Input Scanner class Constructor: myscanner = new Scanner(System.in); can use file instead of System.in new Scanner(new

More information

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

Together, the appearance and how user interacts with the program are known as the program look and feel. Lecture 10 Graphical User Interfaces A graphical user interface is a visual interface to a program. GUIs are built from GUI components (buttons, menus, labels etc). A GUI component is an object with which

More information

COSC 123 Computer Creativity. Graphics and Events. Dr. Ramon Lawrence University of British Columbia Okanagan

COSC 123 Computer Creativity. Graphics and Events. Dr. Ramon Lawrence University of British Columbia Okanagan COSC 123 Computer Creativity Graphics and Events Dr. Ramon Lawrence University of British Columbia Okanagan ramon.lawrence@ubc.ca Key Points 1) Draw shapes, text in various fonts, and colors. 2) Build

More information

BASICS OF GRAPHICAL APPS

BASICS OF GRAPHICAL APPS CSC 2014 Java Bootcamp Lecture 7 GUI Design BASICS OF GRAPHICAL APPS 2 Graphical Applications So far we ve focused on command-line applications, which interact with the user using simple text prompts In

More information

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

Lab 4. D0010E Object-Oriented Programming and Design. Today s lecture. GUI programming in Lab 4 D0010E Object-Oriented Programming and Design Lecture 9 Lab 4: You will implement a game that can be played over the Internet. The networking part has already been written. Among other things, the

More information

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

KF5008 Program Design & Development. Lecture 1 Usability GUI Design and Implementation KF5008 Program Design & Development Lecture 1 Usability GUI Design and Implementation Types of Requirements Functional Requirements What the system does or is expected to do Non-functional Requirements

More information

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

Chapter 8. Java continued. CS Hugh Anderson s notes. Page number: 264 ALERT. MCQ test next week. This time. This place. Chapter 8 Java continued CS3283 - Hugh Anderson s notes. Page number: 263 ALERT MCQ test next week This time This place Closed book CS3283 - Hugh Anderson s notes. Page number: 264 ALERT Assignment #2

More information

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

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 Chapter 8 Java continued MCQ test next week This time This place Closed book ALERT CS3283 - Hugh Anderson s notes. Page number: 263 CS3283 - Hugh Anderson s notes. Page number: 264 ALERT Last week Assignment

More information

17 GUI API: Container 18 Hello world with a GUI 19 GUI API: JLabel 20 GUI API: Container: add() 21 Hello world with a GUI 22 GUI API: JFrame: setdefau

17 GUI API: Container 18 Hello world with a GUI 19 GUI API: JLabel 20 GUI API: Container: add() 21 Hello world with a GUI 22 GUI API: JFrame: setdefau List of Slides 1 Title 2 Chapter 13: Graphical user interfaces 3 Chapter aims 4 Section 2: Example:Hello world with a GUI 5 Aim 6 Hello world with a GUI 7 Hello world with a GUI 8 Package: java.awt and

More information

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

Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008 Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008 Announcements A3 is up, due Friday, Oct 10 Prelim 1 scheduled for Oct 16 if you have a conflict, let us know now 2 Interactive

More information

Java AWT Windows, Text, & Graphics

Java AWT Windows, Text, & Graphics 2 AWT Java AWT Windows, Text, & Graphics The Abstract Windows Toolkit (AWT) contains numerous classes and methods that allow you to create and manage applet windows and standard windows that run in a GUI

More information

Virtualians.ning.pk. 2 - Java program code is compiled into form called 1. Machine code 2. native Code 3. Byte Code (From Lectuer # 2) 4.

Virtualians.ning.pk. 2 - Java program code is compiled into form called 1. Machine code 2. native Code 3. Byte Code (From Lectuer # 2) 4. 1 - What if the main method is declared as private? 1. The program does not compile 2. The program compiles but does not run 3. The program compiles and runs properly ( From Lectuer # 2) 4. The program

More information

User interfaces and Swing

User interfaces and Swing User interfaces and Swing Overview, applets, drawing, action listening, layout managers. APIs: java.awt.*, javax.swing.*, classes names start with a J. Java Lectures 1 2 Applets public class Simple extends

More information

(Incomplete) History of GUIs

(Incomplete) History of GUIs CMSC 433 Programming Language Technologies and Paradigms Spring 2004 Graphical User Interfaces April 20, 2004 (Incomplete) History of GUIs 1973: Xerox Alto 3-button mouse, bit-mapped display, windows 1981:

More information

Chapter 6: Graphical User Interfaces

Chapter 6: Graphical User Interfaces Chapter 6: Graphical User Interfaces CS 121 Department of Computer Science College of Engineering Boise State University April 21, 2015 Chapter 6: Graphical User Interfaces CS 121 1 / 36 Chapter 6 Topics

More information

Course: CMPT 101/104 E.100 Thursday, November 23, 2000

Course: CMPT 101/104 E.100 Thursday, November 23, 2000 Course: CMPT 101/104 E.100 Thursday, November 23, 2000 Lecture Overview: Week 12 Announcements Assignment 6 Expectations Understand Events and the Java Event Model Event Handlers Get mouse and text input

More information

OBJECT ORIENTED PROGRAMMING. Java GUI part 1 Loredana STANCIU Room B616

OBJECT ORIENTED PROGRAMMING. Java GUI part 1 Loredana STANCIU Room B616 OBJECT ORIENTED PROGRAMMING Java GUI part 1 Loredana STANCIU loredana.stanciu@upt.ro Room B616 What is a user interface That part of a program that interacts with the user of the program: simple command-line

More information

CS11 Java. Fall Lecture 3

CS11 Java. Fall Lecture 3 CS11 Java Fall 2014-2015 Lecture 3 Today s Topics! Class inheritance! Abstract classes! Polymorphism! Introduction to Swing API and event-handling! Nested and inner classes Class Inheritance! A third of

More information

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

Graphics. Lecture 18 COP 3252 Summer June 6, 2017 Graphics Lecture 18 COP 3252 Summer 2017 June 6, 2017 Graphics classes In the original version of Java, graphics components were in the AWT library (Abstract Windows Toolkit) Was okay for developing simple

More information

SampleApp.java. Page 1

SampleApp.java. Page 1 SampleApp.java 1 package msoe.se2030.sequence; 2 3 /** 4 * This app creates a UI and processes data 5 * @author hornick 6 */ 7 public class SampleApp { 8 private UserInterface ui; // the UI for this program

More information

Jonathan Aldrich Charlie Garrod

Jonathan Aldrich Charlie Garrod Principles of Software Construction: Objects, Design, and Concurrency (Part 3: Design Case Studies) Introduction to GUIs Jonathan Aldrich Charlie Garrod School of Computer Science 1 Administrivia Homework

More information

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

Part 3: Graphical User Interface (GUI) & Java Applets 1,QWURGXFWLRQWR-DYD3URJUDPPLQJ (( Part 3: Graphical User Interface (GUI) & Java Applets EE905-GUI 7RSLFV Creating a Window Panels Event Handling Swing GUI Components ƒ Layout Management ƒ Text Field ƒ

More information

Previously, we have seen GUI components, their relationships, containers, layout managers. Now we will see how to paint graphics on GUI components

Previously, we have seen GUI components, their relationships, containers, layout managers. Now we will see how to paint graphics on GUI components CS112-Section2 Hakan Guldas Burcin Ozcan Meltem Kaya Muge Celiktas Notes of 6-8 May Graphics Previously, we have seen GUI components, their relationships, containers, layout managers. Now we will see how

More information

Marcin Luckner Warsaw University of Technology Faculty of Mathematics and Information Science

Marcin Luckner Warsaw University of Technology Faculty of Mathematics and Information Science Marcin Luckner Warsaw University of Technology Faculty of Mathematics and Information Science mluckner@mini.pw.edu.pl http://www.mini.pw.edu.pl/~lucknerm } Abstract Window Toolkit Delegates creation and

More information

Graphical User Interfaces. Comp 152

Graphical User Interfaces. Comp 152 Graphical User Interfaces Comp 152 Procedural programming Execute line of code at a time Allowing for selection and repetition Call one function and then another. Can trace program execution on paper from

More information

Building Java Programs Bonus Slides

Building Java Programs Bonus Slides Building Java Programs Bonus Slides Graphical User Interfaces Copyright (c) Pearson 2013. All rights reserved. Graphical input and output with JOptionPane JOptionPane An option pane is a simple dialog

More information

1005ICT Object Oriented Programming Lecture Notes

1005ICT Object Oriented Programming Lecture Notes 1005ICT Object Oriented Programming Lecture Notes School of Information and Communication Technology Griffith University Semester 2, 2015 1 20 GUI Components and Events This section develops a program

More information

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

Swing from A to Z Using Focus in Swing, Part 2. Preface Swing from A to Z Using Focus in Swing, Part 2 By Richard G. Baldwin Java Programming, Lecture Notes # 1042 November 27, 2000 Preface Introduction Sample Program Interesting Code Fragments Summary What's

More information