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

Similar documents
Introduction to the JAVA UI classes Advanced HCI IAT351

Window Interfaces Using Swing Objects

Graphical User Interface (GUI)

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

Window Interfaces Using Swing Objects

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

Graphical User Interfaces. Comp 152

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!

Graphic User Interfaces. - GUI concepts - Swing - AWT

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

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

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

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

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

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

Java & Graphical User Interface II. Wang Yang wyang AT njnet.edu.cn

Packages: Putting Classes Together

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

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

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

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

Part I: Learn Common Graphics Components

Swing. By Iqtidar Ali

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

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

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

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

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

Event Driven Programming

Graphical User Interface (GUI)

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

AP CS Unit 11: Graphics and Events

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

Goals. Lecture 7 More GUI programming. The application. The application D&D 12. CompSci 230: Semester JFrame subclass: ListOWords

CS 251 Intermediate Programming GUIs: Components and Layout

2110: GUIS: Graphical User Interfaces

CompSci 125 Lecture 17. GUI: Graphics, Check Boxes, Radio Buttons

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

PIC 20A GUI with swing

Systems Programming Graphical User Interfaces

MIT AITI Swing Event Model Lecture 17

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

Programming graphics

Unit 6: Graphical User Interface

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.

COMPSCI 230. Software Design and Construction. Swing

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

Agenda. Container and Component

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver

CSE Lab 8 Assignment Note: This is the last lab for CSE 1341

Java IDE Programming-I

Introduction. Introduction

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

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Programming Languages and Techniques (CIS120)

News and info. Array. Feedback. Lab 4 is due this week. It should be easy to change the size of the game grid.

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

Chapter 17 Creating User Interfaces

DEMYSTIFYING PROGRAMMING: CHAPTER FOUR

Graphical User Interface (GUI)

Containers and Components

Chapter 6: Graphical User Interfaces

Advanced Java Programming. Swing. Introduction to Swing. Swing libraries. Eran Werner, Tel-Aviv University Summer, 2005

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

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

JLayeredPane. Depth Constants in JLayeredPane

Java. GUI building with the AWT

GUI Basics. Object Orientated Programming in Java. Benjamin Kenwright

DEMYSTIFYING PROGRAMMING: CHAPTER SIX METHODS (TOC DETAILED) CHAPTER SIX: METHODS 1

Graphical User Interfaces in Java

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

Interfaces & Polymorphism part 2: Collections, Comparators, and More fun with Java graphics

Programmierpraktikum

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

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

Java: Graphical User Interfaces (GUI)

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

Swing from A to Z Some Simple Components. Preface

Java Swing Introduction

JAVA NOTES GRAPHICAL USER INTERFACES

Chapter 8. Java continued. CS Hugh Anderson s notes. Page number: 264 ALERT. MCQ test next week. This time. This place.

Java continued. Chapter 8 ALERT ALERT. Last week. MCQ test next week. This time. This place. Closed book. Assignment #2 is for groups of 3

Hanley s Survival Guide for Visual Applications with NetBeans 2.0 Last Updated: 5/20/2015 TABLE OF CONTENTS

Table of Contents. Chapter 1 Getting Started with Java SE 7 1. Chapter 2 Exploring Class Members in Java 15. iii. Introduction of Java SE 7...

Java Programming Lecture 6

Introduction to GUIs. Principles of Software Construction: Objects, Design, and Concurrency. Jonathan Aldrich and Charlie Garrod Fall 2014

Graphical interfaces & event-driven programming

Lecture 18 Java Graphics and GUIs

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

COMP-202 Unit 10: Basics of GUI Programming (Non examinable) (Caveat: Dan is not an expert in GUI programming, so don't take this for gospel :) )

Window Interfaces Using Swing. Chapter 12

Swing Programming Example Number 2

Parts of a Contract. Contract Example. Interface as a Contract. Wednesday, January 30, 13. Postcondition. Preconditions.

Lecture 9. Lecture

Graphical User Interfaces

CS111: PROGRAMMING LANGUAGE II

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.

SD Module-1 Advanced JAVA

Basicsof. JavaGUI and SWING

Transcription:

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

What is Swing? A group of 14 packages to do with the UI 451 classes as at 1.4 (!) Part of JFC Java Foundation Classes (compare now defunct MFC) Java Swing Walter Milner 2005: Slide 2

Swing and the AWT AWT = abstract windows toolkit (cross platform) AWT = earliest version of Java GUI eg Frame AWT not JFrame Swing Do not mix AWT and Swing Use Swing Java Swing Walter Milner 2005: Slide 3

createandshowgui private static void createandshowgui() { //Create and set up the window. JFrame frame = new JFrame("Hi.."); frame.setdefaultcloseoperation(jframe.exit_on_close); //Add a label. JLabel label = new JLabel("Hello World"); frame.getcontentpane().add(label); //Display the window. frame.pack(); frame.setvisible(true); } Java Swing Walter Milner 2005: Slide 4

Layout Managers Most Swing UIs utilise a LayoutManager to control positioning of items There is a choice of these which work in different ways Initially we do without one, and position items ourselves: frame.setlayout(null); Java Swing Walter Milner 2005: Slide 5

Absolute positioning JFrame frame = new JFrame("I am a JFrame"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setbounds(20,30,300,100); frame.setlayout(null); JButton butt=new JButton("Click me"); frame.getcontentpane().add(butt); butt.setbounds(20, 20, 200,20); frame.setvisible(true); Java Swing Walter Milner 2005: Slide 6

Some LayoutManagers from Swing tutorial on java.sun.com Java Swing Walter Milner 2005: Slide 7

FlowLayout JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("FlowLayout"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.getcontentpane().setlayout(new FlowLayout()); JButton b1 = new JButton("Hello"); frame.getcontentpane().add(b1); JButton b2 = new JButton("Two"); frame.getcontentpane().add(b2); JTextField t1 = new JTextField("Text here"); frame.getcontentpane().add(t1); frame.pack(); frame.setvisible(true); Try this Try re-sizing the frame at runtime Add more buttons Add frame.setbounds Remove pack(); Java Swing Walter Milner 2005: Slide 8

BorderLayout JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("Border"); frame.setdefaultcloseoperation(jframe.exit_on_close); JButton b1 = new JButton("At the top"); frame.getcontentpane().add(b1,borderlayout.page_start ); JButton b2 = new JButton("Bottom"); frame.getcontentpane().add(b2,borderlayout.page_end); JTextField t1 = new JTextField("Left"); frame.getcontentpane().add(t1,borderlayout.line_start); JTextField t2 = new JTextField("Right"); frame.getcontentpane().add(t2,borderlayout.line_end); JButton b3 = new JButton("Centre"); frame.getcontentpane().add(b3,borderlayout.center ); frame.pack(); Try this frame.setvisible(true); Java Swing Walter Milner 2005: Slide 9

Grid JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("Grid"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.getcontentpane().setlayout(new GridLayout(4,3,5,5)); for (int i=0; i<10; i++) frame.getcontentpane().add(new JButton(""+i)); frame.pack(); frame.setvisible(true); Java Swing Walter Milner 2005: Slide 10

Responding to user actions Based on an event-handling model New component eg a button should have a Listener specified The Listener object is programmed to respond to Event objects coming from the component The Listener object needs to implement the appropriate interface Java Swing Walter Milner 2005: Slide 11

Event-handling Event object when clicked interface eg ActionListener the listener eg JFrame component eg button during initialisation, component selects another object eg a JFrame, to be the listener executes appropriate interface method ie actionperformed Java Swing Walter Milner 2005: Slide 12

Interfaces An interface is a set of methods eg the ActionListener interface has just one method - public void actionperformed(actionevent e) A class can declare that it implements it eg public class Main implements ActionListener Then it must actually define the methods in that interface Or the compiler will complain Classes can implement multiple interfaces Java Swing Walter Milner 2005: Slide 13

Button click demo JButton and JLabel clickcounts remembers the number of clicks Class implements ActionListener Make JFrame, JButton and JLabel Instantiate application object Set to be the listener of the button Java Swing Walter Milner 2005: Slide 14

Which button? If have several buttons, all must link to actionperformed How to know which button was clicked? Use the.getsource method of the ActionEvent object Java Swing Walter Milner 2005: Slide 15

Example which button butt1=new JButton("Button 1");.. butt2 = new JButton("Button 2");.. public void actionperformed(actionevent e) { if (e.getsource()==butt1) label.settext("butt1 clicked"); else label.settext("butt2 clicked"); } Java Swing Walter Milner 2005: Slide 16

Swing has a lot of classes containers things that hold other things eg JFRame controls User I/O widgets eg JButton Java Swing Walter Milner 2005: Slide 17

Containers top level containers - JFrame JApplet JDialog general purpose containers - panel scroll pane split pane tabbed pane tool bar Java Swing Walter Milner 2005: Slide 18

JPanel ( in createandshowgui) JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("I am a JFrame"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setbounds(20,30,300,100); frame.setlayout(null); //Create a panel JPanel mypanel = new JPanel(); mypanel.setbackground(new Color(255,3,25)); mypanel.setopaque(true); //Make it the content pane. frame.setcontentpane(mypanel); frame.setvisible(true); Java Swing Walter Milner 2005: Slide 19

JPanel Is a subclass of JComponent So are all the other Swing components except the top-level containers You can add a border And a tool-tip Java Swing Walter Milner 2005: Slide 20

Tooltip and border.. mypanel.setopaque(true); mypanel.settooltiptext("i'm a JPanel"); mypanel.setborder(borderfactory.createlineborder(color.white)); frame.setcontentpane(mypanel);.. Java Swing Walter Milner 2005: Slide 21

JSplitPane.. setlayout(null); //Create a split pane JSplitPane mypane = new JSplitPane(); mypane.setopaque(true); frame.setcontentpane(mypane); frame.setvisible(true); Java Swing Walter Milner 2005: Slide 22

JSplitPane with JPanels //Create a split pane JSplitPane mypane = new JSplitPane(); mypane.setopaque(true); mypane.setdividerlocation(150); // make two panels JPanel right = new JPanel(); right.setbackground(new Color(255,0,0)); JPanel left = new JPanel(); left.setbackground(new Color(0,255,0)); // set as left and right in split mypane.setrightcomponent(right); mypane.setleftcomponent(left); Java Swing Walter Milner 2005: Slide 23

Exercise Program this The buttons set the colour of the left hand pane Java Swing Walter Milner 2005: Slide 24

JTextField For single-line text input Methods gettext, settext Can use ActionListener, triggered when Enter pressed Java Swing Walter Milner 2005: Slide 25

Example of JTextField See source in Word doc Check Main object fields for label and textfield Make a panel, set as content pane Make and add text field Add actionlistener Make and add a label Program actionperformed Java Swing Walter Milner 2005: Slide 26

JTextArea JPanel mypanel = new JPanel(); app.textarea = new JTextArea("Type here",5, 20); mypanel.add(app.textarea); TextArea expands rows and columns as needed Java Swing Walter Milner 2005: Slide 27

JScrollPane JTextArea textarea = new JTextArea("Type here",5, 20); JScrollPane scrollpane = new JScrollPane(textArea); frame.setcontentpane(scrollpane); Java Swing Walter Milner 2005: Slide 28

Exercise Program this Use the selectall and cut methods of JTextComponent, which JTextArea inherits Java Swing Walter Milner 2005: Slide 29

.. Timer t = new Timer(1000, app); t.start(); app.label = new JLabel("Time"); app.label.setbounds(20,20,200,20); frame.getcontentpane().add(app.label);.. public void actionperformed(actionevent e) { String now = (new java.util.date()).tostring(); label.settext(now); } Timer Java Swing Walter Milner 2005: Slide 30

Images JFrame frame = new JFrame("I am Celsius"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setbounds(20,30,200,200); frame.getcontentpane().setlayout(null); ImageIcon icon = new ImageIcon("c:/celsius.jpg", "Celsius"); JLabel label = new JLabel(icon); label.setbounds(20,20,150,150); frame.getcontentpane().add(label); frame.setvisible(true); Java Swing Walter Milner 2005: Slide 31

JScrollBar See source code JScrollBar and JLabel Constructor arguments implements AdjustmentListener adjustmentvaluechanged e.getvalue() Java Swing Walter Milner 2005: Slide 32

Exercise Program this The scroll bars determine the red, green and blue components of the background of the panel Java Swing Walter Milner 2005: Slide 33

JCheckBox See source code implements ActionListener isselected() Java Swing Walter Milner 2005: Slide 34

Exercise Program this The checkbox determines if the text in the label is left or right aligned Java Swing Walter Milner 2005: Slide 35

RadioButton Come in groups only 1 selected per group See demo code Make radiobuttons Make group Add radiobuttons to group ActionListener Java Swing Walter Milner 2005: Slide 36

RadioButton Exercise Modify the demo by adding more colour options Java Swing Walter Milner 2005: Slide 37

RadioButton group border.. JPanel grouppanel = new JPanel(); grouppanel.setbounds(10,10,100,60); grouppanel.setborder(borderfactory.createlineborder( Color.black)); frame.getcontentpane().add(grouppanel); grouppanel.add(app.choice1); grouppanel.add(app.choice2);.. Java Swing Walter Milner 2005: Slide 38

ListBox See source code Data held in array List box shows array List box inside scroll pane mylist.getmodel().getelementat(.. Java Swing Walter Milner 2005: Slide 39

Two JListBoxes See source code We want to add items to list So use a Vector not an array to hold data Check methods to delete items and copy to other listbox Java Swing Walter Milner 2005: Slide 40

Exercise Add a button to the last example which deletes selected items in the second list box Java Swing Walter Milner 2005: Slide 41

Layout Managers A layout manager controls the positioning of components Components have a 'preferred size' so can avoid sizing them.pack() adjusts size of a container to fit components Java Swing Walter Milner 2005: Slide 42

Some LayoutManagers from Swing tutorial on java.sun.com Java Swing Walter Milner 2005: Slide 43

FlowLayout JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("FlowLayout"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.getcontentpane().setlayout(new FlowLayout()); JButton b1 = new JButton("Hello"); frame.getcontentpane().add(b1); JButton b2 = new JButton("Two"); frame.getcontentpane().add(b2); JTextField t1 = new JTextField("Text here"); frame.getcontentpane().add(t1); frame.pack(); frame.setvisible(true); Try this Try re-sizing the frame at runtime Add more buttons Add frame.setbounds Remove pack(); Java Swing Walter Milner 2005: Slide 44

BorderLayout JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("Border"); frame.setdefaultcloseoperation(jframe.exit_on_close); JButton b1 = new JButton("At the top"); frame.getcontentpane().add(b1,borderlayout.page_start ); JButton b2 = new JButton("Bottom"); frame.getcontentpane().add(b2,borderlayout.page_end); JTextField t1 = new JTextField("Left"); frame.getcontentpane().add(t1,borderlayout.line_start); JTextField t2 = new JTextField("Right"); frame.getcontentpane().add(t2,borderlayout.line_end); JButton b3 = new JButton("Centre"); frame.getcontentpane().add(b3,borderlayout.center ); frame.pack(); Try this frame.setvisible(true); Java Swing Walter Milner 2005: Slide 45

Grid JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("Grid"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.getcontentpane().setlayout(new GridLayout(4,3,5,5)); for (int i=0; i<10; i++) frame.getcontentpane().add(new JButton(""+i)); frame.pack(); frame.setvisible(true); Java Swing Walter Milner 2005: Slide 46