Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

GUI. Swing Programs Four basic types of Top Level Window – JFrame, a top level window decorated like a native window – JWindow, an undecorated stand-alone.
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.
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.
Graphics Programming. In this class, we will cover: The difference between AWT and Swing Creating a frame Frame positioning Displaying information in.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
Creating a GUI with Swing. Introduction Very useful link: Swing – is a part of JFC (Java Foundation.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
CPSC150 Week 12 Graphical User Interfaces Chapter 11.
Graphic User Interfaces Part 1. Typical GUI Screen from Microsoft Word What GUI “components” can you see? –Menus? Buttons? Labels? What else? –Anything.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
Introduction to Java GUI Creating Graphical User Interfaces © copyright Bobby Hoggard / material may not be redistributed without permission.
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.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 13 Creating User.
CIS 068 Welcome to CIS 083 ! Introduction to GUIs: JAVA Swing.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal
JAVA, JAVA, JAVA Object-Oriented Problem Solving Ralph Morelli Trinity College Hartford, CT presentation slides for published by Prentice Hall Second Edition.
Introduction to Swing Components Chapter 14.  Part of the Java Foundation Classes (JFC)  Provides a rich set of GUI components  Used to create a Java.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental.
Chapter 9: Visual Programming Basics Object-Oriented Program Development Using Java: A Class-Centered Approach.
Java Graphics. Review 3 kinds of elements in components API? Layout managers Events Extend vs. Implement.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
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.
Swing Differences between Swing and AWT Naming Conventions All Swing components begin with a capital J -- JPanel, JButton, JScrollBar, JApplet, etc..
JAVA, JAVA, JAVA Object-Oriented Problem Solving Ralph Morelli | Ralph Walde Trinity College Hartford, CT presentation slides for published by Prentice.
Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila.
Omer Boyaci.  GUIs are event driven.  When the user interacts with a GUI component, the interaction—known as an event—drives the program to perform.
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.
Lec.10 (Chapter 8 & 9) GUI Jiang (Jen) ZHENG June 27 th, 2005.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
Swing Components. Introduction Swing – A set of GUI classes – Part of the Java's standard library –Much better than the previous library: AWT Abstract.
Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus.
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.
Computer Science [3] Java Programming II - Laboratory Course Lab 4 -1 : Introduction to Graphical user interface GUI Components Faculty of Engineering.
 Many event-listener interfaces contain multiple methods.  An adapter class implements an interface and provides a default implementation (with an empty.
 Figure illustrates a hierarchy containing many event classes from the package java.awt.event.  Used with both AWT and Swing components.  Additional.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
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 Programming in Java: Frames, Simple Components, and Layouts.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
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.
Welcome To java
Chap 7. Building Java Graphical User Interfaces
Chapter 13: Advanced GUIs and Graphics
Graphical User Interfaces -- Introduction
Advanced GUIs and Graphics
Graphical User Interface
Presentation transcript:

Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They depend on the native system. javax.swing Component Container Panel Window Frame Dialog Object java.lang JApplet java.awt java.applet Applet JDialog JFrame JWindow JComponent Class extends Key Abstract Class implements Interface Package

Swing Programs Four basic types of Top Level Window – JFrame, a top level window decorated like a native window – JWindow, an undecorated stand-alone window (splash- screen) – JApplet, an embeddable applet – JDialog, a popup dialog window Each program type is implemented as a framework class

Swing Hierarchy (Part II) Swing components names start with ‘J’. JButton JMenuItem Class extends Key Abstract Class implements Interface Package Object java.lang JComponent java.awt Component Container avax.swing AbstractButton JToggleButton JMenu JLabel JTextComponent JTextField JTextArea JPasswordField JCheckbox JRadioButton JPopupMenu JPanel JMenuBar JScrollPane JList JOptionPane

Event Classes ComponentsEventsDescription Button, JButtonActionEventUser clicked button CheckBox, JCheckBoxItemEventUser toggled a checkbox CheckboxMenuItem, JCheckboxMenuItemItemEventUser toggled a checkbox Choice, JPopupMenuItemEventUser selected a choice Component, JComponentComponentEventComponent was moved or resized FocusEventComponent acquired or lost focus KeyEventUser typed a key MouseEventUser manipulated the mouse Container, JContainerContainerEventComponent added/removed from container List, JListActionEventUser double-clicked a list item ItemEventUser clicked a list item Menu, JMenuActionEventUser selected menu item Scrollbar, JScrollbarAdjustmentEventUser moved scrollbar TextComponent, JTextComponentTextEventUser edited text TextField, JTextFieldActionEventUser typed Enter key Window, JWindowWindowEventUser manipulated window AWT events for each type of component. AWT events for each type of component.

New Swing Event Classes ComponentEvents Description JPopupMenuPopupMenuEvent User selected a choice JComponentAncestorEvent An event occurred in an ancestor JListListSelectionEvent User double-clicked a list item ListDataEvent List's contents were changed JMenuMenuEvent User selected menu item JTextComponentCaretEvent Mouse clicked in text UndoableEditEvent An undoable edit has occurred JTableTableModelEvent Items added/removed from table TableColumnModelEvent A table column was moved JTreeTreeModelEvent Items added/removed from tree TreeSelectionEvent User selected a tree node TreeExpansionEvent User changed tree node JWindowWindowEvent User manipulated window Newly defined Swing events. Newly defined Swing events.

GUI Design: Choosing Basic Components Swing objects for input, output, control, guidance: Swing objects for input, output, control, guidance: Guidance: A JLabel displays a short string of text or an image. It can serve as a prompt. Guidance: A JLabel displays a short string of text or an image. It can serve as a prompt. Input: A JTextField allows editing of a single line of text. It can get the user’s input. Input: A JTextField allows editing of a single line of text. It can get the user’s input. Output: A JTextArea allows editing of multiple lines of text. We’ll use it to display results. Output: A JTextArea allows editing of multiple lines of text. We’ll use it to display results. Control: A JButton is an action control. By implementing the ActionListener interface we will handle the user's action events. Control: A JButton is an action control. By implementing the ActionListener interface we will handle the user's action events.

Types of Layout Managers ManagerDescription java.awt.BorderLayoutArranges elements along the north, south, east, west, and in the center of the container. java.swing.BoxLayoutArranges elements in a single row or single column. java.awt.CardLayoutArranges elements like a stack of cards, with one visible at a time. java.awt.FlowLayoutArranges elements left to right across the container. java.awt.GridBagLayoutArranges elements in a grid of variable sized cells (complicated). java.awt.GridLayoutArranges elements into a two-dimensional grid of equally sized cells. java.swing.OverlayLayoutArranges elements on top of each other.

Default Layout Managers ContainerLayout Manager JAppletBorderLayout (on its content pane) JBoxBoxLayout JDialogBorderLayout (on its content pane) JFrameBorderLayout (on its content pane) JPanelFlowLayout JWindowBorderLayout (on its content pane) In AWT, the default layout for applets was FlowLayout. Top-level windows use BorderLayout

JTextField method setFont (inherited by JTextField indirectly from class Component ) sets the font of the JTextField to a new Font (package java.awt ). String passed to the JCheckBox constructor is the checkbox label that appears to the right of the JCheckBox by default. When the user clicks a JCheckBox, an ItemEvent occurs. – Handled by an ItemListener object, which must implement method itemStateChanged. An ItemListener is registered with method addItemListener. JCheckBox method isSelected returns true if a JCheckBox is selected. (C) 2010 Pearson Education, Inc. All rights reserved.

Painting When a GUI needs to change its visual appearance it performs a paint operation Swing components generally repaint themselves as needed Painting code executes on the event- dispatching thread – If painting takes a long time, no events will be handled during that time

Example import javax.swing.*; import java.awt.*; public class Painting extends JPanel { public Painting() {} public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor( Color.yellow ); g.fillOval( 10,10,50,50 ); g.setColor( Color.black ); g.drawOval( 10,10,50,50 ); } public static void main( String args[] ) { JFrame win = new JFrame( "Painting" ); win.setSize(100, 100); win.getContentPane().add( new Painting() ); win.setvisible(true); }}

The Graphics Object The Graphics object both a context for painting and methods for performing the painting. The graphics context consists of state such as the current painting color, the current font, and the current painting area – The color and font are initialized to the foreground color and font of the component just before the invocation of paintComponent

The Coordinate System Each component has its own integer coordinate system – Ranging from (0, 0) to (width - 1, height - 1) – Each unit represents the size of one pixel

Color Combinations of Red, Green, Blue Each [0, 255] Java: new Color(255, 150, 0) Hokie Orange

Font Color and font: g2.setColor( new Color(r,g,b) ); g2.setFont( new Font(…) ); new font(“Serif”, Font.BOLD, Font.Italic, 18);

Re-Paint Screen is like a painter’s canvas All windows paint on the same surface! Windows don’t “remember” whats under them Need to re-paint when portions are newly exposed Receive Repaint events Open, resize, bring to front When other windows in front move, resize, close

MVC Paradigm The MVC paradigm breaks applications or interfaces into three parts: the model, the view, and the controller. Users interact with a controller (e.g., buttons), and make changes to the model (e.g., data), which is then reflected in the view (e.g., graph). A --> 25 % B --> 60 % C --> 15 % Model View(s) Draw Graph Control

MVC Relationships ModelView Controller MVC Relationships The Controller has references to both the view and the model; the controller is the base of the triad. The Model might have a reference to the view, to allow for notification of changes. The model does not know who controls it, however. The View has a reference to the model, for gathering data. View also has reference to the Controller, to notify of successful updates. (References are minimal, perhaps to a base class, to allow swapping of controls.)

(C) 2010 Pearson Education, Inc. All rights reserved. Many event-listener interfaces contain multiple methods. An adapter class implements an interface and provides a default implementation (with an empty method body) of each method in the interface. You extend an adapter class to inherit the default implementation of every method and override only the method(s) you need for event handling.

(C) 2010 Pearson Education, Inc. All rights reserved.

Interface MouseWheelListener enables applications to respond to the rotation of a mouse wheel. Method mouseWheelMoved receives a MouseWheelEvent as its argument. Class MouseWheelEvent (a subclass of Mouse-Event ) contains methods that enable the event handler to obtain information about the amount of wheel rotation.

(C) 2010 Pearson Education, Inc. All rights reserved.

Class Point (package java.awt ) represents an x-y coordinate. – We use objects of this class to store the coordinates of each mouse drag event. Class Graphics is used to draw. MouseEvent method getPoint obtains the Point where the event occurred. Method repaint (inherited from Component ) indicates that a Component should be refreshed on the screen as soon as possible.

(C) 2010 Pearson Education, Inc. All rights reserved.

Graphics method fillOval draws a solid oval. – Four parameters represent a rectangular area (called the bounding box) in which the oval is displayed. – The first two are the upper-left x-coordinate and the upper-left y- coordinate of the rectangular area. – The last two represent the rectangular area’s width and height. Method fillOval draws the oval so it touches the middle of each side of the rectangular area.

(C) 2010 Pearson Education, Inc. All rights reserved.