1 GUI Programming (Part III). Graphical User Interfaces (Part III) Overview  GUI Programming Issues.  GUI Programming in Java: Then and Now.  Overview.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Graphical User Interfaces
Graphical User Interfaces (Part IV)
Java Software Development Paradigm Lecture # 12. Basics of GUI.
CS18000: Problem Solving and Object-Oriented Programming.
Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
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.
Corresponds with Chapter 12
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.
Java GUI Libraries Swing Programming. Swing Components Swing is a collection of libraries that contains primitive widgets or controls used for designing.
Understanding SWING Architecture CS 4170 UI Design Hrvoje Benko Oct. 9, 2001.
Graphical User Interfaces (GUIs) GUI: An application that uses graphical objects to interact with users GUI applications consist of: –Events: A user or.
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 Lecture 11Lecture 14 Graphical User Interfaces (GUI) with AWT and Swing Overview  GUI Programming Issues.  GUI Programming in Java: Then and Now. 
GUI and Event-Driven Programming Recitation – 3/6/2009 CS 180 Department of Computer Science, Purdue University.
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.
Unit 111 Java GUI Components and Events  Learning Outcomes oDistinguish between GUI components and containers. oIdentify and distinguish top-level containers.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
Contructing GUI’s in Java Implemented in the Swing API Imported into your programs by: import javax.swing.*; Most Swing programs also need the AWT packages.
A.k.a. GUI’s.  If you want to discuss your Lab 2 grade come see me this week. ◦ Office: 436 ERB. One hour prior to class ◦ Open to Appointments MWF 
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.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog.
Java GUIs and Graphics CNS Outline  Introduction  Events  Components  Layout managers  Drawing  Introduction  Events  Components  Layout.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
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.
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.
Copyright © 2002, Systems and Computer Engineering, Carleton University c-Gui3.ppt * Object-Oriented Software Development Part 18-c Building.
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.
CompSci 100E 35.1 Graphical User Interfaces: GUIs  Components  Flat Layouts  Hierarchical Layouts  Designing a GUI  Coding a GUI.
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.
GUI Basics. What is GUI? A graphical user interface (GUI) is a type of user interface item that allows people to interact with programs in more ways than.
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.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
GUI Basics. Agenda What GUI How to make in java Creating frames Frequently used GUI components Layout Managers.
Basics of GUI Programming Chapter 11 and Chapter 22.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
Event Handler Methods Text field Object Responder JAVA AWT Environment: Messages are sent between JAVA Objects Screen Event Notification Press Button.
1 Lecture 8: User Interface Components with Swing.
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.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
Unit 131 GUI Layout Managers Learning Outcomes oList and distinguish between the four most common, standard layout managers in Java. oUse these and other.
A Quick Java Swing Tutorial
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
Graphical User Interfaces
Graphical User Interfaces -- GUIs
A First Look at GUI Applications
Ellen Walker Hiram College
Course Outcomes of Advanced Java Programming AJP (17625, C603)
Graphical User Interface
Presentation transcript:

1 GUI Programming (Part III). Graphical User Interfaces (Part III) Overview  GUI Programming Issues.  GUI Programming in Java: Then and Now.  Overview of AWT and Swing Graphics Elements.  Layout Managers.  Some Examples.  Coming Next: GUI Programming (Part IV).

2 GUI Programming (Part III). AWT GUI Programming Issues  GUI programming consists of the following aspects:  Graphics: line drawing, filling, icons, etc  Windows (Containers, Components, Layout): gadgets for interaction  Events: handling interaction, timings, etc  Media: photos, video, sound, etc  Concurrency: doing several things at once  We have touched on all of these aspects except concurrency in the preceding lectures.  In the this and the next lectures, we will be considering more AWT graphics components and containers (as well as introducing Swing components) that can be used to develop more interesting GUIs.  All the components used in our graphics applications so far are from the AWT library.  Although the AWT is multiplatform (i.e, programs using AWT components run on different computer platforms without change), it was not written in Java and, as a result, it could not benefit from Java’s object-oriented virtues.  AWT components rely on native “peer” user interface elements (buttons, text fields, menus etc) of the underlying OS to render and operate them.

3 GUI Programming (Part III). GUI Programming in Java: Then and Now  This reliance of the AWT components on the native platform gives AWT the following historical limitations:  slow on some platforms (e.g., windows)  portability problems (slightly different look, some behaviors different)  least common denominator phenomenon; rigid look  After many programmer complaints that Java with AWT does not qualify for “write once run everywhere” but “write once debug everywhere”, SUN Microsystems responded with the Java Foundation Classes (JFC) a.k.a Swing.  Unlike AWT, Swing components do not rely on native peers, instead the Java Virtual Machine renders and controls all components.  Swing completely controls the look and feel of all components and enables the creation of fast, flexible and extensible graphics components.  Four important points to remember about Swing:  1) The API is your best friend  2) Put a J in front of almost everything  3) Only “add” to a top-level container (JFrame, JApplet) by getting the container’s content-pane: getContentPane(); DON’T  4) DON’T mix Swing and AWT components.

4 GUI Programming (Part III). AWT and Swing Graphics Elements  Graphics Components and Containers can be grouped into the following main headings  1.) Buttons (see classes Button, JButton): This class creates labeled buttons which generate ActionEvent (as seen in Lecture 13)  2.) Text components (see JTextComponent, JTextArea, JTextField): Allow multi-line (or single- line in case of JTextField) text display are which may be set to be read-only or to allow editing.  3.) Choices (Radio buttons, check boxes, combo boxes): Creates selectable/checkable buttons and boxes, selectable drop-down lists and look and feel renditions.  4.) Menus (see JMenuBar, JMenu, JMenuItem):Provide encapsulations for a platforms menus.  5.) Labels (see JLabel): Components for placing single-line, read-only text in a container.  6.) Panels: T he simplest container class (with FlowLayout default manganer) that provides space in which an application can attach any other component, including other panels.  7.) etc  See Example 2 for a simple demonstration of some of these components.

5 GUI Programming (Part III). Complete GUIs Containers with Separate Layout Managers A Combo Box, Radio Buttons, and Check Boxes Pulldown Menus

6 GUI Programming (Part III). Layout Managers  A layout manager is an object that determines the manner in which components are automatically arranged in a container.  Here are some predefined layout managers Java (the last two are defined in Swing)  FlowLayout: puts as many components in a row as possible, then moves to the next row. Components are displayed in the order they are added.  BorderLayout : defines 5 areas (North, East, South, West and Center) into which components can be added. Each of these areas could contain a container.  CardLayout: Treats each component in the container as a card, only one is visible at a time, and the container acts as a stack of cards.  GridLayout : Partitions the Frame into a fixed number of rows and columns.  GridBagLayout: Aligns components vertically and horizontally, without requiring that the components be of the same size.  BoxLayout: organises components either horizontally (in a row) or vertically (in a column).  OverlayLayout: Arranges components over the top of each other.  Every container has a default layout manager which can be changed using setLayout().

7 GUI Programming (Part III). The GridLayout Manager keypadPanel.setLayout( new GridLayout(4,3,1,1)); A GridLayout arranges components in a two-dimensional grid. 4 rows and 3 columns 1 space between each row and column Design Critique: We should use BorderLayout for top-level window.

8 GUI Programming (Part III). GUI Design Critique Problem: The default layout for a JPanel is FlowLayout but we need GridLayout. We got the keypad layout wrong!

9 GUI Programming (Part III). The BorderLayout Manager Use add(Component, String) method to add components to a border layout : A BorderLayout divides the container into five areas: north, south, east, west, and center. getContentPane().setLayout(new BorderLayout(2, 2)); getContentPane().add(keypadPanel, "East"); Horizontal and vertical gaps.

10 GUI Programming (Part III). The BorderLayout Manager Panels are used to group components by function. All the controls are grouped together.

11 GUI Programming (Part III). Anatomy of a Swing JFrame The surface of a Swing frame is covered with four panes. Three of them, namely the root pane, the layered pane, and the glass pane are of no interest to most Java programmers. You need to know about the content pane, which holds the components that you want to display in the window To add a component to the content pane of a frame, you must first get a reference to the content pane object by calling the getContentPane method. This method returns a reference of type Container.

12 GUI Programming (Part III). Example 1: Using Simple Swing Components import javax.swing.*; import java.awt.*; import java.awt.event.*; public class CountButtonPushes extends JFrame implements ActionListener { private JButton button = new JButton("Press me"); private JLabel total = new JLabel( "Running total:"); private JTextField tally = new JTextField(10); private int sum = 0; public CountButtonPushes() { super("A Container With Components"); setSize(500,500); Container cp = getContentPane(); cp.setLayout(new FlowLayout()); cp.add(total); cp.add(tally); cp.add (button); button.addActionListener(this); show(); } public void actionPerformed( ActionEvent e ) { sum = sum + 1; // add number to sum tally.setText(Integer.toString(sum)); } public static void main(String args [] ) { new CountButtonPushes(); }

13 GUI Programming (Part III). Example 1 (Cont’d)  Example 1 in the preceding slide is the same as in Lecture 10 except that in this case we need to add the components to the content pane of the JFrame window.  Run this program side-by-side the equivalent one in Lecture 10 that extends the AWT’s Frame and observe the behavior of the two programs.  Example 2 on the next slide is meant to provide a way of experimenting with the different layout managers.  In this example, different Swing components are created and added to the main JFrame Container as well as a JPanel subcontainer.  As you might guess, many of these Swing components have more than one constructor. You should consult the Java 2 SDK documentation for more details on these.  In order to facilitate your understanding of layout managers, you should run this program with the different standard layout managers to see their effect.

14 GUI Programming (Part III). Example 2: Understanding Layout Managers import javax.swing.*;import java.awt.*; public class UsingLayoutManagers extends JFrame { private String courses[] = { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven"}; private JCheckBox jcb = new JCheckBox("A check box"); private JRadioButton jrb = new JRadioButton("Radio Button"); private JPanel jp = new JPanel(); private Choice jc = new Choice(); private JComboBox jcbb = new JComboBox(); public UsingLayoutManagers() { super("Experimenting with Layout Managers."); setSize(500,500); Container cp = getContentPane(); cp.setLayout(new FlowLayout()); //cp.setLayout(new FlowLayout(FlowLayout.LEFT)); //cp.setLayout(new GridLayout(3,5)); //cp.setLayout(new GridBagLayout()); //cp.setLayout(new BorderLayout()); for (int i=0; i<courses.length; i++){ cp.add(new JButton(courses[i])); } cp.add(jcb); cp.add(jrb); jc.add("UNIX"); jc.add("Windows"); jc.add("Macintosh"); jp.add(jc); cp.add(jp); cp.add(jcbb); show();} public static void main(String args [] ) { new UsingLayoutManagers();}}

15 GUI Programming (Part III). Example 3: Multiple Listeners to ActionEvent import javax.swing.*;import java.awt.*; import java.awt.event.*; public class UsingLayoutManagers1 extends JFrame implements ActionListener { private JPanel jp = new JPanel(); private JTextField jtf = new JTextField(21); private JButton buttons[] = new JButton[5]; private String courses[] = {"ICS102", "ICS201", "ICS202", "ICS232", "ICS313"}; public UsingLayoutManagers1() { super("Experimenting with Layout Managers!"); setSize(500,500); Container cp = getContentPane(); cp.setLayout(new FlowLayout()); for (int i=0; i<courses.length; i++){ buttons[i] = new JButton(courses[i]); buttons[i].addActionListener(this); cp.add(buttons[i]); } jp.add(jtf); cp.add(jp); show(); }

16 GUI Programming (Part III). Example 3: Multiple Listeners to ActionEvent (Cont’d) public void actionPerformed( ActionEvent ae) { for (int i = 0; i<courses.length; i++){ if (ae.getSource() == buttons[i]) jtf.setText("You Pressed "+ buttons[i].getText()); } public static void main(String args [] ) { new UsingLayoutManagers1(); } Example2

17 GUI Programming (Part III). Example 3: Multiple Listeners to ActionEvent (Cont’d)  Example 3 creates and adds a number of buttons to a JFrame and registers all these buttons to listen to an action event.  The getSource() method returns the object on which the Event initially occurred  The getText() method is used to return a button's text.  You may make similar modifications to this program as you did to the program in Example 2 above to add to your experience with layout managers.  We will consider more interesting examples involving with multiple Swing controls (components and containers)