Part 1: Intro to GUI & events

Slides:



Advertisements
Similar presentations
CS18000: Problem Solving and Object-Oriented Programming.
Advertisements

Graphic User Interfaces Layout Managers Event Handling.
CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
Corresponds with Chapter 12
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Java GUI Libraries Swing Programming. Swing Components Swing is a collection of libraries that contains primitive widgets or controls used for designing.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Graphical User Interface (GUI) Programming III. Lecture Objectives Exploring more GUI programming elements in Java Using labels in GUIs Using colors to.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Swing CS-328 Dick Steflik John Margulies. Swing vs AWT AWT is Java’s original set of classes for building GUIs Uses peer components of the OS; heavyweight.
1 GUI programming Intro to GUI & events. 2 GUI programming GUI stands for graphical user interface In Java, two packages are involved in creating GUI.
GUI and Event-Driven Programming Recitation – 3/6/2009 CS 180 Department of Computer Science, Purdue University.
Chapter 121 Window Interfaces Using Swing Chapter 12.
CS102--Object Oriented Programming Lecture 19: – The Swing Package (II) Copyright © 2008 Xiaoyan Li.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Scott Grissom, copyright 2006Ch 11: GUI Slide 1 Graphical User Interfaces (Ch 11) Careful design of a graphical user interface is key to a viable software.
GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
GUI and event-driven programming An introduction.
Java Swing. Introduction to Swing A GUI (graphical user interface) is a windowing system that interacts with the user The Java AWT (Abstract Window Toolkit)
Java Programming Chapter 10 Graphical User Interfaces.
Introduction to GUI Java offers a great number of pre-defined classes to support the development of graphical user interfaces –These are broken down into.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
CSE 219 Computer Science III Graphical User Interface.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
Comp 249 Programming Methodology Chapter 17 Swing I Prof. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
University of Limerick1 Software Architecture Java Layout Managers.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Computer Science 209 GUIs Model/View/Controller Layouts.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Lecture # 6 Graphical User Interface(GUI). Introduction A graphical user interface (GUI) presents a user- friendly mechanism for interacting with an application.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Lesson 28: More on the GUI button, frame and actions.
Graphical User Interface (GUI)
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
GUIs and Events Rick Mercer.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
CSC 205 Programming II Lecture 5 AWT - I.
Graphical User Interfaces
Swing JComponents.
A First Look at GUI Applications
GUIs Model/View/Controller Layouts
Graphical User Interface (GUI) Programming III
A Quick Java Swing Tutorial
Ellen Walker Hiram College
Chapter 13: Advanced GUIs and Graphics
A Quick Java Swing Tutorial
Window Interfaces Using Swing Objects Chapter 12
CiS 260: App Dev I Chapter 6: GUI and OOD.
Advanced GUIs and Graphics
Graphical User Interface
Presentation transcript:

Part 1: Intro to GUI & events GUI programming Part 1: Intro to GUI & events

GUI programming GUI stands for graphical user interface In Java, two packages are involved in creating GUI programs: AWT (abstract window toolkit), the original suite of GUI classes in Java Swing, an updated and expanded version of AWT; replaces some but not all of the original AWT classes

Event-driven programming Style of programming that uses a signal and response approach to control flow, rather than the standard sequential approach Objects react to events Programs often proceed in a non-linear fashion

Event-driven programming Event: an object that is created by an action, such as a mouse click Listener: an object that performs an action in response to an event Exception handling follows this model: A thrown exception is an event The catch block that corresponds to the event is a listener

Event-driven programming An event component (such as a button) may have from zero to several listeners Each listener can respond to a different type of event (such as mouse movement vs. mouse click), or several listeners might be programmed to respond to the same event Listener objects respond to events by invoking methods called event handlers

An event is: A. An action B. A method C. A class D. An object

How it works As a programmer, you are not so much concerned with the sequence of events as with the interaction of objects You create objects (such as buttons) that can fire events, and you create listeners that can respond to events The events themselves determine the order in which things proceed in your program

How it works The Swing library makes extensive use of the inheritance mechanism Certain methods are automatically invoked when events are fired You will often write methods that are never called by any code you write; instead, the methods are called automatically when triggered by event objects

JFrame – a simple window The window shown below is an example of a basic Swing object, the JFrame: The code on the following slide was used to create this window

import javax.swing.*; // note the x! import java.awt.*; public class JFrameEx1 extends JFrame { // class inherits variables, // constants and methods from public JFrameEx1 () // parent class JFrame setTitle("I've been framed!"); setSize(300, 200); // width is 300 pixels, height is 200 pixels setLocation(150, 250); // window appears 150 pixels from the left // and 250 pixels from the top of the screen setDefaultCloseOperation(EXIT_ON_CLOSE); // indicates that the program should end when the close // button is clicked; default is to do nothing, so the window // would close but the program wouldn’t end } public static void main(String[] args) { JFrameEx1 frame = new JFrameEx1(); frame.setVisible(true); // can setVisible(false) to make window } // disappear, if desired

Adding components to a JFrame The previous program example displayed an empty window We can place GUI objects, such as buttons, text areas or sliders in a JFrame window by using the add method, as shown in the next example

public class JFrameEx2 extends JFrame { private JButton aButton; import javax.swing.*; import java.awt.*; public class JFrameEx2 extends JFrame { private JButton aButton; public JFrameEx2 () { aButton = new JButton(“Click Me”); setTitle("I've been framed!"); setSize(300, 200); setLocation(150, 250); setDefaultCloseOperation(EXIT_ON_CLOSE); add (aButton); } public static void main(String[] args) { JFrameEx2 frame = new JFrameEx2(); frame.setVisible(true); Code at left produces the window shown below: You can click the button, but it doesn’t do anything; there is no action listener attached to the button to handle the action event that is created

Buttons that listen In order for a button to be functional, it must have an action listener associated with it Each button object possesses an addActionListener method which can be used to make the association The addActionListener method takes an ActionListener object as its argument An ActionListener object is an instance of a class that implements the ActionListener interface

The ActionListener Interface An interface in Java is something like a class with methods specified but not defined A class that implements an interface provides the definition(s) for any specified method(s) The ActionListener interface has one method specified: public void actionPerformed(ActionEvent e)

JFrame with working button import javax.swing.*; import java.awt.*; import java.awt.event.*; public class JFrameEx3 extends JFrame implements ActionListener { private JButton aButton; public JFrameEx3 () { aButton = new JButton(“Click Me”); aButton.addActionListener(this); setTitle("I've been framed!"); setSize(300, 200); setLocation(150, 250); setDefaultCloseOperation(EXIT_ON_CLOSE); add(aButton); }

… continued from previous slide public void actionPerformed (ActionEvent e) { aButton.setText(“Thanks”); } public static void main(String[] args) { JFrameEx3 frame = new JFrameEx3(); frame.setVisible(true); Before click: After:

Notes on example The example illustrates some of the principles of event-driven programming discussed earlier Note that, although the actionPerformed method exists, and we can see that it performs its action, it is never explicitly called anywhere in the program Keep in mind that an event object is like an exception object: it only gets created under certain circumstances like a catch block that matches an exception, the actionPerformed method is invoked automatically when its matching event object appears

More notes on example In the example, the class defined (JFrameEx3) both inherited from parent class JFrame (using the extends mechanism) and implemented the ActionListener interface In the first couple of examples in the text, a separate class is defined to implement ActionListener In either case, the only requirement of a class that implements the interface is that the class provides a definition for the actionPerformed method

Even more notes on example The reason for having the example class implement the interface was so that the actionPerformed method would have access to the JButton object and its setText method Note that a third import statement had to be added to the program to make the ActionListener interface available: import java.awt.event.*;

Color and the content pane The Color class and a set of Color constants is defined in the java.awt package To set the background color of a JFrame, you must first access the entire inner portion of the window; this part of the window is called the content pane The content pane is a separate object; it is an instance of the Container class, which, just to keep things interesting, is an ancestor of the JFrame class

Color and the content pane The content pane of a particular JFrame object is returned by the getContentPane method For example, we could create a Container object to represent a JFrame’s content pane with the following code: public class Window extends JFrame { Container pane = this.getContentPane();

Color and the content pane The setBackground() method can be used to add color to a container The argument to setBackground is an object of type Color; the color constants are such objects The code below would make a pink window: public class Window extends JFrame { Container pane = this.getContentPane(); … pane.setBackground(Color.PINK);

The Color Constants Color.BLACK Color.BLUE Color.CYAN Color.DARK_GRAY Color.GRAY Color.GREEN Color.LIGHT_GRAY Color.MAGENTA Color.ORANGE Color.PINK Color.RED Color.WHITE Color.YELLOW

Color objects A color object can be constructed using three int parameters, each of which may have a value between 0 and 255 The first parameter represents the amount of red saturation in the color; the second, the amount of green, the third, the amount of blue For example, the Color.RED constant is an object constructed as follows: public static final Color RED = new Color(255,0,0); Other colors can be created by using different combinations of the three numbers

import javax. swing. ; import java. awt. ; import java. util import javax.swing.*; import java.awt.*; import java.util.*; public class JFrameEx4 extends JFrame { Container pane; Random rg; public JFrameEx4 () { rg = new Random(); pane = this.getContentPane(); pane.setBackground(new Color(rg.nextInt(256),rg.nextInt(256),rg.nextInt(256))); setTitle("Beauty or the beast?"); setSize(300, 200); setLocation(150, 250); setDefaultCloseOperation(EXIT_ON_CLOSE); } public static void main(String[] args) { JFrameEx4 frame = new JFrameEx4(); frame.setVisible(true); } } Output examples:

Containers & layout managers The add method places a component within a JFrame (or other container) To control the way components are arranged when more than one is added to a container, we employ a layout manager To add a layout manager to a container, use the setLayout method, which requires: a Container calling object (a JFrame is a kind of Container) a LayoutManager object argument

Layout Managers in the Java API Several layout manager classes are defined in the API; three of them are: BorderLayout FlowLayout GridLayout

BorderLayout A BorderLayout manager places components in five defined regions of the container to which it is applied As components are added to a container, the region is specified in the call to the add method

import javax.swing.*; import java.awt.*; public class WinEx extends JFrame { Container pane; public WinEx() { setSize(300,300); pane = this.getContentPane(); pane.setLayout(new BorderLayout()); pane.add (new JButton(“NORTH”), BorderLayout.NORTH); pane.add (new JButton(“SOUTH”), BorderLayout.SOUTH); pane.add (new JButton(“EAST”), BorderLayout.EAST); pane.add (new JButton(“WEST”), BorderLayout.WEST); setDefaultCloseOperation(EXIT_ON_CLOSE); } public static void main (String [] args) { WinEx window = new WinEx(); window.setVisible(); }

Output The fifth region is represented by the constant BorderLayout.CENTER; this is the largest region, as can be seen in the example It is not necessary to place components in every region; any component placed in the center will occupy any unused space in other regions – see next slide

import javax.swing.*; import java.awt.*; public class WinEx extends JFrame { Container pane; public static void main (String [] args) { WinEx window = new WinEx(); window.setVisible(); } public WinEx() { setSize(300,300); pane = this.getContentPane(); pane.setLayout(new BorderLayout()); pane.add (new JButton(“CENTER”), BorderLayout.CENTER); pane.add (new JButton(“SOUTH”), BorderLayout.SOUTH); setDefaultCloseOperation(EXIT_ON_CLOSE); }

FlowLayout With the FlowLayout manager, components are laid out left to right, in the order in which they are added When no horizontal space remains, a new row begins The look of the overall layout will vary depending on the size of the window and the size and number of components in it

Variations of FlowLayout with same window contents 300 x 300 window: 200 x 400 window: 400 x 200 window:

Code for FlowLayout Examples import javax.swing.*; import java.awt.*; public class WinEx2 extends JFrame { public WinEx2() { setSize(400,200); this.getContentPane().setBackground(Color.BLUE); setLayout(new FlowLayout()); add (new JButton("button 1")); // other buttons added here … add (new JButton("button")); setDefaultCloseOperation(EXIT_ON_CLOSE); } public static void main (String [] args) { WinEx2 window = new WinEx2(); window.setVisible(true); } }

GridLayout Using the GridLayout manager, components are laid out in an evenly-spaced grid The constructor takes two arguments, indicating the number of rows and columns in the grid Depending on the number and size of components, and the size of the window, there may be some variation: the number of columns will be ignored if it is too small to accommodate the components

GridLayout Examples 3 x 2 grid with 6 buttons in 300 x 200 window 6 x 1 grid in 100 x 400 window 1 x 0 grid in 500 x 100 window

Code for GridLayout examples import javax.swing.*; import java.awt.*; public class WinEx3 extends JFrame { public WinEx3() { setSize(500,100); setLayout(new GridLayout(1,0)); add (new JButton("button 1")); // … other buttons added … add (new JButton("button 6")); setDefaultCloseOperation(EXIT_ON_CLOSE); } public static void main (String [] args) { WinEx3 window = new WinEx3(); window.setVisible(true); } }

Layout managers & containers Before we started looking at layout managers, we saw some examples of GUI windows that had functional buttons All of the layout examples contained buttons, but they weren’t functional; they were just a convenient way to illustrate the layout managers Most GUI windows are laid out using a variety of containers within a larger window, which has a layout manager attached to it Some or all of the component containers may employ their own layout manager(s)

JPanel objects A JFrame makes a good overall container, as it has all of the attributes we have come to expect in a window However, a JFrame contained within a JFrame would be overkill; a better option would be an unadorned window, with just a content pane and no frame or title bar Such a window can be created with a JPanel object A single JFrame can contain several JPanels

Example The next example illustrates the use of this hierarchical layout scheme The main window is split into two panels (display and buttonArea), which are arranged in the main window using the BorderLayout manager The buttonArea has a FlowLayout manager attached to it, and it contains a single component, a JButton The code fragment on the next slide contains that portion of the constructor where the layout managers and components are added; the class and method heading, as well as instructions that set up the main window are omitted for brevity

Example pane = getContentPane(); // content pane of main window pane.setLayout(new BorderLayout()); // layout manager for main window display = new JPanel(); // upper portion of window: displays color display.setBackground(getNewColor()); // starts with random color pane.add(display, BorderLayout.CENTER); control = new JButton ("CHANGE IT!"); // button to change to new color control.addActionListener(this); buttonArea = new JPanel(); // lower portion of window: holds button buttonArea.setLayout(new FlowLayout()); buttonArea.setBackground(Color.BLACK); buttonArea.add(control); pane.add(buttonArea, BorderLayout.SOUTH);

Example continued public Color getNewColor() { // returns a new color constructed randomly Random rg = new Random(); int r, g, b; r = rg.nextInt(256); g = rg.nextInt(256); b = rg.nextInt(256); return new Color(r,g,b); } public void actionPerformed (ActionEvent e) { // changes color when // button is activated display.setBackground(getNewColor()); } public static void main (String [] args) { ColorPanel06 demo = new ColorPanel06(); demo.setVisible(true); }

Complete program produces output like the examples shown below

Swing Menus Menus are lists of options from which a user can choose Three Swing classes are involved in creating a GUI menu interface for a Java program: JMenuItem: an individual menu entry JMenu: a collection of JMenuItems JMenuBar: a container for JMenus

JMenuItem Like JButtons, JMenuItems generate ActionEvents We can program the actionPerformed method to respond to an ActionEvent from a specific menu (or a specific button, if we have more than one) by invoking the getActionCommand() method of the ActionEvent object By default, this method returns the String used to construct the JButton or JMenuItem object

Example The code fragment below creates a JMenuItem and adds an ActionListener to it: JMenuItem cyan = new JMenuItem("cyan"); cyan.addActionListener(this); The corresponding code fragment comes from the actionPerformed method: public void actionPerformed (ActionEvent e) { String command = e.getActionCommand(); if (command.equals(“cyan”)) display.setBackground(Color.CYAN));

JMenu and JMenuBar A JMenu can be thought of as a container for JMenuItems; we place items in the menu by using the set method, as in this example: JMenu other = new JMenu("Others"); other.add(cyan); Similarly, a JMenu can be added to a JMenuBar: JMenuBar menus = new JMenuBar(); menus.add(other);

The setActionCommand method As previously mentioned, the getActionCommand() method, when called from an ActionEvent object, returns a String by default, this will be the label from the button or menu item that created the event the setActionCommand method can be used to set a different return String for the button or menu item

Example import javax.swing.*; import java.awt.*; import java.awt.event.*; public class ActionEx extends JFrame implements ActionListener { JButton b1, b2, b3; Container pane; public ActionEx() { setSize(100,200); setLocation(150,150); pane = this.getContentPane(); pane.setLayout(new GridLayout(3,0)); setupButtons(); setDefaultCloseOperation(EXIT_ON_CLOSE); }

Example continued public void setupButtons() { b1 = new JButton("?"); b1.setActionCommand("1"); b1.addActionListener(this); pane.add(b1); b2 = new JButton("??"); b2.setActionCommand("2"); b2.addActionListener(this); pane.add(b2); b3 = new JButton("???"); b3.setActionCommand("3"); b3.addActionListener(this); pane.add(b3); }

Example concluded Effect of running this code in a program; before buttons are activated: public void actionPerformed(ActionEvent e) { String s = e.getActionCommand(); int x = Integer.parseInt(s); switch (x) { case 1: b1.setText("OK"); break; case 2: b2.setText("Oh no!"); break; case 3: b3.setText ("Ouch"); } // end switch } // end method } // end class After all have been pressed:

Labels, Text Fields and Text Areas Each of the elements listed above describes a way to add text to a GUI window A label is a static text field; it can’t be changed by the user A text field is a fixed-size blank for the user to fill in A text area is basically a larger text field, allowing for multiple lines of input

JLabel example A JLabel is constructed with an initial String: JLabel label = new JLabel(“”); The text content can be changed with the setText() method: label.setText(“New text”); The next slide shows excerpts from a previous example enhanced with an explanatory label – only the additional lines of code and method headings are shown

Example // import statements omitted for brevity public class ColorPanelWithText extends JFrame implements ActionListener { // some declarations not shown Container pane; JLabel label; public ColorPanelWithText () { // a few lines omitted for brevity pane = getContentPane(); label = new JLabel(""); pane.setLayout(new BorderLayout()); pane.add(label, BorderLayout.NORTH); } public Color getNewColor() { // random numbers obtained as before – code not shown label.setText("red: " + r + " green: " + g + " blue: " + b); return new Color(r,g,b); } // actionPerformed() and main() methods not shown - unchanged

Result of changes

Text fields A text field is a single line blank that a program’s user can fill in A JTextField object is constructed with a single int argument, representing the visible length of the field A text field is typically linked with a label, which serves the same purpose as a prompt

Example The next several slides contain excerpts from a program similar to the previous random color example The panel can still display random colors, but the ability to control the color values has been added via the use of text fields

Example - declarations Container pane; // content pane of main window JPanel display, // middle panel – displays color buttonArea, // bottom panel – holds random color button textArea; // top panel – holds labels & text fields JButton control, // random color button go; // control button for chosen values JTextField red, // editable display fields for color values green, blue;

Text area setup method public void setUpTextArea() { textArea = new JPanel(); textArea.setLayout(new FlowLayout()); go = new JButton("GO"); go.addActionListener(this); JLabel prompt1, prompt2, prompt3; prompt1 = new JLabel("red value"); prompt2 = new JLabel ("green value"); prompt3 = new JLabel ("blue value"); textArea.add(prompt1); textArea.add(red); textArea.add(prompt2); textArea.add(green); textArea.add(prompt3); textArea.add(blue); textArea.add(go); }

Revision of actionPerformed public void actionPerformed (ActionEvent e) { String command = e.getActionCommand(); if (command.equals("SURPRISE ME!")) display.setBackground(getNewColor()); else changeFromText(); }

New method - changeFromText public void changeFromText() { int r = Integer.parseInt(red.getText()); int g = Integer.parseInt(green.getText()); int b = Integer.parseInt(blue.getText()); display.setBackground(new Color(r,g,b)); }

Result examples

Text Areas A JTextArea object is similar to a JTextField, but is constructed with two int values instead of one first argument is number of text lines (rows) second argument is number of characters per line (columns) As with a JTextField, the arguments to the constructor provide guaranteed minimum values, which can be exceeded in practice

Adding a String argument Although labels can be used to prompt users to fill in the blanks for text fields and text areas, another option is to place the prompt within the box for the user to replace; for example: JTextArea commentBox = new JTextArea (“Please add your comments here”, 10, 20);