JAVA NOTES GRAPHICAL USER INTERFACES

Similar documents
JAVA NOTES GRAPHICAL USER INTERFACES

Window Interfaces Using Swing Objects

Window Interfaces Using Swing Objects

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

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

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!

Introduction. Introduction

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

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

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

Introduction to the JAVA UI classes Advanced HCI IAT351

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

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

CSIS 10A Assignment 7 SOLUTIONS

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

Agenda. Container and Component

Chapter 13 Lab Advanced GUI Applications

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

Graphic User Interfaces. - GUI concepts - Swing - AWT

Calculator Class. /** * Create a new calculator and show it. */ public Calculator() { engine = new CalcEngine(); gui = new UserInterface(engine); }

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

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

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

JRadioButton account_type_radio_button2 = new JRadioButton("Current"); ButtonGroup account_type_button_group = new ButtonGroup();

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

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

H212 Introduction to Software Systems Honors

Example 3-1. Password Validation

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

Chapter 14. More Swing

Java Programming Lecture 6

Dr. Hikmat A. M. AbdelJaber

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

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

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

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

Swing from A to Z Some Simple Components. Preface

University of Cape Town Department of Computer Science Computer Science CSC1017F

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

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

Chapter 12 Advanced GUIs and Graphics

GUI Design. Overview of Part 1 of the Course. Overview of Java GUI 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

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

University of Cape Town Department of Computer Science. Computer Science CSC117F Solutions

1.00 Lecture 14. Lecture Preview

Class 14: Introduction to the Swing Toolkit

Programming Language Concepts: Lecture 8

Systems Programming Graphical User Interfaces

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

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

Window Interfaces Using Swing. Chapter 12

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

Java Graphical User Interfaces AWT (Abstract Window Toolkit) & Swing

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

Building Graphical User Interfaces. Overview

MIT AITI Swing Event Model Lecture 17

Lecture 5: Java Graphics

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

Building Graphical User Interfaces. GUI Principles

Java - Applications. The following code sets up an application with a drop down menu, as yet the menu does not do anything.

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

User interfaces and Swing

Graphical User Interfaces in Java

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

Graphical interfaces & event-driven programming

Class 16: The Swing Event Model

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

Graphical User Interfaces

Packages: Putting Classes Together

CS 251 Intermediate Programming GUIs: Components and Layout

Attempt FOUR questions Marking Scheme Time: 120 mins

Unit 6: Graphical User Interface

// Class fully inheritance and Person immutable GUI nun and Gridbag Layout import java.awt.*; import java.awt.event.*; import javax.swing.

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

Graphical User Interfaces. Comp 152

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

Swing Programming Example Number 2

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Part I: Learn Common Graphics Components

Theory Test 3A. University of Cape Town ~ Department of Computer Science. Computer Science 1016S ~ For Official Use

Object-Oriented Software Engineering. PersonGui (Mark 1) Case Study

JList. Displays a series of items The user can select one or more items Class JList extends directly class Jcomponent Class Jlist supports

AP CS Unit 11: Graphics and Events

COMP16121 Sample Code Lecture 1

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

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

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

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

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

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

PIC 20A GUI with swing

GUI Basics. Object Orientated Programming in Java. Benjamin Kenwright

SD Module-1 Advanced JAVA

We are on the GUI fast track path

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

SD Module-1 Advanced JAVA. Assignment No. 4

CS193k, Stanford Handout #16

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

Transcription:

1 JAVA NOTES GRAPHICAL USER INTERFACES Terry Marris 24 June 2001 5 TEXT AREAS 5.1 LEARNING OUTCOMES By the end of this lesson the student should be able to understand how to get multi-line input from the user use text area for input and output 5.2 INTRODUCTION In the last chapter we saw how to obtain input from the user a line at a time. Sometimes we need to collect user input that is more than one line long. To do this we use the JTextArea component. With a text area, a user can enter any number of lines by using the enter key to separate them. Each line is terminated with '\n'.

2 5.3 CAR SERVICE GUI Figure 5.1 shows what we are aiming for. Figure 5.1 Text Area Input Output The user enters a car's registration number and makes some entries in the Service History area of the input panel. When the ok button is clicked, the input is echoed to the text area of the output panel. But because there are more lines of text than can fit in the allocated space, a scroll bar is generated. A scroll bar allows the user to scroll through text in a text area.

3 5.4 JTEXT AREAS We specify the number of rows and columns in the JTextArea constructor. servicehistorytextarea = new JTextArea(8, 40); This creates a text area with eight lines of forty columns each. We place the text area in a scroll pane. A scroll pane handles scrolling and scroll bars. JScrollPane scrollpane = new JScrollPane(serviceHistoryTextArea); Then we add the scroll pane to a container e.g. a panel. add(scrollpane, "South"); If a line of text is too long to fit into its allocated width, we can specify line breaks to occur at word boundaries. outputtextarea.setlinewrap(true); outputtextarea.setwrapstyleword(true); This does not insert hard returns or newline characters '\n'; it just provides a view of the text.

4 5.5 CAR SERVICE HISTORY FRAME The code that produced Figure 4.1 is shown below. /* CarServiceHistoryFrame.java Terry Marris 24 June 2001 */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; class RegistrationNumberPanel extends JPanel private JTextField regnumfield; public RegistrationNumberPanel() JLabel regnumlabel = new JLabel("Registration Number"); add(regnumlabel); regnumfield = new JTextField(10); add(regnumfield);

5 class ServiceHistoryPanel extends JPanel private JTextArea servicehistorytextarea; public ServiceHistoryPanel() setlayout(new BorderLayout()); JLabel servicehistorylabel = new JLabel("Service History"); add(servicehistorylabel, "North"); servicehistorytextarea = new JTextArea(8, 40); JScrollPane scrollpane = new JScrollPane(serviceHistoryTextArea); add(scrollpane, "South"); public String servicehistory() return servicehistorytextarea.gettext();

6 class InputPanel extends JPanel private RegistrationNumberPanel regnumpanel; private ServiceHistoryPanel servicehistorypanel; public InputPanel() Border etched = BorderFactory.createEtchedBorder(); Border titled = BorderFactory.createTitledBorder( etched, "Input Panel"); setborder(titled); setlayout(new BorderLayout()); regnumpanel = new RegistrationNumberPanel(); add(regnumpanel, "North"); servicehistorypanel = new ServiceHistoryPanel(); add(servicehistorypanel, "South"); public String servicehistory() return servicehistorypanel.servicehistory();

7 class OutputPanel extends JPanel private JTextArea outputtextarea; public OutputPanel() Border etched = BorderFactory.createEtchedBorder(); Border titled = BorderFactory.createTitledBorder( etched, "Output Panel"); setborder(titled); outputtextarea = new JTextArea(3, 40); outputtextarea.seteditable(false); outputtextarea.setbackground(color.white); outputtextarea.setlinewrap(true); outputtextarea.setwrapstyleword(true); JScrollPane scrollpane = new JScrollPane(outputTextArea); add(scrollpane); public void setoutputtextarea(string text) outputtextarea.settext(text);

8 class ButtonPanel extends JPanel private JButton okbutton; public ButtonPanel() okbutton = new JButton("OK"); add(okbutton); public JButton getokbutton() return okbutton;

9 class CarServiceHistoryPanel extends JPanel implements ActionListener private InputPanel inputpanel; private OutputPanel outputpanel; private ButtonPanel buttonpanel; public CarServiceHistoryPanel() setlayout(new BorderLayout()); inputpanel = new InputPanel(); add(inputpanel, "North"); buttonpanel = new ButtonPanel(); add(buttonpanel, "Center"); buttonpanel.getokbutton().addactionlistener(this); outputpanel = new OutputPanel(); add(outputpanel, "South"); public void actionperformed(actionevent e) Object source = e.getsource(); if (source == buttonpanel.getokbutton()) String text = inputpanel.servicehistory(); outputpanel.setoutputtextarea(text);

10 public class CarServiceHistoryFrame extends JFrame public CarServiceHistoryFrame() settitle("car Service History Frame"); Toolkit tk = Toolkit.getDefaultToolkit(); Dimension screen = tk.getscreensize(); setlocation(0, 0); addwindowlistener(new WindowAdapter() public void windowclosing(windowevent e) System.exit(0); ); Container contentpane = getcontentpane(); contentpane.add(new CarServiceHistoryPanel()); pack(); public static void main(string[] s) JFrame aframe = new CarServiceHistoryFrame(); aframe.show();

11 5.6 DOCUMENTATION JTextArea has methods that are the same as JTextField because they both have the same inheritance hierarchy. javax.swing.jtextarea Constructors JTextArea(int rows, int columns) JTextArea(String text, int rows, int columns) initialises a new empty text area with the given number of rows and columns. initialises a new text area with the given text, rows and columns. Methods void append(string str) int getlinecount() void insert(string str, int position) void replacerange(string str, int start, int end) void setlinewrap(boolean on) void setwrapstyleword(boolean on) adds the given string to the end of the document. returns the number of lines contained in this area. inserts the given string at the given position in this text area. replaces the text from the start to the end position with the given text. sets the line-wrapping policy for this area. If true the lines will be wrapped if they are too long to fit within the allocated width. sets the style of wrapping used if this text area is wrapping lines. If true the lines will be wrapped at word boundaries.

12 5.7 FURTHER READING HORSTMANN & CORNELL Core Java 2 Volume 1 pp 400 www.java.sun.com/docs/books/tutorial In the next chapter we look at radio buttons. 5.8 REVIEW 5.9 EXERCISES 1 Draw a class diagram that reflects the structure of program CarServiceHistoryFrame.java shown in 5.5 above. 2 Explain each line of the OutputPanel class. 3 Explain each line of the CarServiceHistoryPanel class. By diagrams, or otherwise, explain the sequence of messages, how the CarServiceHistoryPanel object obtains a copy of the service history text.