Swing/GUI Cheat Sheet

Similar documents
To gain experience using GUI components and listeners.

Part I: Learn Common Graphics Components

CS111: PROGRAMMING LANGUAGE II

Graphical User Interfaces. Comp 152

Adding Buttons to StyleOptions.java

What Is an Event? Some event handler. ActionEvent. actionperformed(actionevent e) { }

EVENTS, EVENT SOURCES AND LISTENERS

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

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

Introduction. Introduction

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

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!

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

Chapter 18. Advanced graphics programming

Graphical User Interfaces in Java - SWING

Chapter 6: Graphical User Interfaces

Welcome to CIS 068! 1. GUIs: JAVA Swing 2. (Streams and Files we ll not cover this in this semester, just a review) CIS 068

Programming graphics

Chapter 12 Advanced GUIs and Graphics

AP CS Unit 11: Graphics and Events

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

Class 16: The Swing Event Model

Window Interfaces Using Swing. Chapter 12

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

CS 251 Intermediate Programming GUIs: Components and Layout

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

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

Agenda. Container and Component

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

Java Event Handling -- 1

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

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

Chapter 6 Using Objects

Summary. Section 14.1 Introduction. Section 14.2 Java s New Nimbus Look-and-Feel. 618 Chapter 14 GUI Components: Part 1

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

CS 201 Advanced Object-Oriented Programming Lab 5 - Sudoku, Part 1 Due: March 3/4, 11:30 PM

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

GUI in Java TalentHome Solutions

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

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Object Oriented Programming

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

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

An array is a type of variable that is able to hold more than one piece of information under a single variable name.

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

COURSE DESCRIPTION. John Lewis and William Loftus; Java: Software Solutions; Addison Wesley

Summary Chapter 25 GUI Components: Part 2

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

Name: Checked: Learn about listeners, events, and simple animation for interactive graphical user interfaces.

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

Graphic User Interfaces. - GUI concepts - Swing - AWT

Window Interfaces Using Swing Objects

DM503 Programming B. Peter Schneider-Kamp.

Chapter 7: A First Look at GUI Applications

Introduction to the JAVA UI classes Advanced HCI IAT351

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

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

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

Systems Programming Graphical User Interfaces

Java Programming Lecture 6

Java, Swing, and Eclipse: The Calculator Lab.

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

Information Technology for Industrial Engineers 15 November ISE 582: Information Technology for Industrial Engineers

Chapter 12 GUI Basics

CS 209 Spring, 2006 Lab 8: GUI Development Instructor: J.G. Neal

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

import javax.swing.jframe; import javax.swing.joptionpane;

SD Module-1 Advanced JAVA

MIT AITI Swing Event Model Lecture 17

Unit 7: Event driven programming

SD Module-1 Advanced JAVA. Assignment No. 4

Graphical Applications

CS 180 Fall 2006 Exam II

Window Interfaces Using Swing Objects

First Name: AITI 2004: Exam 2 July 19, 2004

11/27/2007 TOPICS CHAPTER TOPICS LISTS READ ONLY TEXT FIELDS. Advanced GUI Applications. This module discusses the following main topics:

Dr. Hikmat A. M. AbdelJaber

Points Missed on Page page 1 of 8

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

CS Exam 1 Review Suggestions

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

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

PIC 20A GUI with swing

Together, the appearance and how user interacts with the program are known as the program look and feel.

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

Containers and Components

CHAPTER 2. Java Overview

BASICS OF GRAPHICAL APPS

14.2 Java s New Nimbus Look-and-Feel 551 Sample GUI: The SwingSet3 Demo Application As an example of a GUI, consider Fig. 14.1, which shows the SwingS

First Name: AITI 2004: Exam 2 July 19, 2004

Building Java Programs Bonus Slides

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

Java Graphical User Interfaces

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

Programming Mobile Devices J2SE GUI

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

+! Today. Lecture 3: ArrayList & Standard Java Graphics 1/26/14! n Reading. n Objectives. n Reminders. n Standard Java Graphics (on course webpage)

Top-Level Containers

Computer Science 210: Data Structures. Intro to Java Graphics

Transcription:

General reminders To display a Swing component, you must: Swing/GUI Cheat Sheet Construct and initialize the component. Example: button = new JButton ("ButtonLabel"); Add it to the content pane of the window to a JPanel that is added to the display. Example: getcontentpane().add (button); Impt javax.swing.* and sometimes also java.awt.* at the beginning of the class creating the components. To get events from a GUI component, you must do the following: Declare that the class handling the event implements the appropriate listener interface. Example: implements Define the method that the listener interface requires. Example: public void actionperfmed (ActionEvent event) Add a listener appropriate f the component to the component. Example: button.add (this); Impt java.awt.event.* (and occasionally javax.swing.event.*) at the beginning of the class that is the listener. When the listener method is called, you can find out which component sent the event by calling getsource() on the event. public void actionperfmed (ActionEvent event) { Object thebutton = event.getsource(); if (thebutton == framedcirclebutton) { // Create a framed circle } } If a method returns a String, remember to compare the result using the equals method, not ==: amenu.getselecteditem ().equals ("A value") GUI Components The following methods can be applied to any Component: void setfont (Font f) void setfeground (Col c) void setbackground (Col c) To construct a font use: new Font (String name, int style, int size) Find out the font names on the computer in Eclipse as follows: Open the Window menu Select Preferences www.cs.williams.edu/ /swing-gui.html 1/5

click on the triangle next to Wkbench Select Fonts Select Text Font to the right and then click on the Change... button. Style can be one of Font.BOLD, Font.ITALIC, Font.PLAIN, Font.BOLD + Font.ITALIC. The specific components we have considered: 1. JButton new JButton (String s) Adding the listener: void add ( al) 2. JComboBox Construct and Initialization: new JComboBox ( ) void additem (Object item) To find out which item was selected, use: Object getselecteditem ( ) If you wish to treat the value returned from this method as a String, you may use a type cast: String text = (String) menu.getselecteditem ( ); To find out the index of the item that was selected, use: int getselectedindex ( ) The JComboBox component is unusual in that it can hear about the user making a menu selection by either implementing ItemListener. Be sure to be consistent in your choice of listener interface, method to add the listener, and www.cs.williams.edu/ /swing-gui.html 2/5

listening method. ItemListener Adding the listener: void additemlistener (ItemListener il) void add ( al) void itemstatechanged (ItemEvent e) 3. JLabel new JLabel (String s) new JLabel (String s, int align) align is one of JLabel.RIGHT, JLabel.LEFT, JLabel.CENTER 4. JSlider no listeners available f JLabels new JSlider (int ientation, int minimum, int maximum, int initialvalue) ientation is one of JSlider.HORIZONTAL JSlider.VERTICAL void setvalue (int newval) To find out the current value, use: int getvalue ( ) ChangeListener www.cs.williams.edu/ /swing-gui.html 3/5

ChangeListener Adding the Listener: addchangelistener (ChangeListener al) void statechanged (ChangeEvent e) 5. JTextField new JTextField (String s) To find out the value typed, use: Adding the Listener: add ( al) Containers Both JPanel and the object obtained by sending getcontentpane() to a WindowController object are containers (and have type Container). The following methods are available f all containers. To define the type of layout, use: void setlayout (LayoutManager lm) LayoutManager may be any of the layout managers listed below. To add something to a container: void add (Component c) Component may be any Component (such as JButton, JTextField, JSlider,...) Container (such as JPanel). Use the method above if the container has a FlowLayout GridLayout. Use the one below if it has a BderLayout. void add (Component c, int position) The position may be any of BderLayout.NORTH, BderLayout.SOUTH, BderLayout.EAST, BderLayout.WEST, BderLayout.CENTER. Constructing a JPanel is very straightfward: www.cs.williams.edu/ /swing-gui.html 4/5

new JPanel () Layout Managers 1. BderLayout (Default f WindowController) new BderLayout () 2. FlowLayout (Default f JPanel) new FlowLayout () 3. GridLayout new GridLayout (int rows, int cols) new GridLayout (int rows, int cols, int colspacing, int rowspacing) www.cs.williams.edu/ /swing-gui.html 5/5