Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.

Slides:



Advertisements
Similar presentations
G5BUID - Java Swing Laying out components Manage realized components Determine size and position Each container has a layout manager (usually)
Advertisements

Unit 121 A layout manager is an object that determines the manner in which components are arranged in a container. Each layout manager implements one of.
Graphical User Interfaces (Part IV)
Java Software Development Paradigm Lecture # 12. Basics of GUI.
CS18000: Problem Solving and Object-Oriented Programming.
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.
Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
1 GUI Programming (Part III). Graphical User Interfaces (Part III) Overview  GUI Programming Issues.  GUI Programming in Java: Then and Now.  Overview.
Graphical User Interfaces (GUIs) GUI: An application that uses graphical objects to interact with users GUI applications consist of: –Events: A user or.
1 Lecture 11Lecture 14 Graphical User Interfaces (GUI) with AWT and Swing Overview  GUI Programming Issues.  GUI Programming in Java: Then and Now. 
Graphical User Interfaces Allow for interaction with –Buttons –Menus –Text Fields Two Java Libraries to assist in GUI Programming –AWT –Swing.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
Layout Mangers CSC 171 FALL 2001 LECTURE 14. History: The Transistor William Shockley, John Bardeen, and Walter Brattain invent the transfer resistance.
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.
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.
OOP (Java): Layout/ OOP Objectives – –describe the basic layout managers for GUIs Semester 2, GUI Layout.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Java the UML Way versjon Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
MSc Workshop - © S. Kamin, U.Reddy Lect 5 – GUI Prog - 1 Lecture 5 – GUI Programming r Inner classes  this and super r Layout r Reading: m.
CIS 068 Welcome to CIS 083 ! Introduction to GUIs: JAVA Swing.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Java GUI building with Swing. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
SWING. AbstractButton Swing Buttons are subclasses of the AbstractButton class, which extends JComponent. Abstract class javax.swing.AbstractButton AbstractButton.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
Copyright © 2002, Systems and Computer Engineering, Carleton University c-Gui3.ppt * Object-Oriented Software Development Part 18-c Building.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Layout Managers Arranges and lays out the GUI components on a container.
Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila.
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.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
University of Limerick1 Software Architecture Java Layout Managers.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
Ajmer Singh PGT(IP) JAVA IDE Programming - I. Ajmer Singh PGT(IP) GUI (Graphical User Interface) It is an interface that uses a graphic entities along.
Chapter 14- More Swing, Better looking applications.
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7-3 ( Book Chapter 14) GUI and Event-Driven Programming.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Event Handler Methods Text field Object Responder JAVA AWT Environment: Messages are sent between JAVA Objects Screen Event Notification Press Button.
Applets. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 2 Applets Usually.
Introduction to GUI in 1 Graphical User Interface 3 Nouf Almunyif.
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.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
GUIs & Event-Driven Programming Chapter 11 Review.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
Chapter 7 A First Look at GUI Applications Layout Managers.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
Christopher Budo, Davis Nygren, spencer franks, Luke miller
Swing JComponents.
Modern Programming Language Java
Graphical User Interface (pronounced "gooey")
Creating Graphical User Interfaces
IFS410: Advanced Analysis and Design
Graphical User Interface
Chapter 7-3 (Book Chapter 14)
Graphical User Interface
Presentation transcript:

Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. GUI Components and Events

16-2 Objectives: Get a more systematic introduction to basic Swing components, their methods, and events they generate. Components discussed:  JLabel  JButton  JToggleButton  JCheckBox  JComboBox  JSlider  JTextField  JPasswordField  JTextArea

16-3 Pluggable Look and Feel Look and feel refers to the GUI aspect of a program. Java’s Swing supports PLAF (Pluggable Look and Feel). Java provides several LAFs, including:  “Metal”  platform independent  “Windows”  for Windows  “Windows Classic”  like Windows 95  “Motif”  for Unix / Linux

16-4 PLAF (cont’d) Three ways to set look and feel:  swing.properties file:  Command-line switch:  In the program: swing.defaultlaf=com.sun.java.swing.plaf.windows.WindowsLookAndFeel C:\> java -Dswing.defaultlaf=javax.swing.plaf.metal. MetalLookAndFeel... try { UIManager.setLookAndFeel(plafName); } catch (Exception ex) {... }

16-5 GUI Components Components are created using constructors: To be usable, a component must be added to the application’s “content pane” or to another component: JLabel guest = new JLabel ("Guest”); JPanel scorePanel = new JPanel(); scorePanel.add (guest);

16-6 GUI Events Components (except JLabel) can generate events. Events are captured and processed by “listeners” — objects equipped to handle a particular type of events. Different types of events are processed by different types of listeners.

16-7 Listeners Different types of “listeners” are described as interfaces:  ActionListener  ChangeListener  ItemListener  etc. The same object can serve as different types of listeners (as long as its class implements all the corresponding interfaces).

16-8 Listeners (cont’d) public class GoHandler implements ActionListener {... public void actionPerformed (ActionEvent e) {... }... JButton go = new JButton (“Go”); go.addActionListener (new GoHandler ()); Objects of this class are GoHandlers but also ActionListeners This method expects an ActionListener; a GoHandler object qualifies. This method is called automatically when the button is clicked

16-9 Listeners (cont’d) When implementing an event listener, programmers often use a private inner class that has access to all the fields of the surrounding public class. An inner class can have constructors and private fields, like any other class. A private inner class is accessible only in its outer class.

16-10 Listeners (cont’d) public class MyPanel extends JPanel { private JButton go;... go = new JButton (“Go”); go.addActionListener (new GoHandler ());... private class GoHandler implements ActionListener { public void actionPerformed (ActionEvent e) { go.setText(“Stop”);... } go is accessible in constructors and methods of the inner class

16-11 Listeners (cont’d) You don’t have to capture all events. If you don’t want to deal with events from a component, just don’t attach a listener to it. If you do want to capture events but forget to add a listener, no events will be captured (a common omission).

16-12 Listeners (cont’d) actionPerformed (or another listener method takes a corresponding type of event as a parameter. Event objects have useful methods. For example, getSource returns the object that produced this event. A MouseEvent has methods getX, getY.

16-13 GUI Components Review Java Methods A&AB Appendix D contains brief summaries of several Swing components, their constructors, methods, and events. For a complete specification refer to the Java Swing API docs.

16-14 JLabel Constructors: JLabel (String text); JLabel (ImageIcon icon); JLabel (String text, ImageIcon icon, SwingConstants.LEFT); // or CENTER, RIGHT, LEADING, TRAILING. Methods: void setText (String text); void setIcon (ImageIcon icon); Events: None

16-15 JButton Constructors: JButton (String text); JButton (ImageIcon picture); JButton (String text, ImageIcon picture); Methods: void addActionListener (ActionListener object) void setText (String text); void setActionCommand (String cmd); void setIcon (ImageIcon icon); void requestFocus(); Events: class... implements ActionListener { public void actionPerformed(ActionEvent e) { JButton b = (JButton)e.getSource(); String s = e.getActionCommand(); }

16-16 JCheckBox Constructors: JCheckBox (String text, boolean checked); JCheckBox (ImageIcon icon, boolean checked); JCheckBox (String text, ImageIcon icon, boolean checked); Methods: void addActionListener (ActionListener object) boolean isSelected () void setSelected (boolean checked) void setText (String text); void setIcon (ImageIcon icon); Events: class... implements ActionListener { public void actionPerformed(ActionEvent e) { JCheckBox b = (JCheckBox)e.getSource(); if (b == checkBox1 && b.isSelected())... }

16-17 etc. See Java Methods A&AB Appendix D.

16-18 Layouts A layout manager is a strategy for placing components on the content pane or another component (usually a panel). In Java, the content pane and any GUI component is a Container. A layout is chosen by calling the container’s setLayout method.

16-19 Layouts (cont’d) Layouts are used to achieve some degree of platform independence and scalability. awt/Swing support several layout managers. Here we consider four:  FlowLayout  GridLayout  BorderLayout  BoxLayout These classes implement the java.awt.LayoutManager interface.

16-20 FlowLayout Places components in a line as long as they fit, then starts the next line. Uses “best judgement” in spacing components. Centers by default. Lets each component assume its natural (preferred) size. Often used for placing buttons on panels.

16-21 FlowLayout (cont’d) Container c = getContentPane(); c.setLayout(new FlowLayout()); c.add (new JButton ("Back to Start")); c.add (new JButton ("Previous Slide")); c.add (new JButton ("Next Slide")); c.add (new JButton ("Last Slide")); c.add (new JButton ("Exit"));

16-22 GridLayout Splits the panel into a grid with given numbers of rows and columns. Places components into the grid cells. Forces the size of each component to occupy the whole cell. Allows additional spacing between cells.

16-23 GridLayout (cont’d) Container c = getContentPane(); c.setLayout (new GridLayout(3, 2, 10, 20 )); c.add (new JButton ("Back to Start")); c.add (new JButton ("Previous Slide")); c.add (new JButton ("Next Slide")); c.add (new JButton ("Last Slide")); c.add (new JButton ("Exit")); Extra space between the cells (in pixels)

16-24 BorderLayout Divides the area into five regions and adds a component to the specified region. Forces the size of each component to occupy the whole region. NORTH SOUTH CENTEREASTWEST

16-25 BorderLayout (cont’d) Container c = getContentPane(); c.setLayout(new BorderLayout()); // optional: default c.add (new JButton ("Next Slide"), BorderLayout.EAST); c.add (new JButton ("Previous Slide"), BorderLayout.WEST); c.add (new JButton ("Back to Start"), BorderLayout.NORTH); c.add (new JButton ("Last Slide"), BorderLayout.SOUTH); c.add (new JButton ("Exit"), BorderLayout.CENTER);

16-26 BoxLayout In a horizontal box, components are placed horizontally, left to right. In a vertical box, components are placed vertically, top to bottom. “Horizontal” or “vertical” has nothing to do with the shape of the box itself.

16-27 BoxLayout (cont’d) BoxLayout is the default layout for a Box container. The idiom for working with boxes is slightly different: Box box1 = Box.createHorizontalBox(); box1. add (...); // add a spacer, 60 pixels: box1.add(Box.createHorizontalStrut (60)); Box box2 = Box.createVerticalBox();...

16-28 BoxLayout (cont’d) Container c = getContentPane(); c.setLayout(new FlowLayout()); Box box = Box.createVerticalBox(); box.add (new JButton ("Next Slide")); box.add (new JButton ("Previous Slide")); box.add (Box.createVerticalStrut (20) ); box.add (new JButton ("Exit")); c.add (box); Adds extra vertical space between components

16-29 Default Layouts Each component has a default layout manager, which remains in effect until the component’s setLayout method is called. The defaults are: Content pane BorderLayout JPanel FlowLayout Box BoxLayout

16-30 Menus You can add a JMenuBar object to JFrame or JApplet. You can add JMenu objects to a JMenuBar. You can add other JMenus, JMenuItems, JCheckBoxMenuItems, JRadioButtonMenuItems, etc. to a JMenu. See Section 16.5 for an example.

16-31 Review: What are the three ways to set a PLAF? Can a container contain another container? Name several Swing GUI components. Is an action listener a class, an interface, an object, or a method? How do FlowLayout and GridLayout deal with the sizes of components?

16-32 Review (cont’d): What is the default layout manager for the content pane? What type of objects can you add to a JMenu?