Part I: Learn Common Graphics Components

Similar documents
Introduction to Graphical Interface Programming in Java. Introduction to AWT and Swing

Containers and Components

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

Graphical User Interfaces. Comp 152

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!

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

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

Agenda. Container and Component

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

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

Window Interfaces Using Swing Objects

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

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

Graphical User Interfaces (GUIs)

Swing/GUI Cheat Sheet

Window Interfaces Using Swing Objects

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

Class 16: The Swing Event Model

Java Programming Lecture 6

Dr. Hikmat A. M. AbdelJaber

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

AP CS Unit 11: Graphics and Events

Introduction to the JAVA UI classes Advanced HCI IAT351

Introduction. Introduction

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

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

Graphic User Interfaces. - GUI concepts - Swing - AWT

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

Graphical User Interfaces

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

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

SINGLE EVENT HANDLING

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

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

encompass a group of features for building Graphical User Interfaces (GUI).

Welcome to CIS 068! 1. GUIs: JAVA Swing 2. (Streams and Files we ll not cover this in this semester, just a review) CIS 068

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

Graphical User Interfaces in Java - SWING

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

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

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

To gain experience using GUI components and listeners.

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

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Building Java Programs Bonus Slides

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

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

Java. GUI building with the AWT

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

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

Graphical User Interface (GUI)

CSC 161 SPRING 17 LAB 2-1 BORDERLAYOUT, GRIDLAYOUT, AND EVENT HANDLING

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

GUI Applications. Let s start with a simple Swing application in Java, and then we will look at the same application in Jython. See Listing 16-1.

CS 251 Intermediate Programming GUIs: Components and Layout

CS 2113 Software Engineering

2110: GUIS: Graphical User Interfaces

CS 251 Intermediate Programming GUIs: Event Listeners

Systems Programming Graphical User Interfaces

PIC 20A GUI with swing

Chapter 7: A First Look at GUI Applications

Class 14: Introduction to the Swing Toolkit

Interfaces & Polymorphism part 2: Collections, Comparators, and More fun with Java graphics

Programming graphics

Chapter 6: Graphical User Interfaces

Hanley s Survival Guide for Visual Applications with NetBeans 2.0 Last Updated: 5/20/2015 TABLE OF CONTENTS

Solution register itself

Graphical User Interfaces in Java

DM503 Programming B. Peter Schneider-Kamp.

Window Interfaces Using Swing. Chapter 12

1.00 Lecture 14. Lecture Preview

G51PGP Programming Paradigms. Lecture 009 Concurrency, exceptions

SE1021 Exam 2. When returning your exam, place your note-sheet on top. Page 1: This cover. Page 2 (Multiple choice): 10pts

CSSE 220 Day 19. Object-Oriented Design Files & Exceptions. Check out FilesAndExceptions from SVN

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

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

TTTK Program Design and Problem Solving Tutorial 3 (GUI & Event Handlings)

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

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Chapter 12 GUI Basics

Graphical User Interface (GUI) and Object- Oriented Design (OOD)

1005ICT Object Oriented Programming Lecture Notes

CSSE 220 Day 19. Object-Oriented Design Files & Exceptions. Check out FilesAndExceptions from SVN

Unit 6: Graphical User Interface

Jonathan Aldrich Charlie Garrod

Commands. Written commands can be reused, cataloged, etc. Sometimes they also contain "undo" commands, too.

Java: Graphical User Interfaces (GUI)

Building Graphical User Interfaces. GUI Principles

Adding Buttons to StyleOptions.java

Basicsof. JavaGUI and SWING

Building Graphical User Interfaces. Overview

Swing: Building GUIs in Java

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

Swing: Building GUIs in Java

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

Overview. Building Graphical User Interfaces. GUI Principles. AWT and Swing. Constructing GUIs Interface components GUI layout Event handling

Introduction to concurrency and GUIs

CHAPTER 2. Java Overview

Top-Level Containers

Transcription:

OOP GUI Components and Event Handling Page 1 Objectives 1. Practice creating and using graphical components. 2. Practice adding Event Listeners to handle the events and do something. 3. Learn how to connect a graphical interface to the rest of your application. What to Submit 1. Basic Program Structure Part I: Learn Common Graphics Components To see the basic layout of the code for a GUI app, create a JFrame containing only a single JButton. import javax.swing.*; import java.awt.*; import java.awt.event.actionevent; Import classes from AWT and Swing public class ComponentDemo { private JFrame frame; // attributes for components private JButton button; Declare attributes for components your application needs to access. Some components can be declared as local vars. public ComponentDemo( ) { frame = new JFrame("Demo"); frame.setdefaultcloseoperation( JFrame.EXIT_ON_CLOSE ); initcomponents( ); Constructor usually does: (1) initialize attributes (2) set some properties of the window (3) call initcomponents /** initialize components in the window */ private void initcomponents() { // (1) create components for the UI & set properties (color, font...) // (2) position components using layout managers and containers // (3) add event listeners to components (Problem 2) button = new JButton("Press Me"); frame.add( button ); frame.pack( ); /** Display the graphics window. */ public void run() { frame.setvisible( true ); public static void main(string [] args) { ComponentDemo demo = new ComponentDemo(); demo.run(); System.out.println("Done launching window. Do you see it?"); 2. Add an Event Listener When the user does something in a graphical application, the graphics system dispatches an Event. Your program must tell the graphics system what code it should call when an event occurs. This code is called an "event listener" or "event handler" and it must implement a specific interface. Let's add an event listener to the JButton so we are notified when the button is pressed. 2.1 Define an inner class that implements ActionListener (the interface for button events). The interface has only one method, as shown here. // this class is inside the ComponentDemo class. Its an "inner class". class ButtonListener implements ActionListener { /** this method receives events */ public void actionperformed( ActionEvent event ) {

OOP GUI Components and Event Handling Page 2 System.out.println("Ouch! Don't press so hard"); // end of inner class 2.2 In the initcomponents() method, add a ButtonListener to the button component: /** initialize components in the window */ private void initcomponents() { button = new JButton("Press Me"); ButtonListener listener = new ButtonListener(); button.addactionlistener( listener );... 2.3 Test the program -- press the button and see that it works. 3. Layout Multiple Components Java uses a Layout manager to arrange and manage components. The default layout manager for JFrame is BorderLayout, which divides window into 5 regions North, Center, East, West, and South. Components are resized to fill the entire region. To see this, add some labels and a Textfield to the ComponentDemo class. public class ComponentDemo { //Add a label and textfield private JLabel counter; // count button clicks private JTextfield message; // display a message private void initcomponents() { button = new JButton("Press Me"); ButtonListener listener = new ButtonListener(); button.addactionlistener( listener ); counter = new JLabel(" 0"); message = new JTextfield("Make my day."); frame.add(button); // default location in CENTER frame.add( counter, BorderLayout.WEST ); frame.add( message, BorderLayout.SOUTH );... 3.1 Count Button Presses Modify the ButtonListener to do: a) count how many times the button is pressed and display the value in counter JLabel. b) print a message in the JTextfield (message) instead of System.out. 3.2 Add Your Own Components to the EAST and NORTH. 4. Set Component Properties (Make your UI Beautiful) Most components support these properties: setforeground( Color ) foreground color (text color) setbackground( Color ) background color setenabled( boolean ) true = enable, false = disable setborder( Border ) draw a border around component setfont( Font ) font for showing text

OOP GUI Components and Event Handling Page 3 settooltiptext( "press here" ) shown when mouse is over component Text fields like JTextField also have these properties: seteditable( boolean ) false if field is "read only" sethorizontalalignment(jtextfield.center) LEFT, CENTER, or RIGHT Visual Graphics Editor for Eclipse If you are using Eclipse, then use WindowBuilder Editor to do this exercise. 1. Close the editor window for ComponentDemo 2. Right-click on ComponentDemo and choose "Open with... WindowBuilder Editor" 3. At the bottom of the editor window there are 2 tabs: "Source" and "Design". In Source view you can edit the Java source code. In Design view, use a graphical Window Builder to create the UI and set component properties. 4. In Design view, select (click) a component then edit its properties using the Properties panel as shown here. 5. You can switch to Source view at any time and see the code that Window Builder has generated for you. You should Save changes before switching views. How to Create Fonts and Colors in Java: to write the code yourself (instead of using Window Builder), see the end of this handout. you 5. Fix Ugly Layout: Arrange Components in a JPanel inside the JFrame The components look ugly. BorderLayout resizes the components to fill all the space available. We fix this by putting some components in their own container (called a JPanel) and then add the JPanel to the JFrame. The idea is like this: 1) Create a JPanel in initcomponents. JPanel panel = new JPanel(); 2) Put components inside the JPanel (not frame). The default Layout for JPanel is FlowLayout so the components will have their preferred sizes. panel.add( label ); panel.add( counter ); panel.add( button ); 3) Add the panel to the JFrame (actually it gets added to the JFrame's contentpane). frame.add( panel, BorderLayout.CENTER ); 4) (Optional) Add a Border so that the JPanel is visually distinct. panel.setborder( new BevelBorder(BevelBorder.LOWERED) ); Swing has many Border types. Other borders are EtchedBorder and TitledBorder. can

OOP GUI Components and Event Handling Page 4 6. Be a JFrame In ComponentDemo we created a JFrame as an attribute in the ComponentDemo class. Another way to create a UI is to extend the JFrame class. Then your UI is a JFrame. Both ways ("has a JFrame" and "be a JFrame") have advantages, so you should know how to write both. To be a JFrame requires only a small change in the code you already wrote: public class ComponentDemo extends JFrame { private JFrame frame; // attributes for components private JButton button; public ComponentDemo( ) { super("demo"); // set the title frame = this; // you don't need the frame variable, but its convenient initcomponents(); Since frame now refers to this object, you can use this in your code. For example: /** Display the graphics window. */ public void run() { this.setvisible( true ); 7. Try Different Layout Managers The default layout for JFrame is BorderLayout. You can change the layout using the command: frame.setlayout( somelayoutmanager ) Try these layouts and see the difference: GridLayout arranges components in a grid and makes them the same size LayoutManager layout = new GridLayout( rows, columns ); //try: (2,3) frame.setlayout(layout); frame.add(button); frame.add( new JLabel("Express Yourself"); frame.add( new JTextfield(10) );... FlowLayout flows components to use available space and does not resize components. This is the default layout for JPanel. LayoutManager layout = new FlowLayout( ); // other code same as above BoxLayout: Horizontal layout is BoxLayout.X_AXIS, vertical layout is BoxLayout.Y_AXIS. layout = new BoxLayout( contents, BoxLayout.X_AXIS ); GridBagLayout: This is one of the best, most flexible layouts and not too hard to use, but you need to study it a little. On your own time, please read the Java Tutorial for GridBagLayout.

OOP GUI Components and Event Handling Page 5 Part II: Add a GUI as a User Interface in the GuessingGame In the Guessing Game you divided the game into 2 classes: Game (the game itself) and GameConsole (user interface). You assigned different responsibilities to separate classes. A benefit of this is that you can change the user interface and still reuse the same Game code. Apply this design to create a Graphical UI for the Guessing Game. User Interface or View: the top layer handles interaction with the user, manages the display and receives input events. Controller handles processing of user requests. It knows how the application should behave; e.g. stop when the user guesses the secret. It also knows how to translate user input into method calls for the domain layer. Domain Layer or Model contains logic used by application, provides "domain objects" and services. MyActionListener actionevent( event ) Game guess( number ) gethint( ): String getcount( ): int 1. Create a GUI Interface for the GuessingGame Use what you learned in Part 1 of this lab to create a GUI for playing the guessing game. The GUI needs a reference to the game, just like the GameConsole interface you wrote before: GameUI gui = new GameUI( game ); WRONG: The GameUI should not create a Game. class GameUI extends JFrame { private Game game; public GameUI( ) { // WRONG. Don't write this. UI does not create domain objects. game = new Game(100); 2. Use JPanel to Separate the Input Components Example: JPanel panel = new JPanel(); panel.add( label ); // a label panel.add( inputfield ); panel.add( enterbutten ); panel.add( quitbutton ); contents.add ( panel, BorderLayout.CENTER ); 3. Write a Main Class to Connect Components Together Just like in the original Guessing Game, write a separate Main class to create the objects and connect them together.

OOP GUI Components and Event Handling Page 6 JLabel displays text and/or an image: Common Graphics Components JLabel label = new JLabel("A nice label"); ImageIcon icon = new ImageIcon("c:/images/1baht.png"); JLabel label = new JLabel(icon); JButton displays text and/or an image. You can press it. JButton button = new JButton("Press Me"); ImageIcon coin = new ImageIcon("c:/images/1baht.png"); JButton button2 = new JButton( "One Baht", coin ); JTextField for inputting text: JTextField textfield = new JTextField( 12 ); // 12 is width JComboBox to select one item from choices // you can put any kind of Objects in a combo box String [] fruit = {"Apple", "Banana", "Orange"; JComboBox combobox = new JComboBox( fruit ); JTextArea is a text box with more than one row: JTextArea textarea = new JTextArea( rows, columns ); textarea.setlinewrap(true); textarea.setwrapstyleword(true); JSlider has a min and max. It can also have labels and tick marks. (See Java Tutorial) JSlider slider = new JSlider( 0, 100 ); // min 0, max 100 slider.setvalue( 50 ); // current value slider.setmajortickspacing( 25 ); slider.setpaintlabels( true ); The Java Tutorial has more component examples: http://docs.oracle.com/javase//tutorial/ui/features/components.html How to Create a Color The Color class has predefined colors: Color.RED, Color.BLUE, Color.GREY, Color.CYAN, etc. You can create your own colors using: Color color = new Color( red, green, blue) // red, green, blue are int between 0 and 255 Color darkgreen = new Color( 0, 100, 0); // red = 0, green = 100, blue = 0 Color orange = new Color( 255, 165, 0); // maximum red + some green Color torquiose = new Color( 0, 245, 255); // green and blue How to Create a Font Font font = new Font( name, style, size ); Font arial = new Font("Arial", Font.PLAIN, 24); name String font name like "Arial", "Angsana New", or an OS-independent font name: Font.DIALOG, Font.DIALOG_INPUT, Font.MONOSPACED, Font.SERIF, or Font.SANS_SERIF style one of Font.PLAIN, Font.BOLD, Font.ITALIC. (integer constants) size font size in points. Like in a word processor.

OOP GUI Components and Event Handling Page 7 Event Listeners Use an Event Listener so that your code will be notified when something happens in the graphical interface. 1) create an ActionListener me : MyActionListener actionperformed( event ) 2) add Listener to a Button button.addactionlistener( me ); 3) wait for an event Click! Writing an ActionListener There are 3 common ways to write event listeners: create an inner class in the GUI that implements the event listener. The advantage of an inner class is that it can access members its outer class, even private attributes and private methods. create an anonymous class and immediately assign it to a variable or component as a listener use the GUI class itself as the event listener; e.g. declare the GUI implements ActionListener Here is how to write an inner class to implement ActionListener. This example displays info about the event on the console so you can see what is passed as the ActionEvent parameter. class ButtonListener implements ActionListener { private int count = 0; // count the events public void actionperformed( ActionEvent event ) { // which component caused the event? Object source = event.getsource(); // e.g. a JButton object System.out.println( "Event source: " + source.getclass().getname()); // what event or command occurred? String cmd = event.getactioncommand(); System.out.println("Action command: " + cmd); // count the event and show the count count = count + 1; label.settext( Integer.toString(count) ); You must add the ActionListener to your components. You can use the same listener object as listener for several components. ActionListener listener = new ButtonListener(); button.addactionlistener( listener ); textfield.addactionlistener( listener ); // reuse same listener Other Kinds of Event Listener Different components require different kinds of event listeners. For example, a JSlider uses a ChangeListener. To write a ChangeListener for a JSlider: /** a listener for slider */ 4) Java calls your code actionperformed( ActionEvent evt )

OOP GUI Components and Event Handling Page 8 class SliderListener implements ChangeListener { public void statechanged(changeevent e) { JSlider slider = (JSlider) e.getsource(); int value = slider.getvalue(); System.out.println( "slider value is "+ value ); In your initcomponents() method you would add a change listener to a slider using: slider.addchangelistener( new SliderListener() ); The Recommended Way to Launch Swing Applications Swing uses threads. This is why a GUI window keep running even after your main() method has finished. Swing may use several threads, but one thread is special: the Event Dispatcher Thread. This thread performs event handling, which includes responding to all user input. Java has two rules for using theads in Swing: 1. all UI updates should be performed only in the event dispatcher thread. 2. time consuming or long running tasks should not be performed in the event dispatcher thread. Rule 1 means we should start our Swing UI in the event dispatcher thread. But how? The SwingUtility class has method that will insure your code is run in the event dispatcher thread. The typical way of using it is this: public static void main(string [] args) { /* Create and display the form */ SwingUtilities.invokeLater( new Runnable() { public void run() { new ComponentDemo().setVisible(true); ); SwingUtilities.invokeLater() starts the Runnable and returns immediately. As an alternative, you can call SwingUtilities.invokeAndWait() which will wait until the Runnable's run() method exits. In cases where you need to apply Rule #2 (run a task on another thread and communicate the results to the event dispatcher thread so they show on the UI), use the SwingWorker class.