The Abstract Windowing Toolkit. Java Foundation Classes. Swing. In April 1997, JavaSoft announced the Java Foundation Classes (JFC).



Similar documents
Swing. A Quick Tutorial on Programming Swing Applications

GUIs with Swing. Principles of Software Construction: Objects, Design, and Concurrency. Jonathan Aldrich and Charlie Garrod Fall 2012

GUI Components: Part 2

LAYOUT MANAGERS. Layout Managers Page 1. java.lang.object. java.awt.component. java.awt.container. java.awt.window. java.awt.panel

5.17 GUI. Xiaoyi Jiang Informatik I Grundlagen der Programmierung

Mouse Event Handling (cont.)

Fachbereich Informatik und Elektrotechnik Java Swing. Advanced Java. Java Swing Programming. Programming in Java, Helmut Dispert

Programming with Java GUI components

Java is commonly used for deploying applications across a network. Compiled Java code

Using A Frame for Output

CS 335 Lecture 06 Java Programming GUI and Swing

Cours de Java. Sciences-U Lyon. Java - Introduction Java - Fondamentaux Java Avancé.

@ - Internal # - Online TH PR OR TW TOTAL HOURS # @ 175

Java GUI Programming

Graphical User Interfaces

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 7: Object-Oriented Programming. Introduction

The Basic Java Applet and JApplet

How To Write A Program For The Web In Java (Java)

JIDE Common Layer Developer Guide (Open Source Project)

JiST Graphical User Interface Event Viewer. Mark Fong


Extending Desktop Applications to the Web

How to Convert an Application into an Applet.

CaptainCasa. CaptainCasa Enterprise Client. CaptainCasa Enterprise Client. Feature Overview

Microsoft FrontPage 2003

Essentials of the Java Programming Language

Essentials of the Java(TM) Programming Language, Part 1

Assignment # 2: Design Patterns and GUIs

Nexawebホワイトペーパー. Developing with Nexaweb ~ Nexaweb to Improve Development Productivity and Maintainability

Event processing in Java: what happens when you click?

Principles of Software Construction: Objects, Design and Concurrency. GUIs with Swing. toad Spring 2013

JIDE Action Framework Developer Guide

Software Design: Figures

public class Craps extends JFrame implements ActionListener { final int WON = 0,LOST =1, CONTINUE = 2;

Creating Personal Web Sites Using SharePoint Designer 2007

Syllabus for CS 134 Java Programming

Homework/Program #5 Solutions

Skills and Topics for TeenCoder: Java Programming

JIDE Docking Framework Developer Guide

core. Volume I - Fundamentals Seventh Edition Sun Microsystems Press A Prentice Hall Title ULB Darmstadt

Mapping to the Windows Presentation Framework

Tutorial Reference Manual. Java WireFusion 4.1

Using Adobe Dreamweaver CS4 (10.0)

Here's the code for our first Applet which will display 'I love Java' as a message in a Web page

Lab 1A. Create a simple Java application using JBuilder. Part 1: make the simplest Java application Hello World 1. Start Jbuilder. 2.

method is never called because it is automatically called by the window manager. An example of overriding the paint() method in an Applet follows:

How To Build A Swing Program In Java.Java.Netbeans.Netcode.Com (For Windows) (For Linux) (Java) (Javax) (Windows) (Powerpoint) (Netbeans) (Sun) (

An Overview of Java. overview-1

Dev Articles 05/25/07 11:07:33

For Introduction to Java Programming, 5E By Y. Daniel Liang

Interactive Programs and Graphics in Java

Microsoft PowerPoint Tutorial

How To Program In Java (Ipt) With A Bean And An Animated Object In A Powerpoint (For A Powerbook)

Java GUI Programming. Building the GUI for the Microsoft Windows Calculator Lecture 2. Des Traynor 2005

Java and JavaScript. Krishna Tateneni

CSC 551: Web Programming. Spring 2004

Microsoft Publisher 2010 What s New!

Site Maintenance. Table of Contents

Adobe Dreamweaver CC 14 Tutorial

Packaging and Deploying Java Projects in Forte

Java applets. SwIG Jing He

Asset Track Getting Started Guide. An Introduction to Asset Track

Generating Automated Test Scripts for AltioLive using QF Test

Tutorial: Time Of Day Part 2 GUI Design in NetBeans

Artificial Intelligence. Class: 3 rd

Introduction to OpenOffice Writer 2.0 Jessica Kubik Information Technology Lab School of Information University of Texas at Austin Fall 2005

Study of Development of Java Applications in Eclipse Environment and Development of Java Based Calendar Application with Notifications

SAS BI Dashboard 4.3. User's Guide. SAS Documentation

Creating Web Pages with Microsoft FrontPage

Karsten Lentzsch JGoodies SWING WITH STYLE

Mesa DMS. Once you access the Mesa Document Management link, you will see the following Mesa DMS - Microsoft Internet Explorer" window:

Advanced Network Programming Lab using Java. Angelos Stavrou

Spreadsheet - Introduction

Course/Year W080/807 Expected Solution Subject: Software Development to Question No: 1

Fondamenti di Java. Introduzione alla costruzione di GUI (graphic user interface)

Clip Art in Office 2000

Getting Started with Vision 6

Project Builder for Java. (Legacy)

INTRODUCTION TO DESKTOP PUBLISHING

Joomla Article Advanced Topics: Table Layouts

Virto SharePoint Gantt Chart App for Office 365 Release User and Installation Guide

To Begin Customize Office

Using NetBeans IDE for Desktop Development. Geertjan Wielenga

Create Charts in Excel

understand how image maps can enhance a design and make a site more interactive know how to create an image map easily with Dreamweaver

Flash Tutorial Part I

CS506 Web Design and Development Solved Online Quiz No. 01

Hypercosm. Studio.

ECDL. European Computer Driving Licence. Spreadsheet Software BCS ITQ Level 2. Syllabus Version 5.0

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

JavaFX Session Agenda

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

Excel basics. Before you begin. What you'll learn. Requirements. Estimated time to complete:

How to create labels using a Microsoft Access data file?

Transcription:

The Abstract Windowing Toolkit Since Java was first released, its user interface facilities have been a significant weakness The Abstract Windowing Toolkit (AWT) was part of the JDK form the beginning, but it really was not sufficient to support a complex user interface JDK 1.1 fixed a number of problems, and most notably, it introduced a new event model. It did not make any major additions to the basic components ICSS235 - Swing 1 Java Foundation Classes In April 1997, JavaSoft announced the Java Foundation Classes (JFC). a major part of the JFC is a new set of user interface components called Swing. AWT Swing Accessibility Java 2D Drag And Drop ICSS235 - Swing 2 Swing Swing classes are used to build GUIs Swing does not stand for anything Swing is built on top of the 1.1 and 1.2 AWT libraries Swing makes 3 major improvements on the AWT does not rely on the platform s native components it supports Pluggable Look-and-Feel or PLAF based on Model-View-Controller (MVC) JFC AWT Swing JDK 1.2 ICSS235 - Swing 3 1

GUI Packages AWT java.awt java.awt.color java.awt.datatransfer java.awt.event java.awt.font java.awt.geom java.awt.image... Swing javax.accessibility javax.swing javax.swing.colorchooser javax.swing.event javax.swing.filechooser javax.swing.plaf javax.swing.table javax.swing.text.html javax.swing.tree... ICSS235 - Swing 4 Components A graphical user interface consists of different graphic Component objects which are combined into a hierarchy using Container objects. Component class An abstract class for GUI components such as menus, buttons, labels, lists, etc. Container An abstract class that extends Component. Classes derived from Container, most notably Panel, Applet, Window, Dialog, Frame, can contain multiple components. ICSS235 - Swing 5 Weighing Components Sun make a distinction between lightweightand heavyweight components Lightweight components are not dependent on native peers to render themselves. They are coded in Java. Heavyweight components are rendered by the host operating system. They are resources managed by the underlying window manager. ICSS235 - Swing 6 2

Heavyweight Components Heavyweight components were unwieldy for two reasons Equivalent components on different platforms do not necessarily act alike. The look and feel of each component was tied to the host operating system Almost all Swing components are lightweight except JApplet, JFrame, JDialog, and JWindow ICSS235 - Swing 7 Additional Swing Features Swing also provides A wide variety of components (tables, trees, sliders, progress bars, internal frame, ) Swing components can have tooltips placed over them. Arbitrary keyboard events can be bound to components. Additional debugging support. Support for parsing and displaying HTML based information. ICSS235 - Swing 8 Applets versus Applications It is possible to create two different types of GUIs Standalone applications Programs that are started from the command line Code resides on the machine on which they are run Applets Programs run inside a web browser Code is downloaded from a web server JVM is contained inside the web browser For security purposes Applets are normally prevented from doing certain things (for example opening files) For now we will write standalone applications ICSS235 - Swing 9 3

JFrames A JFrame is a Window with all of the adornments added. A JFrame provides the basic building block for screen-oriented applications. JFrame win = new JFrame( title ); ICSS235 - Swing 10 import javax.swing.*; Creating a JFrame public class SwingFrame { public static void main( String args[] ) { JFrame win = new JFrame( "My First GUI Program" ); win.show(); // SwingFrame ICSS235 - Swing 11 JFrame Sizing a Frame You can specify the size Height and width given in pixels The size of a pixel will vary based on the resolution of the device on which the frame is rendered The method, pack(), will set the size of the frame automatically based on the size of the components contained in the content pane Note that pack does not look at the title bar ICSS235 - Swing 12 4

import javax.swing.*; Creating a JFrame public class SwingFrame { public static void main( String args[] ) { JFrame win = new JFrame( "My First GUI Program" ); win.setsize( 250, 150 ); win.show(); // SwingFrame ICSS235 - Swing 13 Glass pane JFrame Layered pane Menu bar Content pane The content pane is where the components will be placed The entire collection of panes is called the RootPane ICSS235 - Swing 14 Swing Components JComponent JComboBox, JLabel, JList, JMenuBar, JPanel, JPopupMenu, JScrollBar, JScrollPane, JTable, JTree, JInternalFrame, JOptionPane, JProgressBar, JRootPane, JSeparator, JSlider, JSplitPane, JTabbedPane, JToolBar, JToolTip, Jviewport, JColorChooser, JTextComponent, ICSS235 - Swing 15 5

JLabels JLabels are components that you can put text into. When creating a label you can specify the initial value and the alignment you wish to use within the label. You can use gettext() and settext() to get and change the value of the label. lbl = new JLabel( text", JLabel.RIGHT ) ; ICSS235 - Swing 16 Hello World import javax.swing.*; public class SwingFrame { public static void main( String args[] ) { JFrame win = new JFrame( "My First GUI Program" ); JLabel label = new JLabel( "Hello World" ); win.getcontentpane().add( label ); win.pack(); win.show(); // SwingFrame ICSS235 - Swing 17 JButtons JButton extends Component, displays a string and delivers anactionevent for each mouse click. Normally buttons are displayed with a border In addition to text, JButtons can also display button icons= new JButton( text ) ; ICSS235 - Swing 18 6

Buttons import javax.swing.*; public class SwingFrame { public static void main( String args[] ) { JFrame win = new JFrame( "My First GUI Program" ); JButton button = new JButton( "Click Me!!" ); win.getcontentpane().add( button ); win.pack(); win.show(); // SwingFrame ICSS235 - Swing 19 Layout Manager Layout Manager An interface that defines methods for positioning and sizing objects within a container. Java defines several default implementations of LayoutManager. Geometrical placement in a Container is controlled by a LayoutManager object ICSS235 - Swing 20 Components, Containers, and Layout Managers Containers may contain components (which means containers can contain containers!!). All containers come equipped with a layout manager which positions and shapes (lays out) the container's components. Much of the action in the AWT occurs between components, containers, and their layout managers. ICSS235 - Swing 21 7

Layout Managers Layouts allow you to format components on the screen in a platform independent way The standard JDK provides five classes that implement the LayoutManager interface: FlowLayout GridLayout BorderLayout CardLayout GridBagLayout Layout managers are defined in the AWT package ICSS235 - Swing 22 Changing the Layout To change the layout used in a container you first need to create the layout. Then the setlayout() method is invoked on the container is used to use the new layout. JPanel p = new JPanel() ; p.setlayout( new FlowLayout() ); The layout manager should be established before any components are added to the container ICSS235 - Swing 23 FlowLayout FlowLayout is the default layout for the JPanel class. When you add components to the screen, they flow left to right (centered) based on the order added and the width of the screen. Very similar to word wrap and full justification on a word processor. If the screen is resized, the components' flow will change based on the new width and height ICSS235 - Swing 24 8

import javax.swing.*; import java.awt.*; Flow Layout public class SwingFrame { public static void main( String args[] ) { JFrame win = new JFrame( "My First GUI Program" ); win.getcontentpane().setlayout( new FlowLayout() ); for ( int i = 0; i < 10; i++ ) win.getcontentpane().add( new JButton( String.valueOf( i ) ) ); win.pack(); win.show(); // SwingFrame ICSS235 - Swing 25 FlowLayout ICSS235 - Swing 26 GridLayout Arranges components in rows/columns. If the number of rows is specified, the number of columns will the number of components divided by the rows If the number of columns is specified, the number of rows will be the number of components divided by the columns Specifying the number of columns affects the layout only when the number of rows is set to zero. The order in which you add components is relevant. ICSS235 - Swing 27 9

GridLayout gridlayout( 2, 4 ) gridlayout( 0, 4 ) gridlayout( 4, 4 ) gridlayout( 10, 10 ) ICSS235 - Swing 28 BorderLayout BorderLayout provides 5 areas to hold components. These are named after the four different borders of the screen, North, South, East, West, and Center. When a Component is added to the layout, you must specify which area to place it in. The order in which components is not important. The center area will always be resized to be as large as possible ICSS235 - Swing 29 BorderLayout import javax.swing.*; import java.awt.*; public class SwingFrame { public static void main( String args[] ) { JFrame win = new JFrame( "My First GUI Program" ); Container content = win.getcontentpane(); content.setlayout( new BorderLayout() ); content.add( "North", new JButton( "North" ) ); content.add( "South", new JButton( "South" ) ); content.add( "East", new JButton( "East" ) ); content.add( "West", new JButton( "West" ) ); content.add( "South", new JButton( "South" ) ); content.add( "Center", new JButton( "Center" ) ); win.pack(); win.show(); // SwingFrame ICSS235 - Swing 30 10

BorderLayout ICSS235 - Swing 31 Containers A JFrame is not the only type of container that you can use in Swing The subclasses of Container are: JPanel JWindow JApplet Window is subclassed as follows: JDialog JFrame ICSS235 - Swing 32 A Simple 4 Function Calculator ICSS235 - Swing 33 11

Swing Components JFrame with BorderLayout JLabel JButton JPanel with GridLayout ICSS235 - Swing 34 import javax.swing.*; import java.awt.*; import java.awt.event.*; CalcGui.java public class CalcGui implements { // Labels for the buttons private static final String labels = "789X456/123-0C=+"; private static final int NUMROWS = 4; private static final int NUMCOLS = 4; private JLabel display; // The display public CalcGui( String name ) { // A Frame for the calculator JFrame win = new JFrame(name); ICSS235 - Swing 35 CalcGui.java // Create the button panel JPanel buttons = new JPanel(); buttons.setlayout(new GridLayout(NUMROWS, NUMCOLS)); JButton b; for ( int i = 0 ; i < labels.length() ; i++ ) { b = new JButton( labels.substring( i, i + 1 ) ); buttons.add( b ); // Create the display display = new JLabel( "0", JLabel.RIGHT ) ICSS235 - Swing 36 12

CalcGui.java // "Assemble" the calculator Container content = win.getcontentpane(); content.setlayout( new BorderLayout() ); content.add( "North", display ); content.add( "Center", buttons ); // Display it and let the user run with it :-) win.pack(); win.show(); ICSS235 - Swing 37 13