AWT Vs SWING. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many applications Uses.

Slides:



Advertisements
Similar presentations
1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
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
Graphic User Interfaces Layout Managers Event Handling.
Corresponds with Chapter 12
Event Driven Programming and GUIs Part 3 CS221 – 4/15/09.
1 Chapter 7 Graphics and Event Handling. 2 Overview The java.awt and javax.swing packages and their subpackages support graphics and event handling. Many.
1 Simple Phone Applet Lab Mobile Phone Display Area Send, Menu and End Numbers 0-9 * and #
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.
CS102--Object Oriented Programming Lecture 19: – The Swing Package (II) Copyright © 2008 Xiaoyan Li.
Java GUI building with the AWT. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many.
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.
CPSC150 JavaLynn Lambert CPSC150 Week 12 InheritanceInterfaces.
Java Programming Chapter 10 Graphical User Interfaces.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Applets and Frames CS 21a: Introduction to Computing I First Semester,
CSE 219 Computer Science III Graphical User Interface.
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 GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
Java GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 12.
Java GUI building with Swing. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
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.
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.
Timer class and inner classes. Processing timer events Timer is part of javax.swing helps manage activity over time Use it to set up a timer to generate.
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.
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
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.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
Csc Basic Graphical User Interface (GUI) Components.
Computer Science 209 GUIs Model/View/Controller Layouts.
Graphical User Interfaces Tonga Institute of Higher Education.
Graphical User Interfaces (GUI). PART ONE About GUI’s.
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
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.
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 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.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
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.
GUI building with the AWT
Swing JComponents.
Ellen Walker Hiram College
Chap 7. Building Java Graphical User Interfaces
Graphical User Interfaces -- Introduction
Timer class and inner classes
Creating Graphical User Interfaces
GUI building with the AWT
GUI building with the AWT
13 April 2010 GUIS: Graphical User Interfaces
Graphical User Interface
Presentation transcript:

AWT Vs SWING

2 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 it's “least common denominator” Difficult to build an attractive GUI import java.awt.*; import java.awt.event.*;

3 Swing Same concepts as AWT Doesn’t work in ancient Java implementations (Java 1.1 and earlier) Many more controls, and they are more flexible Some controls, but not all, are a lot more complicated Gives a choice of “look and feel” packages Much easier to build an attractive GUI import javax.swing.*;

4 Swing vs. AWT Swing is bigger, slower, and more complicated But much faster than it used to be Swing is more flexible and better looking Swing and AWT are incompatible--you can use either, but you can’t mix them Actually, you can, but it’s tricky and not worth doing Learning the AWT is a good start on learning Swing Many of the most common controls are just renamed AWT: Button b = new Button ("OK"); Swing: JButton b = new JButton("OK");

5 To build a GUI... Make somewhere to display things—usually a Frame or Dialog (for an application), or an Applet Create some Components, such as buttons, text areas, panels, etc. Add your Components to your display area Arrange, or lay out, your Components Attach Listeners to your Components Interacting with a Component causes an Event to occur A Listener gets a message when an interesting event occurs, and executes some code to deal with it

6 Container s and Component s The job of a Container is to hold and display Component s Some common subclasses of Component are Button, Checkbox, Label, Scrollbar, TextField, and TextArea A Container is also a Component This allows Containers to be nested Some Container subclasses are Panel (and Applet ), Window, and Frame

7 Example: A "Life" applet Container ( Applet ) Containers ( Panel s) Component ( Canvas ) Components ( Button s) Components ( Label s) Components ( TextField s)

8 Some types of components Label Button Checkbox Choice List Scrollbar TextField TextArea CheckboxGroup Checkbox

9 Creating components Label lab = new Label ("Hi, Dave!"); Button but = new Button ("Click me!"); Checkbox toggle = new Checkbox ("toggle"); TextField txt = new TextField ("Initial text.", 20); Scrollbar scrolly = new Scrollbar (Scrollbar.HORIZONTAL, initialValue, bubbleSize, minValue, maxValue);

10 Creating a Frame When you create an Applet, you get a Panel “for free” When you write a GUI for an application, you need to create and use a Frame : Frame frame = new Frame(); frame.setTitle("My Frame"); frame.setSize(300, 200); // width, height... add components... frame.setVisible(true); Or: class MyClass extends Frame {... setTitle("My Frame"); // in some instance method

11 Complete example: FlowLayout import java.awt.*; import java.applet.*; public class FlowLayoutExample extends Applet { public void init () { setLayout (new FlowLayout ()); // default add (new Button ("One")); add (new Button ("Two")); add (new Button ("Three")); add (new Button ("Four")); add (new Button ("Five")); add (new Button ("Six")); } }

12 BorderLayout At most five components can be added If you want more components, add a Panel, then add components to it. setLayout (new BorderLayout()); add (new Button("NORTH"), BorderLayout.NORTH);

13 BorderLayout with five Button s public void init() { setLayout (new BorderLayout ()); add (new Button ("NORTH"), BorderLayout.NORTH); add (new Button ("SOUTH"), BorderLayout.SOUTH); add (new Button ("EAST"), BorderLayout.EAST); add (new Button ("WEST"), BorderLayout.WEST); add (new Button ("CENTER"), BorderLayout.CENTER); }

14 Complete example: BorderLayout import java.awt.*; import java.applet.*; public class BorderLayoutExample extends Applet { public void init () { setLayout (new BorderLayout()); add(new Button("One"), BorderLayout.NORTH); add(new Button("Two"), BorderLayout.WEST); add(new Button("Three"), BorderLayout.CENTER); add(new Button("Four"), BorderLayout.EAST); add(new Button("Five"), BorderLayout.SOUTH); add(new Button("Six"), BorderLayout.SOUTH); }

15 Using a Panel Panel p = new Panel(); add (p, BorderLayout.SOUTH); p.add (new Button ("Button 1")); p.add (new Button ("Button 2"));

16 GridLayout The GridLayout manager divides the container up into a given number of rows and columns: new GridLayout( rows, columns ) All sections of the grid are equally sized and as large as possible

17 Complete example: GridLayout import java.awt.*; import java.applet.*; public class GridLayoutExample extends Applet { public void init () { setLayout(new GridLayout(2, 3)); add(new Button("One")); add(new Button("Two")); add(new Button("Three")); add(new Button("Four")); add(new Button("Five")); } }

18 Making components active Most components already appear to do something-- buttons click, text appears To associate an action with a component, attach a listener to it Components send events, listeners listen for events Different components may send different events, and require different listeners

19 Listeners Listeners are interfaces, not classes class MyButtonListener implements ActionListener { An interface is a group of methods that must be supplied When you say implements, you are promising to supply those methods

20 Writing a Listener For a Button, you need an ActionListener b1.addActionListener (new MyButtonListener ( )); An ActionListener must have an actionPerformed(ActionEvent) method public void actionPerformed(ActionEvent e) { … }

21 MyButtonListener public void init () {... b1.addActionListener (new MyButtonListener ()); } class MyButtonListener implements ActionListener { public void actionPerformed (ActionEvent e) { showStatus ("Ouch!"); }

22 Listeners for TextField s An ActionListener listens for someone hitting the Enter key An ActionListener requires this method: public void actionPerformed (ActionEvent e) You can use getText( ) to get the text A TextListener listens for any and all keys A TextListener requires this method: public void textValueChanged(TextEvent e)

23 When not to use listeners Some GUI elements are active—the user expects them to do something Buttons and some menu items are active When an active element is used, the program should do something, and should look like it’s done something! Most GUI elements are passive Text fields, text areas, checkboxes, radio buttons, pulldown lists—these provide data or flags, but don’t (shouldn’t) cause the program to actually do anything The appearance of the passive element changes, but that’s all In general, it’s a bad idea to put listeners on passive elements Active GUI elements should get the state of passive elements as needed For example, clicking a button might cause the program to do getText()

24 AWT and Swing AWT Button s vs. Swing JButton s: A Button is a Component A JButton is an AbstractButton, which is a JComponent, which is a Container, which is a Component Containers: Swing uses AWT Container s and AWT Component s AWT Frame s vs. Swing JFrame s: A Frame is a Window is a Con t ainer is a Component A JFrame is a Frame, etc. Layout managers: Swing uses the AWT layout managers, plus a couple of its own Listeners: Swing uses many of the AWT listeners, plus a couple of its own Bottom line: Not only is there a lot of similarity between AWT and Swing, but Swing actually uses much of the AWT

25 Summary I: Building a GUI Create a container, such as Frame or Applet Choose a layout manager Create more complex layouts by adding Panel s; each Panel can have its own layout manager Create other components and add them to whichever Panel s you like

26 Summary II: Building a GUI For each active component, look up what kind of Listener s it can have Create (implement) the Listener s often there is one Listener for each active component Active components can share the same Listener For each Listener you implement, supply the methods that it requires.

27 Vocabulary AWT – The Abstract Window Toolkit provides basic graphics tools (tools for putting information on the screen) Swing – A much better set of graphics tools Container – a graphic element that can hold other graphic elements (and is itself a Component ) Component – a graphic element (such as a Button or a TextArea) provided by a graphics toolkit listener – A piece of code that is activated when a particular kind of event occurs layout manager – An object whose job it is to arrange Components in a Container

28 The End