Module 13: Swing API Object Oriented Programming(Java)

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

Introduction to Java 2 Programming
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 program.
Graphical User Interfaces Java’s AWT and Swing APIs.
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.
Introduction to JFC Swing Written by Adam Carmi. Agenda About JFC and Swing Pluggable Look and Feel Swing Components Borders Layout Management Events.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
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.
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.
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.
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.
Introduction to Java GUI Creating Graphical User Interfaces © copyright Bobby Hoggard / material may not be redistributed without permission.
Introduction to Swing. Introduction and Background Swing is Sun’s second Java GUI kit Built on AWT, but most programs will directly use only Swing classes.
Intro to Java 2 By Geb Thomas Based on the Java TutorialJava Tutorial.
CIS 068 Welcome to CIS 083 ! Introduction to GUIs: JAVA Swing.
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.
Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal
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.
3461A Readings from the Swing Tutorial. 3461A Overview  The follow is the Table of Contents from the trail “Creating a GUI with JFC/Swing” in the “The.
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.
 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.
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.
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.
1 Java Swing - Lecture 2 Components and Containment Boriana Koleva
Layout Manager Summary
Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila.
CSCI Swing1 The Abstract Windowing Toolkit Since Java was first released, its user interface facilities have been a significant weakness –The Abstract.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
University of Limerick1 Software Architecture Java Layout Managers.
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
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.
Swing Components. Introduction Swing – A set of GUI classes – Part of the Java's standard library –Much better than the previous library: AWT Abstract.
Swinging in Your Java Playground. Background Swing is a part of the Java Foundation Classes (JFC). The JFC is made up of features intended to give a programmer.
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.
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.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
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:
Introduction to Swing Mr. Crone. What is Swing? a collection of pre-made Java classes used to create a modern graphical user interface.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
GUI 1: JFC and Swing Basics OOP tirgul No
A Quick Java Swing Tutorial
Graphical User Interfaces
University of Central Florida COP 3330 Object Oriented Programming
Java Swing.
University of Central Florida COP 3330 Object Oriented Programming
A Quick Java Swing Tutorial
Chap 7. Building Java Graphical User Interfaces
Chapter 13: Advanced GUIs and Graphics
Timer class and inner classes
Steps to Creating a GUI Interface
A Quick Java Swing Tutorial
Advanced Programming in Java
Graphical User Interface
Presentation transcript:

Module 13: Swing API Object Oriented Programming(Java)

About JFC and Swing Pluggable Look and Feel Swing Components Borders Layout Management Events Handling Swing API (Agenda)

JFC – JavaTM Foundation Classes Encompass a group of features for constructing graphical user interfaces (GUI). Implemented without any native code. “Swing” is the codename of the project that developed the first JFC components (JFC 1.11). The name “Swing” is frequently used to refer to new components and related API. Swing API (About JFC and Swing)

Swing features: –The Swing Components Dialog, Tabbed pane, Buttons, File Chooser,... –Pluggable Look and Feel –Accessibility API Screen readers, Braille displays,... –Java 2DTM API (Java 2 Platform only) –Drag and Drop (Java 2 Platform only) Between Java applications and native applications. Swing API: About JFC and Swing (cont)

Each picture shows the same program but with a different look and feel Swing API: Pluggable Look and Feel

Swing provides many standard GUI components such as buttons, lists, menus, and text areas, which you combine to create your program's GUI. Swing provides containers such as windows and tool bars. –top level: frames, dialogs –intermediate level: panel, scroll pane, tabbed pane,... –other Swing components: buttons, labels,... A visual index of Swing components Swing API: Swing Components

Descendents of the java.awt.Container class Components that can contain other components. Use a layout manager to position and size the components contained in them. Components are added to a container using one of the various forms of its add method –Depending on which layout manager is used by the container panel.add(component); Swing API (Containers)

Every program that presents a Swing GUI contains at least one top-level container. A Top level container provides the support that Swing components need to perform their painting and event- handling. Swing provides three top-level containers: –JFrame (Main window) –JDialog (Secondary window) –JApplet (An applet display area within a browser window) Swing API: Top Level Containers

To appear on screen, every GUI component must be part of a containment hierarchy, with a top-level container as its root. Each top-level container has a content pane that contains visible components in that top-level container’s GUI. Don’t add a component directly to a top-level container. Swing API: Top Level Containers (cont)

A frame implemented as an instance of the JFrame class, is a window that has decorations such as a border, a title and buttons for closing and iconifying the window. –The decorations on a frame are platform dependent. Applications with a GUI typically use at least one frame. Swing API: JFrame

import javax.swing.*; public class HelloWorldSwing { public static void main(String[] args) { JFrame frame = new JFrame("HelloWorldSwing"); final JLabel label = new JLabel("Hello World"); frame.getContentPane().add(label); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } pack() causes a window to be sized to fit the preferred size and layouts of its sub- components Swing API: Example 1

import javax.swing.*; public class HelloWorldFrame extends JFrame { public HelloWorldFrame() { super(“HelloWorldSwing”); final JLabel label = new JLabel("Hello World"); getContentPane().add(label); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setVisible(true); } public static void main(String[] args) { HelloWorldFrame frame = new HelloWorldFrame(); } In this example a custom frame is created Swing API: Example 2

Every dialog is dependent on a frame –Destroying a frame destroys all its dependent dialogs. –When the frame is iconified, its dependent dialogs disappear from the screen. –When the frame is deiconified, its dependent dialogs return to the screen. A dialog can be modal. When a modal dialog is visible it blocks user input to all other windows in the program. Swing API: JDialog

To create custom dialogs, use the JDialog class directly (as in the previous examples). Swing provides several standard dialogs –JProgressBar, JFileChooser, JColorChooser,... The JOptionPane class can be used to create simple modal dialogs –icons, title, text and buttons can be customized. Swing API: JDialog (Cont)

Object[] options = {"Yes!", "No, I'll pass", "Well, if I must"}; int n = JOptionPane.showOptionDialog( frame, "Duke is a cartoon mascot. \n" + "Do you still want to cast your vote?", "A Follow-up Question", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[2]); Swing API (Example 3)

JComponent is the base class for all Swing components except top-level containers. –JLabel, JButton, JList, JPanel, JTable,... To use a component that inherits from JComponent, it must be placed in a containment hierarchy who’s base is a top-level container. Swing API: JComponent

The JComponent class provides the following (partial list): – Pluggable Look & Feel – Keystroke handling – Tooltip support – Accessibility – An infrastructure for painting – Support for borders. All descendents of JComponent are also Containers – A JButton can contain text, icons etc. Swing API: JComponent

Every JComponent can have one or more borders. The class BorderFactory may be used to create standard borders pane.setBorder(BorderFactory. createLineBorder(Color.black)); Using a compound border, you can combine any two borders, which can themselves be compound borders BorderFactory.createCompoundBorder(border1, border2); Swing API : Borders

Swing API: Sample Borders

Swing API: Titled Borders

Swing API: Compound Border

Also known as panels or panes Simplify the positioning of other components. –JPanel Play a visible and interactive role in a program’s GUI –JScrollPane –JTabbedPane A panel’s default layout manager is FlowLayout. –Other layout managers can easily be set panel.setLayout(new BorderLayout()); Swing API: Intermediated Level Containers

By default, panels don’t paint anything except for their background. By default, panels are opaque. –An opaque panel can be set as a top-level container’s content pane. –transparent (non-opaque) panels draw no background. Swing API : Intermediated level containers (cont)

The process of determining the size and position of components. Layout management can be done using absolute positioning –Size and position of every component within the container must be specified. –Does not adjust well when the top-level container is resized. –Does not adjust well to differences between users and systems, such as font size. Swing API: Layout Management

Layout management is often performed using layout managers –Components can provide size and position hints to layout managers, but layout managers have the final say on the size and position of those components. Swing API: Layout Management (cont)

Layout hints –Minimum, preferred and maximum size –X axis alignment, Y axis alignment Customizing layout hints –Invoking setter methods: setMinimumSize, setAlignmentX,... –Subclassing and overriding the getter methods: getMinimumSize, getAlignmentX,... Swing API: Layout Management (cont

The Java platform supplies five commonly used layout managers: –BorderLayout –BoxLayout –FlowLayout –GridLayout –GridBagLayout Swing API: Layout Management (cont)

When using the add method to put a component in a container, the container’s layout manager must be taken into account. –Relative position (BorderLayout) panel.add(component, BorderLayout.CENTER); –Order of addition (BoxLayout, GridLayout,...) panel.add(component); Swing API: Layout Management (cont)

Has five areas available to hold components –north, south, east, west and center All extra space is placed in the center area –Only the center area is affected when the container is resized. Default layout manager of content panes. Swing API: BorderLayout

Places components in a single row (left to right) or column (top to bottom). Respects component’s maximum size and alignment hints. Swing API: BoxLayout

Places components from left to right, starting new rows if necessary. Default LayoutManager of JPanel Swing API: FlowLayout

The following factors influence the amount of space between visible components in a container: –Layout manager automatically, user specified, none –Invisible components often used with BoxLayout –Empty borders works best with components that have no default border such as panels and labels. Swing API: Layout Management (cont)

Places components in a requested number of rows and columns. Components are placed left-to-right and top-to- bottom. Forces all components to be the same size –as wide as the widest component's preferred width –as high as the highest component’s preferred height Swing API: GridLayout

Every time a user types a character or pushes a mouse button, an event occurs. Any object can be notified of an event by registering as an event listener on the appropriate event source. Multiple listeners can register to be notified of events of a particular type from a particular source. Swing API: Events Handling

Act that results in eventListener type User clicks a button, presses Return while typing in a text field, or chooses a menu item ActionListener User closes a frame (main window)WindowListener User presses a mouse button while the cursor is over a component MouseListener User moves the mouse over a componentMouseMotionListener Component becomes visibleComponentListener Component gets the keyboard focusFocusListener Table or list selection changesListSelectionListener Swing API: Types of Event Listener

Implement a listener interface or extend a class that implements a listener interface. Register an instance of the event handler class as a listener upon one or more components. Implement the methods in the listener interface to handle the event. Lesson: Writing Events Handlers Swing API: Implementing an Event Handler

button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { numClicks++; label.setText(labelPrefix + numClicks); }}); JButton Swing API: Example 4

Questions and Comments