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

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

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

Introduction to the JAVA UI classes Advanced HCI IAT351

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

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

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

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

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

CS 251 Intermediate Programming GUIs: Components and Layout

Graphic User Interfaces. - GUI concepts - Swing - AWT

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

Java Programming Lecture 6

Laying Out Components. What is Widget Layout?

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

Chapter 12 Advanced GUIs and Graphics

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

Window Interfaces Using Swing Objects

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

Dr. Hikmat A. M. AbdelJaber

Ingineria Sistemelor de Programare

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!

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

2110: GUIS: Graphical User Interfaces

Graphical User Interfaces (GUIs)

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

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

Lecture 5: Java Graphics

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

JAVA NOTES GRAPHICAL USER INTERFACES

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

JComponent. JPanel. JFrame. JFrame JDialog, JOptionPane. JPanel. JPanel

Window Interfaces Using Swing Objects

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing

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

Building Java Programs Bonus Slides

Introduction. Introduction

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

Systems Programming Graphical User Interfaces

Lecture 18 Java Graphics and GUIs

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

17 GUI API: Container 18 Hello world with a GUI 19 GUI API: JLabel 20 GUI API: Container: add() 21 Hello world with a GUI 22 GUI API: JFrame: setdefau

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

Unit 6: Graphical User Interface

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

Packages: Putting Classes Together

Containers and Components

DM503 Programming B. Peter Schneider-Kamp.

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

Trees CSCI 201 Principles of Software Development

Basicsof. JavaGUI and SWING

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

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

Java. GUI building with the AWT

JAVA NOTES GRAPHICAL USER INTERFACES

Basics of programming 3. Java GUI and SWING

Java IDE Programming-I

CSE 331 Software Design & Implementation

Graphical User Interfaces in Java

CSC 1214: Object-Oriented Programming

User interfaces and Swing

Tool Kits, Swing. Overview. SMD158 Interactive Systems Spring Tool Kits in the Abstract. An overview of Swing/AWT

1.1 GUI. JFrame. import java.awt.*; import javax.swing.*; public class XXX extends JFrame { public XXX() { // XXX. init() main() public static

Lecture 9. Lecture

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

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

Graphical User Interfaces. Comp 152

Object-Oriented Programming Design. Topic : User Interface Components with Swing GUI Part III

Resizing a Window. COSC 3461: Module 5B. What is Widget Layout? Size State Transitions. What is Widget Layout? Hierarchical Widget Layout.

Administrivia. CSSS Movie Night: Zombieland & Iron Man Date: Thurs., Mar 11 Time: 6 10 pm Location: DMP 310 Free pop & popcorn for every attendee!

Agenda. Container and Component

Java Graphical User Interfaces

Swing. By Iqtidar Ali

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

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

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

Java: Graphical User Interfaces (GUI)

Top-Level Containers

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

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

1.00 Lecture 14. Lecture Preview

Chapter 7: A First Look at GUI Applications

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

Class 14: Introduction to the Swing Toolkit

Sri Vidya College of Engineering & Technology

Java Swing. Recitation 11/(20,21)/2008. CS 180 Department of Computer Science, Purdue University

This exam is closed textbook(s) and closed notes. Use of any electronic device (e.g., for computing and/or communicating) is NOT permitted.

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.

JFrame In Swing, a JFrame is similar to a window in your operating system

Graphical interfaces & event-driven programming

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

CONTENTS. Chapter 1 Getting Started with Java SE 6 1. Chapter 2 Exploring Variables, Data Types, Operators and Arrays 13

12/22/11. Copyright by Pearson Education, Inc. All Rights Reserved.

Graphical User Interface (GUI)

Išdėstymai. Lekt. dr. Pijus Kasparaitis m. m. pavasario semestras.

INTRODUCTION TO (GUIS)

Graphical User Interface (GUI)

Graphical User Interfaces in Java - SWING

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

Transcription:

Course Name: Advanced Java

Lecture 19 Topics to be covered Java Swing Lists Trees Tables Styled Text Components Progress Indicators Component Organizers

AWT to Swing AWT: Abstract Windowing Toolkit import java.awt.* Swing: new with Java2 import javax.swing.* Extends AWT Tons o new improved components Standard dialog boxes Look-and-feel Event listeners

Top Level Containers: JFrame javax.swing.jframe: Top-level window with a title and a border. Usually used as a program's main window

Internal Containers Not Top level containers Can contain other non-top level components Examples: JScrollPane: Provides a scrollable view of its components JSplitPane: Separates two components JTabbedPane: User chooses which component to see

Containers - Layout Each container has a layout manager Determines the size, location of contained widgets. Setting the current layout of a container: void setlayout(layoutmanager lm) LayoutManager implementing BorderLayout BoxLayout FlowLayout GridLayout classes:

FlowLayout Components flow onto form left-toright and top-to-bottom Components take on normal size

The Code Container contentpane = getcontentpane(); contentpane.setlayout(new FlowLayout()); contentpane.add(new JButton("Button 1")); contentpane.add(new JButton("2")); contentpane.add(new JButton("Button 3")); contentpane.add(new JButton("LongNamed Button 4")); contentpane.add(new JButton("Button 5"));

The FlowLayout API Three constructors: public FlowLayout() public FlowLayout(int alignment) public FlowLayout(int alignment, verticalgap) int horizontalgap, int The alignment argument must have one of the values : FlowLayout.LEFT, FlowLayout.CENTER, FlowLayout.RIGHT. horizontalgap and verticalgap specify the number of pixels to put between components. default gap value = 5 pixels. Properties: Alignment, Hgap, Vgap: int, RW,

BorderLayout Container divided into five regions: West, North, East, South, Center.

Example public class BorderLayout1 extends JApplet { public void init() { Container cp = getcontentpane(); cp.setlayout(new BorderLayout()); // default is FlowLayout cp.add(new JButton("North"), BorderLayout.NORTH); // cp.add(borderlayout.north, new JButton("North")); // also ok! // cp.add(new JButton("North"), North ); // also ok! cp.add(borderlayout.south, new JButton("South")); cp.add(borderlayout.east, new JButton("East")); cp.add(borderlayout.west, new JButton("West")); cp.add(borderlayout.center, new JButton("Center")); }} Default for most things

Containers - Layout

Swing Components

Swing Components

GUI Component API Java: GUI component = class Properties Methods Events JButton

Using a GUI Component 1. Create it 2. Configure it 3. Properties: b.text = press me ; [avoided in java] Methods: b.settext( press me ); Add it 4. Instantiate object: b = new JButton( press me ); panel.add(b); Listen to it Events: Listeners JButton

Anatomy of an Application GUI GUI Internal structure JFrame JFrame JPanel containers JPanel JButton JButton JLabel JLabel

Build from bottom up Create: Frame Panel Components Listeners Add: (bottom up) Listener JLabel JPanel listeners into components components into panel panel into frame JFrame JButton

Code JFrame f = new JFrame( title ); JPanel p = new JPanel( ); JButton b = new JButton( press me ); p.add(b); panel f.setcontentpane(p); frame // add button to // add panel to press me f.show();

Application Code import javax.swing.*; class hello { public static void main(string[] args){ JFrame f = new JFrame( title ); JPanel p = new JPanel(); JButton b = new JButton( press me ); p.add(b); panel f.setcontentpane(p); frame f.show(); } } // add button to // add panel to press me

Layout Managers Automatically control placement of components in a panel Why?

Layout Manager Heuristics null FlowLayout none, programmer sets x,y,w,h Left to right, Top to bottom BorderLayout n w c s e

Combinations JButton JButton JTextArea

Combinations JButton JButton JFrame n JPanel: FlowLayout JPanel: BorderLayout c JTextArea

Code: null layout JFrame f = new JFrame( title ); JPanel p = new JPanel( ); JButton b = new JButton( press me ); b.setbounds(new Rectangle(10,10, 100,50)); p.setlayout(null); // x,y layout p.add(b); press me f.setcontentpane(p);

Code: FlowLayout JFrame f = JPanel p = FlowLayout JButton b1 JButton b2 new JFrame( title ); new JPanel( ); L = new FlowLayout( ); = new JButton( press me ); = new JButton( then me ); p.setlayout(l); p.add(b1); p.add(b2); f.setcontentpane(p); press me Set layout mgr before adding components then me

Applets JApplet JApplet is like a JFrame Already has a panel Access panel with JApplet.getContentPane( ) contentpane import javax.swing.*; class hello extends JApplet { JButton public void init(){ JButton b = new JButton( press me ); getcontentpane().add(b); } }

Applet Methods Called by browser: init( ) - initialization start( ) - resume processing (e.g. animations) stop( ) - pause destroy( ) - cleanup paint( ) - redraw stuff ( expose event)

How to Use Scroll Panes textarea = new JTextArea(5, 30); JScrollPane scrollpane = new JScrollPane(textArea);... contentpane.setpreferredsize(new Dimension(400, 100));... contentpane.add(scrollpane, BorderLayout.CENTER);

How to Use Lists A JList presents the user with a group of items, displayed in one or more columns, to choose from. Lists can have many items, so they are often put in scroll panes.

// Create the list of images and put it in a scroll pane JList list = new JList(); list.setselectionmode(listselectionmodel.sin GLE_SELECTION);... JScrollPane listscrollpane = new JScrollPane(list); possible selection modes: SINGLE_SELECTION SINGLE_INTERVAL_SELECTION MULTIPLE_INTERVAL_SELECTION

Creating a Model There are three ways to create a list model: DefaultListModel Take care of few things. AbstractListModel you manage the data and invoke the "fire" methods. For this approach, you must subclass AbstractListModel and implement the getsize and getelementat methods inherited from the ListModel interface. ListModel you manage everything.

Adding Items to and Removing Items from a List Here is the code that creates a list model object, puts the initial items in it, and uses the list model to create a list: ListModel listmodel = new DefaultListModel(); listmodel.addelement("alison Huml"); listmodel.addelement("kathy Walrath"); listmodel.addelement("lisa Friendly"); listmodel.addelement("mary Campione"); list = new JList(listModel);

Event Handling JList fires list selection events whenever the selection changes. You can process these events by adding a list selection listener to the list with the addlistselectionlistener method. A list selection listener must implement one method: valuechanged

Trees The simplest and most common way to use JTree is to create objects of type DefaultMutableTreeNode to act as the nodes of the tree. The MutableTreeNode interface extends TreeNode. The DefaultMutableTreeNode class implements the MutableTreeNode. Nodes that have no children will be displayed as leaves. The tostring method returns the selected node.

Once you have some nodes, you hook them together in a tree structure via parentnode.add(childnode). add() is a method of DefaultMutableTreeNode. Finally, you pass the node to the JTree constructor. Note that, since trees can change size based upon user input (expanding and collapsing nodes), trees are usually placed inside a JScrollPane.

For example, here is a very simple tree DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root"); DefaultMutableTreeNode child1 = new DefaultMutableTreeNode("Child 1"); root.add(child1); DefaultMutableTreeNode child2 = new DefaultMutableTreeNode("Child 2"); root.add(child2); JTree tree = new JTree(root); somewindow.add(new JScrollPane(tree));

Tables A table is a component that displays rows and columns of data. Table is implemented by JTable class.its constructor is: JTable(Object data[][],object colheads[]) Here data is a 2-dimensional array of information to be presented and colheads is 1-dimensional array of column headings.

Steps for using table in an Applet Create JApplet object. Create JScrollPane object. Add table to scroll pane Add scroll pane to the content pane of applet.

Split Pane

the code //Create a split pane with the two scroll panes in it. splitpane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, listscrollpane, picturescrollpane); splitpane.setonetouchexpandable(true); splitpane.setdividerlocation(150); //Provide minimum sizes for the two components in the split pane Dimension minimumsize = new Dimension(100, 50); listscrollpane.setminimumsize(minimumsize); picturescrollpane.setminimumsize(minimumsize );

Formatted Text Fields Formatted text fields provide a way for developers to specify the valid set of characters that can be typed in a text field. Specifically, the JFormattedTextField class adds a formatter and an object value to the features inherited from the JTextField class. The formatter translates the field's value into the text it displays, and the text into the field's value.

Using the formatters that Swing provides, you can set up formatted text fields to type dates and numbers in localized formats. Another kind of formatter enables you to use a character mask to specify the set of characters that can be typed at each position in the field. For example, you can specify a mask for typing phone numbers in a particular format, such as (XX) X-XX-XX-XX-XX. Using the formatters that Swing provides, you can set up formatted text fields to type dates and numbers in localized formats. Another kind of formatter enables you to use a character mask to specify the set of characters that can be typed at each position in the field. For example, you can specify a mask for typing phone numbers in a particular format, such as (XX) X-XX-XX-XX-XX.