Java Graphical User Interfaces

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

Graphical User Interface (GUI) components in Java Applets. With Abstract Window Toolkit (AWT) we can build an applet that has the basic GUI

Introduction to the JAVA UI classes Advanced HCI IAT351

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

encompass a group of features for building Graphical User Interfaces (GUI).

Swing UI. Powered by Pentalog. by Vlad Costel Ungureanu for Learn Stuff

Introduction p. 1 JFC Architecture p. 5 Introduction to JFC p. 7 The JFC 1.2 Extension p. 8 Swing p. 9 Drag and Drop p. 16 Accessibility p.

China Jiliang University Java. Programming in Java. Java Swing Programming. Java Web Applications, Helmut Dispert

Handout 14 Graphical User Interface (GUI) with Swing, Event Handling

Introduction to Graphical Interface Programming in Java. Introduction to AWT and Swing

Packages: Putting Classes Together

11/6/15. Objec&ves. RouleQe. Assign 8: Understanding Code. Assign 8: Bug. Assignment 8 Ques&ons? PROGRAMMING PARADIGMS

Java: Graphical User Interfaces (GUI)

CSE 331. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

CS 251 Intermediate Programming GUIs: Components and Layout

Chapter 12 GUI Basics

Swing. By Iqtidar Ali

Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008

Part 3: Graphical User Interface (GUI) & Java Applets

Java Swing. Lists Trees Tables Styled Text Components Progress Indicators Component Organizers

All the Swing components start with J. The hierarchy diagram is shown below. JComponent is the base class.

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

1. Swing Note: Most of the stuff stolen from or from the jdk documentation. Most programs are modified or written by me. This section explains the

Window Interfaces Using Swing Objects

CSE 331. Event- driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

Java IDE Programming-I

Chapter 13 GUI Basics. Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved.

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

Learn Java Programming, Dr.Hashamdar. Getting Started with GUI Programming

Java Swing. based on slides by: Walter Milner. Java Swing Walter Milner 2005: Slide 1

Lecture 18 Java Graphics and GUIs

Sri Vidya College of Engineering & Technology

2110: GUIS: Graphical User Interfaces

Chapter 12 GUI Basics. Motivations. The design of the API for Java GUI programming

Graphical User Interfaces (GUIs)

Programming graphics

Graphics programming. COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson)

Window Interfaces Using Swing Objects

Chapter 12 Advanced GUIs and Graphics

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

Lab 4. D0010E Object-Oriented Programming and Design. Today s lecture. GUI programming in

INTRODUCTION TO (GUIS)

Graphical User Interfaces. Comp 152

Java. GUI building with the AWT

Unit 6: Graphical User Interface

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

Graphical User Interface (GUI)

CSE 331 Software Design & Implementation

CHAPTER 2. Java Overview

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

GUI Programming. Chapter. A Fresh Graduate s Guide to Software Development Tools and Technologies

Basicsof. JavaGUI and SWING

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

GUI Design. Overview of Part 1 of the Course. Overview of Java GUI Programming

Widgets. Overview. Widget. Widgets Widget toolkits Lightweight vs. heavyweight widgets Swing Widget Demo

Summary Chapter 25 GUI Components: Part 2

Lecture 5: Java Graphics

Chapter 6: Graphical User Interfaces

Course Status Networking GUI Wrap-up. CS Java. Introduction to Java. Andy Mroczkowski

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

BASICS OF GRAPHICAL APPS

Swing from A to Z Some Simple Components. Preface

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Graphical User Interfaces. Swing. Jose Jesus García Rueda

Containers and Components

Top-Level Containers

Introduction. Introduction

JBuilder 8.0 JFC and Swing Programming

Contents Introduction 1

Announcements. Introduction. Lecture 18 Java Graphics and GUIs. Announcements. CSE 331 Software Design and Implementation

Part I: Learn Common Graphics Components

Graphical User Interface (GUI)

DM503 Programming B. Peter Schneider-Kamp.

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

MODEL UPDATES MANIPULATES USER

Dr. Hikmat A. M. AbdelJaber

GUI and its COmponent Textfield, Button & Label. By Iqtidar Ali

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Review. Designing Interactive Systems II. Introduction. Web 2.0 in keywords GWT Cappuccino HTML5. Cross platform GUI Toolkit

Designing Interactive Systems II

Computer Science 210: Data Structures. Intro to Java Graphics

Chapter 13. Applets and HTML. HTML Applets. Chapter 13 Java: an Introduction to Computer Science & Programming - Walter Savitch 1

What is Widget Layout? Laying Out Components. Resizing a Window. Hierarchical Widget Layout. Interior Design for GUIs

Graphic User Interfaces. - GUI concepts - Swing - AWT

Week Chapter Assignment SD Technology Standards. 1,2, Review Knowledge Check JP3.1. Program 5.1. Program 5.1. Program 5.2. Program 5.2. Program 5.

Graphical User Interface (GUI)

KF5008 Program Design & Development. Lecture 1 Usability GUI Design and Implementation

Datenbank-Praktikum. Universität zu Lübeck Sommersemester 2006 Lecture: Swing. Ho Ngoc Duc 1

SD Module-1 Advanced JAVA

The Java Programming Language Basics. Identifiers, Keywords, and Types. Expressions and Flow Control. Object-Oriented Programming. Objects and Classes

SD Module-1 Advanced JAVA. Assignment No. 4

Swing from A to Z Using Focus in Swing, Part 2. Preface

Programming Mobile Devices J2SE GUI

1.00 Lecture 14. Lecture Preview

Class 14: Introduction to the Swing Toolkit

Widgets. Widgets Widget Toolkits. User Interface Widget

TTTK Program Design and Problem Solving Tutorial 3 (GUI & Event Handlings)

Graphical interfaces & event-driven programming

Transcription:

Java Graphical User Interfaces 1 The Abstract Windowing Toolkit (AWT) Since Java was first released, its user interface facilities have been a significant weakness The Abstract Windowing Toolkit (AWT) was part of the JDK from 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, introduced a new event model It did not make any major additions to the basic components 2 1

Part of The AWT Hierarchy Object Component MenuComponent Container Canvas Button Choice TextComponent Window Panel ScrollPane TextField TextArea Frame Applet 3 Java Foundation Classes (JFC) In April 1997, JavaSoft announced the Java Foundation Classes (JFC) A major part of the JFC was a new set of user interface components called Swing AWT Swing Accessibility Java 2D Drag And Drop 4 2

Swing The Swing classes are used to build graphic user interfaces. Swing does not stand for anything Swing is built on top of the core 1.1 and 1.2 AWT libraries Swing improves AWT on three major issues It (usually) does not rely on the platform s native components. It supports Pluggable Look-and-Feel or PLAF It is based on the Model-View-Controller (MVC) design pattern JFC AWT Swing JDK 1.2 5 Why is Swing so Hot? It uses lightweight components It uses a variant of the Model View Controller Architecture (MVC) It has Pluggable Look And Feel (PLAF) It uses the Delegation Event Model 6 3

Weighting Components Sun makes a distinction between lightweight and 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 7 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 JWindow 8 4

Model View Controller Independent elements: Model state data for each component different data for different models View how the component looks onscreen Controller dictates how the component reacts to events 9 MVC Communication Model passes data to view for rendering View determines which events are passed to controller Controller updates model based on events received http://java.sun.com/products/jfc/tsc/articles/architecture/images/mvc_lg.gif 10 5

MVC Example Model: Minimum=0 Maximum=100 Value=0 View: Controller: -accept mouse click on end buttons -accept mouse drag on thumb 11 MVC in Java Swing uses the model-delegate design, a similar architecture to MVC The View and Controller elements are combined into the UI delegate since Java handles most events in the AWT anyway Multiple views can be used with a single model Changes to a single Model can affect different views Components can share a model (JScrollbar and JSlider share the BoundedRangeModel) http://java.sun.com/products/jfc/tsc/articles/component_gallery/index.html 12 6

MVC in Java Model View Controller Component UI-delegate 13 MVC in Java (2) Most components provide the model-defined API directly in the component class. The component can be manipulated without interacting with the model at all //example of method in JSlider class public int getvalue() return getmodel().getvalue(); //so we can use the following and avoid the model JSlider slider = new JSlider(); int value = slider.getvalue(); 14 7

PLAF Features in Swing Default Metal style Can emulate Motif, and Windows styles Supports Mac style through download New styles can be designed Can be reset at runtime 15 PLAF examples http://java.sun.com/products/jfc/tsc/articles/jlf-design/index.html 16 8

PLAF Structure All components have an abstract UI delegate in the swing.plaf package (Jbutton -ButtonUI) UI delegate is accessed by get/setui() method Each Look and Feel has a concrete class for each abstract UI delegate (WindowsButtonUI) communicate through UIManager class get/setlookandfeel() 17 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... 18 9

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 class An abstract class that extends Component. Classes derived from Container -most notably Panel, Applet, Window, Dialog, Frame- can contain multiple components 19 Inheritance Hierarchies for Components 20 10

Additional Swing Features Using Swing: A wide variety of components can be created (tables, trees, sliders, progress bars, internal frame, ) Components can have tooltips placed over them Arbitrary keyboard events can be bound to components There is additional debugging support There is support for parsing and displaying HTML-based information 21 Applets vs. Applications Using Swing it is possible to create two different types of GUI programs Standalone applications Programs 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 22 11

Running Applets <APPLET code= HelloWorld.class HelloWorld.class width=300 height=200> <PARAM name= username username value= Fred Fred > <PARAM name= age age value= 34 34 > </APPLET> We name this file as Filename.html We run it buy appletviewer command An applet should always contains an init method import java.applet.applet; import java.awt.*; public class MyApplet extends Applet public void init () add (new Label ( This ( is a read-only string )); 23 JFrame A JFrame is a Window with all of the adornments added. A JFrame provides the basic building block for screen-oriented applications To show or hide a JFrame, setvisible() is used. Moreover, in previous to 1.5 versions, show() can also be used. JFrame win = new JFrame( title ); 24 12

Creating a JFrame import javax.swing.*; public class SwingFrame public static void main (String[] args) JFrame win = new JFrame("My First GUI Program"); win.setvisible(true); 25 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 (i.e. the Container that corresponds to the Frame) Note that pack() does not look at the title bar setbounds() or setsize() together with setlocation() can be used 26 13

Creating a JFrame import javax.swing.*; public class SwingFrame public static void main (String[] args) JFrame win = new JFrame("My First GUI Program"); win.setsize(250, 150); win.setvisible(true); 27 JFrame A JFrame contains a JRootPane as its only child JLayeredPane glasspane JMenuBar contentpane A JRootpane is made up of a glasspane, an optional JMenuBar, and a contentpane 28 14

JFrame The contentpane provided by the root pane should, as a rule, contain all the non-menu components displayed by the JFrame The JLayeredPane manages the menubar and the contentpane The glasspane sits over the top of everything, where it is in a position to intercept mouse movements Although the menubar component is optional, the JLayeredPane, contentpane, and glasspane always exist To add components to the JFrame (other than the optional menu bar), you add the object to the contentpane of the JRootPane, like this: jframe.getcontentpane().add(child); The JMenuBar is specified with: jframe.setjmenubar(menubar); 29 JFrame 30 15

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 31 JLabel JLabels are components where 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 label s text Initial value Alignment JLabel label = new JLabel( text", JLabel.RIGHT); Like the majority of JComponents, JLabel has more than one constructors 32 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.setvisible(true); 33 JButton JButton extends JComponent, displays a string and delivers an ActionEvent for each mouse click Normally buttons are displayed with a border In addition to text, JButtons can also display icons Initial value JButton button = new JButton( text ); 34 17

JButton 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.setvisible(true); 35 Layout Manager LayoutManager 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 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 36 18

Layout Managers Layouts allow you to format components on the screen in a platform independent way The standard JDK provides several classes that implement the LayoutManager interface. Some of them are: BorderLayout CardLayout FlowLayout GridBagLayout GridLayout Layout managers are defined in the AWT package 37 Changing the Layout To change the layout used in a container you first need to create the container Then the setlayout() method is invoked on the container The layout manager should be established before any components are added to the container JPanel p = new JPanel() ; p.setlayout(new FlowLayout()); 38 19

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 39 FlowLayout import javax.swing.*; import java.awt.*; 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.setvisible(true); 40 20

FlowLayout 41 GridLayout The GridLayout is used for arranging components in rows and columns If the number of rows is specified, the number of columns will be set to the number of components divided by the rows If the number of columns is specified, the number of rows will be set to 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 The first component is placed at (0,0), the second at (0,1),. The underlying components are resized to fill the row-column area if possible 42 21

GridLayout gridlayout(4, 4) gridlayout(2, 4) gridlayout(0, 4) gridlayout(10, 10) 43 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 are added is not important The center area will always be resized to be as large as possible 44 22

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("center", new JButton("Center")); win.pack(); win.setvisible(true); 45 BorderLayout 46 23

GridBagLayout import java.awt.*; import javax.swing.*; import java.util.*; public class GridBagEx1 extends JApplet protected void makebutton(string name, GridBagLayout gridbag, GridBagConstraints c) JButton button = new JButton(name); gridbag.setconstraints(button, c); this.getcontentpane().add(button); 47 GridBagLayout public void init() GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); this.getcontentpane().setlayout(gridbag); c.fill = GridBagConstraints.BOTH; c.weightx = 1.0; this.makebutton("button1", gridbag, c); this.makebutton("button2", gridbag, c); this.makebutton("button3", gridbag, c); //end row c.gridwidth = GridBagConstraints.REMAINDER; this.makebutton("button4", gridbag, c); 48 24

GridBagLayout c.weightx = 0.0; //reset to the default this.makebutton("button5",gridbag,c); //another row c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last in row this.makebutton("button6", gridbag, c); c.gridwidth=gridbagconstraints.remainder; //end row this.makebutton("button7", gridbag, c); c.gridwidth = 1; c.gridheight = 2; c.weighty = 1.0; //reset to the default 49 GridBagLayout this.makebutton("button8", gridbag, c); c.weighty = 0.0; //reset to the default c.gridwidth = GridBagConstraints.REMAINDER; //end row c.gridheight = 1; //reset to the default this.makebutton("button9", gridbag, c); this.makebutton("button10", gridbag, c); this.setsize(300, 100); 50 25

GridBagLayout public static void main(string args[]) JFrame f = new JFrame("GridBag Layout Example"); GridBagEx1 ex1 = new GridBagEx1(); ex1.init(); f.getcontentpane().add("center", ex1); f.pack(); f.setsize(f.getpreferredsize()); f.setvisible(true); 51 GridBagLayout 52 26

Containers A JFrame is not the only type of container that you can use in Swing Some subclasses of Container are: Panel ( JPanel) Applet ( JApplet) Window ( JWindow) Dialog ( JDialog) Frame ( JFrame) 53 A Simple 4 Function Calculator 54 27

Swing Components JFrame with BorderLayout JButton JLabel JPanel with GridLayout 55 CalcGui.java import javax.swing.*; import java.awt.*; import java.awt.event.*; 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 56 28

CalcGui.java public CalcGui(String name) // A Frame for the calculator JFrame win = new JFrame(name); // 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 ); 57 CalcGui.java // Create the display display = new JLabel("0", JLabel.RIGHT) // "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.setvisible(true); 58 29

Java Gui Builders Java Frame Builder JVider (with plugin for eclipse) Matisse (for NetBeans Developers) Jigloo (with plugin for eclipse) 59 Java Gui Code Samples & Tutorials http://examples.oreilly.com/jswing2/code/ http://www.codeguru.com/java/swing/index.shtml http://javafaq.nu/java/free-swing-book/index.shtml http://www.tutorialized.com/tutorials/java/swing/1 60 30