Java Programming 1 Java Programming II Events, AWT, and Swing.

Slides:



Advertisements
Similar presentations
1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Advertisements

Introduction to Java 2 Programming
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
CS18000: Problem Solving and Object-Oriented Programming.
Graphic User Interfaces Layout Managers Event Handling.
Gui Interfaces a la Swing Up to speed with Swing by Steven Gutz is a good source It don’t mean a thing if it ain’t got that swing Duke Ellington.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
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.
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 Programming Chapter 10 Graphical User Interfaces.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal
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.
עקרונות תכנות מונחה עצמים תרגול 4 - GUI. Outline  Introduction to GUI  Swing  Basic components  Event handling.
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.
Objectives of This Session
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.
GUI Clients 1 Enterprise Applications CE00465-M Clients with Graphical User Interfaces.
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.
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.
Layout Managers Arranges and lays out the GUI components on a container.
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.
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.
CS 4244: Internet Programming User Interface Programming in Java 1.0.
Ch13 Creating windows and applets. Short overview AWT (Abstract Windowing Toolkit) Early Java development used graphic classesEarly Java development used.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
Graphical User Interfaces Tonga Institute of Higher Education.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
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.
Graphical User Interface (GUI)
1 Lecture 8: User Interface Components with Swing.
CSC 205 Programming II Lecture 7 AWT – Event Handling & Layout.
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:
AWT Vs SWING. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many applications Uses.
GUI Programming in Java Hao Jiang Boston College April, 2009.
Chapter 6 Building Java GUIs. MVC Model View Controller The model passes its data to the view for rendering The view determines which events are passed.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
A Quick Java Swing Tutorial
CSC 205 Programming II Lecture 5 AWT - I.
A First Look at GUI Applications
A Quick Java Swing Tutorial
Ellen Walker Hiram College
Swing Advanced HCI (IAT 351)
Chap 7. Building Java Graphical User Interfaces
Chapter 13: Advanced GUIs and Graphics
Graphical User Interfaces -- Introduction
A Quick Java Swing Tutorial
Advanced Programming in Java
Programming Graphical User Interface (GUI)
GUI building with the AWT
Advanced GUIs and Graphics
Graphical User Interface
Presentation transcript:

Java Programming 1 Java Programming II Events, AWT, and Swing

Java Programming 2 Contents  Events and Delegation Model  Overview of the AWT  Canvas  Button, TextField, List  Menu, MenuBar and MenuItem  Layout Managers  Panel  Swing  Creating New Window Frame  Dialogs and File Chooser

Java Programming 3 Using the ActionListener  Stages for Event Handling by ActionListener First, import event class import java.awt.event.*; Define an overriding class of event type (implements ActionListener) Create an event listener object ButtonListener bt = new ButtonListener(); Register the event listener object b1 = new Button(“Show”); b1.addActionListener(bt); Class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { // Write what to be done... label.setText(“Hello World!”); } addActionListener ButtonListener action Button Click Event ① ②

Java Programming 4 A Hello Example Using Button Listener import java.awt.*; import java.awt.event.*; public class HelloAWT extends Frame { // Using Frame Label contents; Button dispbutton; public HelloAWT() { // Constructor setLayout(new FlowLayout(FlowLayout.CENTER, 50, 50)); contents = new Label(" "); // Create Label object add(contents); // Add the label to this Frame dispbutton = new Button("Show"); // Create Button object dispbutton.addActionListener(new DispButtonListener()); // Add Event Listener add(dispbutton); // Add the button object to this Frame } class DispButtonListener implements ActionListener { // Event Listener public void actionPerformed(ActionEvent e) { // What to do when the button is clicked contents.setText("Hello World!"); } public static void main (String[] args) { HelloAWT f = new HelloAWT(); // Create Hello GUI f.setTitle("Hello!"); f.setSize(400,150); f.setVisible(true); } } // end of “HelloAWT.java” Run: Java HelloAWT // Can be replaced by anonymous class dispbutton.addActionListener(new ActionListener () { public void actionPerformed(ActionEvent e) { contents.setText("Hello Annoymus"); } } );

Java Programming 5 Abstract Window Toolkit(AWT)  The Abstract Window Toolkit (AWT) and Swing provide standard components to build a graphical user interface (GUI)  The GUI enables interaction between the user and the program by using the mouse, keyboard, or another input device  The AWT provides a mechanism to paint different shapes on the screen (e.g., lines, rectangles, text, etc.), and create different elements on a screen (buttons, lists, and others)

Java Programming 6 Example: GUI

Java Programming 7 AWT Class Hierarchy Component Object Button Panel Canvas Window Applet TextComponent ContainerLabelListScrollbar DialogFrame FileDialog TextAreaTextField MenuBar MenuComponent MenuItem Menu

Java Programming 8 Example: Frame and Canvas public class FRM extends Frame { public FRM() { super("Example: Frame and Canvas"); add(new CVS()); // add a canvas to paint setSize(400,200); } public static void main(String[] args) { new FRM().setVisible(true); } class CVS extends Canvas { // paint this canvas public void paint(final Graphics g) { g.drawRect(50,25,300,100); g.drawString("FRM is-a container",60,50); g.drawString("CVS is-not-a container",60,80); }  A Canvas is used to draw some shapes on it using the Graphics. It has the paint method.  CVS is an inner class  A Graphics object is used to draw shapes on the canvas  FRM is a container – it contains a CVS object Component Canvas Window Container Frame CVS Object FRM Graphics

Java Programming 9 Component  A component is an object having a graphical representation  Component is an abstract class  Components can be displayed on the screen  Components allow the user to interact with the program

Java Programming 10 Button  A Button is a component that simulates the appearance of a push button  When the user presses the mouse inside a button an ActionEvent is generated import java.awt.*; import java.awt.event.*; class BTN extends Frame { BTN() { super("Example: Button"); final Button b = new Button("Press me!"); b.addActionListener(new ActionListener() { // the event handler public void actionPerformed(ActionEvent ae) { b.setLabel("Thank you!"); } }); add(b); setSize(200,100); } public static void main(String[] args) { new BTN().setVisible(true); } ActionEvent Mouse click Anonymous Class

Java Programming 11 Label and TextField  A Label displays a string that cannot be changed by a user  A TextField allows a user to enter or edit one line of text  A FlowLayout arranges components : in a directional flow (left-to- right, or right-to-left) horizontally until no more components fit on the same line import java.awt.*; import java.awt.event.*; class LTF extends Frame { LTF() { super("Example: Label & TextField"); setLayout(new FlowLayout(FlowLayout.LEFT)); setResizable(false); add(new Label("Cannot edit!")); final TextField tf = new TextField("Edit me!",37); tf.addTextListener(new TextListener() { public void textValueChanged(TextEvent te) { System.out.println(te.paramString()); } }); add(tf); setSize(400,100); } public static void main(String[] args) { new LTF().setVisible(true); }

Java Programming 12 List  The List component presents the user with a scrolling list of text items  It can be set up so that the user can choose either one item or multiple items import java.awt.*; import java.awt.event.*; class LST extends Frame { LST() { super("Example: List"); final List l = new List(); l.add("I"); l.add("like"); l.add("programming"); l.add("in"); l.add("Java"); l.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent ie) { System.out.println(ie.paramString()); } }); add(l); setSize(200,150); } public static void main(String[] args) { new LST().setVisible(true); } Mouse click

Java Programming 13 Menu, MenuBar and MenuItem  A frame may contain a menu bar with options (i.e. items)  When the mouse is clicked on an option a drop down menu appears  Each menu consists of one or more menu items import java.awt.*; import java.awt.event.*; class MNB extends Frame { MNB() { super("Example: MenuBar"); final MenuBar mb = new MenuBar(); setMenuBar(mb); final Menu m = new Menu("File"); MenuItem mi; mi = new MenuItem("Exit"); mi.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { System.exit(0); } }); m.add(mi); mb.add(m); setSize(250,100); } public static void main(String[] args) { new MNB().setVisible(true); }

Java Programming 14 Layout Managers  A layout manager helps in arranging the components in a container  Each layout manager: Encapsulates an algorithm for positioning and sizing of components Automatically calculates the coordinates of each component it manages If a container is resized, the layout manager readjusts the placement of the components

Java Programming 15 BorderLayout  Allows placing of components by using the geographic terms: CENTER EAST NORTH SOUTH WEST  The components are placed around the edges  The component in the center uses the remaining space import java.awt.*; import java.awt.event.*; class BLM extends Frame { BLM() { super("Example: BorderLayout"); setLayout(new BorderLayout()); add(new Button("Center"),BorderLayout.CENTER); add(new Button("East"),BorderLayout.EAST); add(new Button("North"),BorderLayout.NORTH); add(new Button("South"),BorderLayout.SOUTH); add(new Button("West"),BorderLayout.WEST); setSize(200,200); } public static void main(String[] args) { new BLM().setVisible(true); }

Java Programming 16 GridLayout  Automatically arranges components in a grid of rows and columns  The container is divided into equal- sized cells, and one component is placed in each cell import java.awt.*; import java.awt.event.*; class GLM extends Frame { GLM() { super("Example: GridLayout"); setLayout(new GridLayout(2,2)); add(new Button("1,1")); add(new Button("1,2")); add(new Button("2,1")); add(new Button("2,2")); setSize(250,100); } public static void main(String[] args) { new GLM().setVisible(true); }

Java Programming 17 Panel  Panel is the simplest container class  A panel provides space in which an application can attach any other component, including other panels  The default layout manager for a panel is the FlowLayout manager import java.awt.*; import java.awt.event.*; class PNL extends Frame { PNL() { super("Example: Panel"); final Panel p = new Panel(); p.add(new Button("1")); p.add(new Button("2")); p.add(new Button("3")); add(p); setSize(250,100); } public static void main(String[] args) { new PNL().setVisible(true); }

Java Programming 18 Swing  Differences between AWT and Swing: Swing components use no native code and they can be present on every platform Typically, Swing components start their names with ‘J’ Have capabilities beyond what equivalent AWT components can offer Swing components need not be rectangular Swing components can dynamically change their appearance (i.e. pluggable look-and-feel)

Java Programming 19 Swing Components (Java Look and Feel)

Java Programming 20 Example: Hello World import java.awt.*; import java.awt.event.*; import javax.swing.*; class HLW extends JFrame { HLW() { super("Example: Swing GUI"); final JButton b = new JButton("Show message!"); b.addActionListener(new HLWButtonListener(b)); add(b); setSize(250,100); } public static void main(String[] args) { new HLW().setVisible(true); } class HLWButtonListener implements ActionListener { private JButton jb; HLWButtonListener(JButton b) { jb = b; } public void actionPerformed(ActionEvent e) { jb.setText("Hello World!"); } ActionEvent Mouse click

Java Programming 21 Creating New Window Frame // Dialog Box import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class CreatNewFrame extends JFrame { JLabel client_title; JButton create_button; public CreatNewFrame() { getContentPane().setLayout(new GridLayout(1,0)); create_button = new JButton("Create"); create_button.addActionListener(new ButtonListener()); getContentPane().add(create_button); } class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { NewFrame nf = new NewFrame(); nf.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} }); nf.setTitle("New Window Frame"); nf.setSize(200,150); nf.setVisible(true); } public static void main (String args[]) { CreatNewFrame f = new CreatNewFrame(); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} }); f.setTitle("Create New Frame"); f.setSize(200,150); f.setVisible(true); } } // end of CreatNewFrame class NewFrame extends JFrame { JLabel label; public NewFrame() { getContentPane().setLayout(new FlowLayout()); label = new JLabel("Another New Frame"); getContentPane().add(label); } // NewFrame constructor } // end of NewFrame class Button clicked

Java Programming 22 Dialogs  A dialog is a special window to convey a message or provides a special function  Every dialog is dependent on a frame – when that frame is destroyed, so are its dependent dialogs  A modal dialog blocks user input to all other windows in the program import java.awt.*; import java.awt.event.*; import javax.swing.*; class DLG extends JFrame { DLG() { super("Example: Swing Dialog"); final JFrame jf = this; final JButton jb = new JButton("Show a message dialog!"); jb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { JOptionPane.showMessageDialog(jf,"This is a simple message dialog"); } }); add(jb); setSize(250,100); } public static void main(String[] args) { new DLG().setVisible(true); }

Java Programming 23 FileChooser  File choosers provide a GUI for navigating the file system or open a file  To display a file chooser, use the JFileChooser API to show a modal dialog containing the file chooser import javax.swing.*; class FCH extends JFrame { final JLabel jl = new JLabel(); FCH() { super("Example: Swing FileChooser"); add(jl); setSize(300,50); } public static void main(String[] args) { final FCH fch = new FCH(); final JFileChooser jfc = new JFileChooser(); fch.setVisible(true); final int val = jfc.showOpenDialog(fch); if(val == JFileChooser.APPROVE_OPTION) fch.jl.setText("You chose to open this file: " + jfc.getSelectedFile().getName()); }