Today. cisc3120-fall2012-parsons-lectiii.3 2

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

Building Graphical User Interfaces. Overview

Graphical User Interfaces. Comp 152

Building Graphical User Interfaces. GUI Principles

Java Programming Lecture 6

Overview. Building Graphical User Interfaces. GUI Principles. AWT and Swing. Constructing GUIs Interface components GUI layout Event handling

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

Laying Out Components. What is Widget Layout?

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

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

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

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

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!

Chapter 13 Lab Advanced GUI Applications Lab Objectives. Introduction. Task #1 Creating a Menu with Submenus

Window Interfaces Using Swing Objects

Window Interfaces Using Swing Objects

Graphic User Interfaces. - GUI concepts - Swing - AWT

Prototyping a Swing Interface with the Netbeans IDE GUI Editor

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

Chapter 13 Lab Advanced GUI Applications

CSSE 220 Day 19. Object-Oriented Design Files & Exceptions. Check out FilesAndExceptions from SVN

Widgets. Widgets Widget Toolkits. User Interface Widget

CSSE 220 Day 19. Object-Oriented Design Files & Exceptions. Check out FilesAndExceptions from SVN

Introduction to the JAVA UI classes Advanced HCI IAT351

BASICS OF GRAPHICAL APPS

Widgets. Widgets Widget Toolkits. 2.3 Widgets 1

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

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

2.6 Error, exception and event handling

What is Widget Layout? COSC 3461 User Interfaces. Hierarchical Widget Layout. Resizing a Window. Module 5 Laying Out Components

Lecture 5: Java Graphics

Unit 6: Graphical User Interface

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

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

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

Java IDE Programming-I

import javax.swing.*; import java.awt.*; import java.awt.event.*;

Chapter 12 Advanced GUIs and Graphics

PIC 20A GUI with swing

Sri Vidya College of Engineering & Technology

CS 251 Intermediate Programming GUIs: Components and Layout

Introduction This assignment will ask that you write a simple graphical user interface (GUI).

Graphical User Interface (GUI)

Java. Error, Exception, and Event Handling. Error, exception and event handling. Error and exception handling in Java

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

CS 3 Introduction to Software Engineering. 3: Exceptions

Event Driven Programming

Programming graphics

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

Implementing Graphical User Interfaces

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

Layout. Dynamic layout, Swing and general layout strategies

CS11 Java. Fall Lecture 3

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

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

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

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

CS 11 java track: lecture 3

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

Programming In Java Prof. Debasis Samanta Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

CS11 Java. Fall Lecture 4

No SVN checkout today. Object-Oriented Design

More Swing. CS180 Recitation 12/(04,05)/08

Lecture 28. Exceptions and Inner Classes. Goals. We are going to talk in more detail about two advanced Java features:

Basicsof. JavaGUI and SWING

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Graphical User Interfaces

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

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

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

Chapter 7: A First Look at GUI Applications

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

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

Graphical User Interfaces (GUIs)

CSE 331 Software Design & Implementation

Chapter 2 First Java Programs

Lecture 3: Java Graphics & Events

Jonathan Aldrich Charlie Garrod

Chapter 6: Graphical User Interfaces

Lecture 18 Java Graphics and GUIs

Class 16: The Swing Event Model

Java. GUI building with the AWT

Programming Languages and Techniques (CIS120)

CSC 161 SPRING 17 LAB 2-1 BORDERLAYOUT, GRIDLAYOUT, AND EVENT HANDLING

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

G51PGP Programming Paradigms. Lecture 009 Concurrency, exceptions

H212 Introduction to Software Systems Honors

Chapter 14. More Swing

Summary Chapter 25 GUI Components: Part 2

CS11 Java. Fall Lecture 4

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

Exceptions. References. Exceptions. Exceptional Conditions. CSE 413, Autumn 2005 Programming Languages

Chapter 14. Exception Handling and Event Handling

SE1021 Exam 2. When returning your exam, place your note-sheet on top. Page 1: This cover. Page 2 (Multiple choice): 10pts

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

PIC 20A Exceptions. Ernest Ryu UCLA Mathematics. Last edited: November 27, 2017

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

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

Exceptions. CSE 142, Summer 2002 Computer Programming 1.

Transcription:

MORE GUI COMPONENTS

Today Last time we looked at some of the components that the: AWT Swing libraries provide for building GUIs in Java. This time we will look at several more GUI component topics. We will also look at: Exception handling a topic that isn t directly related to GUIs, but which we need to cover. cisc3120-fall2012-parsons-lectiii.3 2

BorderTest The next few slides refer to the BorderTest program that you can download from the class website. cisc3120-fall2012-parsons-lectiii.3 3

The BorderLayout class allows five things to be arranged in a container: north south east west on the borders of the container, and in the: center BorderLayout doesn t need to be explicitly named, because it is the default. Note that add() has two parameters. cisc3120-fall2012-parsons-lectiii.3 4

If you add a component to BorderLayout without specifying what position you want it in, it is positioned in the center. cisc3120-fall2012-parsons-lectiii.3 5

HTML labels The labels in buttons have to be strings, but they can also be strings that implement HTML commands. For example: String s1 = "<html><table>" + "<tr><td>one</td><tr>two</td></tr>" + "<tr><td>three</td><tr>four</td></tr>" + "</table></html>" JButton button = new JButton(s1); will display the table in the button. HTML can be used in this way to embed graphics in buttons. cisc3120-fall2012-parsons-lectiii.3 6

Nesting Containers The next few slides refer to the NestContain program that you can download from the class website. cisc3120-fall2012-parsons-lectiii.3 7

Here we want an interface that looks like: Buttons Graphics We can do this by nesting one container in another. cisc3120-fall2012-parsons-lectiii.3 8

We put the buttons in a GridLayout in one container. We then put this container in a second container along with the graphics. We use Borderlayout for ths outer container. Make the container with the buttons the WEST component. Make the graphics the CENTER component. cisc3120-fall2012-parsons-lectiii.3 9

Resize The next few slides refer to the Resize program that you can download from the class website. cisc3120-fall2012-parsons-lectiii.3 10

If you run NestContain you will notice that if you resize the window, the buttons behave as you would like: They change size as the window grows and shrinks. The graphics component, however, does not change in size. This is because we fixed its size with RADIUS. RESIZE shows a trick that allows us to resize graphics. cisc3120-fall2012-parsons-lectiii.3 11

It hinges on the following function: public void setbounds(int x, int y, int width, int height){ radius = Math.min(width, height) / 2; super.setbounds(x, y, width, height); repaint(); } This over-rides the setbounds() function but calls the superclass version. This does not interfere with what setbounds does, but allows us access to its parameters. cisc3120-fall2012-parsons-lectiii.3 12

setbounds() is called by the layout manager to tell a container where it is on the screen and how big it is. x and y are the location within the screen. width and height are the window dimensions. Here we use them to set the size of the star. cisc3120-fall2012-parsons-lectiii.3 13

Menus The main GUI component that we have not yet covered is the menu. The next few slides refer to the SimplePaintMenu program that you can download from the class website. This program was also given out in class. This is an extension of the SimplePaint program from the last lecture. cisc3120-fall2012-parsons-lectiii.3 14

We start by adding a menu bar, which is where menus live: JFrame frame = new JFrame("SimplePaint"); Container pane = frame.getcontentpane(); JMenuBar menubar = new JMenuBar(); frame.setjmenubar(menubar); Note that we add the menu bar to the JFrame, not to the ContentPane. cisc3120-fall2012-parsons-lectiii.3 15

Once we have a menu bar, then we can add menus: JMenu filemenu = new JMenu("File"); JMenu optionsmenu = new JMenu("Options"); menubar.add(filemenu); menubar.add(optionsmenu); And we can add shortcuts, keystrokes that will activate the menu options: filemenu.setmnemonic( F ); optionsmenu.setmnemonic( O ); (To use these, hold down the Alt key while pressing the relevant letter). cisc3120-fall2012-parsons-lectiii.3 16

We can then add items to the menus. Here we add an itemn to the File menu: JMenuItem exit = new JMenuItem("Exit", x ); filemenu.add(exit); exit.addactionlistener(new GoodBye()); The x is the keyborad shortcut here. The action listener will be called when the menu item is selected. cisc3120-fall2012-parsons-lectiii.3 17

In the above example, the connection between listener and menu is simple there is one listener for one menu item. Here is a more complex example from SimplePaintMenu JMenu penadjustmenu = new JMenu("Pen Size"); penadjustmenu.setmnemonic( P ); JMenuItem smallpen = new JMenuItem("Small", S ); penadjustmenu.add(smallpen); JMenuItem mediumpen = new JMenuItem("Medium", M ); penadjustmenu.add(mediumpen); JMenuItem largepen = new JMenuItem("Large", L ); penadjustmenu.add(largepen); optionsmenu.add(penadjustmenu); cisc3120-fall2012-parsons-lectiii.3 18

We have one listener for all these menu items: PenAdjuster penadjust = new PenAdjuster(listener); smallpen.addactionlistener(penadjust); mediumpen.addactionlistener(penadjust); largepen.addactionlistener(penadjust); cisc3120-fall2012-parsons-lectiii.3 19

In the listener we define: public void actionperformed(actionevent e){ painter.setpensize(e.getactioncommand()); } The getactioncommand() returns the string in the menu item so that we can write: public void setpensize(string size){ if (size.equals("small")){ radius = 0; diameter = 1; } cisc3120-fall2012-parsons-lectiii.3 20

Other GUI items Two useful components are check boxes and radio buttons. Radio buttons need to be grouped. Here s an example: Given: JPanel mypanel = new JPanel(); we can add a button to the panel using: ButtonGroup mygroup = new ButtonGroup(); JRadioButton radiobutton = new JRadioButton("Java"); radiobutton.setactioncommand("java"); mypanel.add(radiobutton); mygroup.add(radiobutton); And we would repeat for all the other radio buttons we want grouped together. cisc3120-fall2012-parsons-lectiii.3 21

In the listener we then ask the ButtonGroup what the actioncommand of the selected button is: string lang = mygroup.getselection().getactioncommand(); Note that in this case we are assuming that the listener is an object inside the one that defines the interface, allowing direct access to mygroup. cisc3120-fall2012-parsons-lectiii.3 22

Check boxes are easier to set up since we don t have an object to group them, though we will typcially handle them by adding them to their own panel: JPanel mypanel2 = new JPanel(); mypanel2.add(new JCheckBox("Java")); and we repeat the second part for other check boxes that we want to group together. This panel is then added to a container that is part of the interface. cisc3120-fall2012-parsons-lectiii.3 23

Since we can have several check boxes selected, we need to handle them something like this in the listener: Component[] components = mypanel2.getcomponents(); for(component c : components){ JCheckBox cb = (JCheckBox) c; if(cb.isselected()){ <whatever action we want> } Again we assume that the listener has direct access to the variables in the interface. Note that if mypanel2 contained other kinds of component, we would have to pick out the check boxes before the if construct, for example by using instanceof. cisc3120-fall2012-parsons-lectiii.3 24

Exception Handling The next few slides refer to the TextInputWExceptH program that you can download from the class website. This is an extension of the TextInput program from the last lecture. cisc3120-fall2012-parsons-lectiii.3 25

Exception handling is one of the nice features of Java. Provides an elegant way to handle runtime problems with programs. Allows programs to start running even when the compiler knows there are problems. It is a legacy of Java s origins in embedded systems. Now exploited in Android cisc3120-fall2012-parsons-lectiii.3 26

Exceptions are handled using: try catch finally Also we may use: throw when we want to generate exceptions. cisc3120-fall2012-parsons-lectiii.3 27

When we expect code to generate exceptions that we will handle, we wrap the code in a try construct. try{ } // This will run normally, except when there // is an exception. cisc3120-fall2012-parsons-lectiii.3 28

To handle an exception, we add a catch to the try. try{ } catch(someexception e1){ } // If the try generates an exception, Java will // try to match it against SomeException. // // If there is a match, code here will be excuted. // // That code can refer to the exception as e1. cisc3120-fall2012-parsons-lectiii.3 29

We can have multiple catch constructs for any try. It is typical to have different catches to handle the different exceptions that might arise. If an execption in a given try does not match a following catch, the interpreter looks for an enclosing try and its corresponding catches. If these don t exist, or don t match the exception, it tries the calling method. This process will eventually (if nothing catches the exception) percolate up to main(). If this doesn t catch the exception, the interpreter prints a stack trace on the console and exits. (You will likely have seen this already.) cisc3120-fall2012-parsons-lectiii.3 30

After the last catch, we may have a finally. The code in a finally construct is always called after the try is executed. This happens whether or not there is an exception, and whether or not the exception is handled. The only time finally does not run is if try calls System.exit(). finally is useful for doing housekeeping things. cisc3120-fall2012-parsons-lectiii.3 31

Note that all exceptions are (of course) objects. The base class for exceptions is java.lang.throwable Throwable has two subclasses: Error Exception It is rare to want to try to handle Errors. They are usually fatal and best left alone. cisc3120-fall2012-parsons-lectiii.3 32

Sometimes we want to generate our own exception. That is we want to force one to happen. This allows us to use exception handling as a way of doing error checking. if (d2 < 0){ throw new IllegalArgumentException(); } The above example is from TextInputWExceptH2. cisc3120-fall2012-parsons-lectiii.3 33

The exception that is thrown must be a bona fide exception object. But, of course, we can create our own exception (extending an existing exception) if we need to. cisc3120-fall2012-parsons-lectiii.3 34

Summary This lecture looked very quickly at a number of interface components in Java: Buttons Reading from text fields Writing to text fields Listeners for multiple components Simple drawing Mouse events cisc3120-fall2012-parsons-lectiii.3 35