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

Similar documents
Graphical User Interface

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

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!

Graphical User Interfaces in Java - SWING

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

CS111: PROGRAMMING LANGUAGE II

CSCI 201L Midterm Written Summer % of course grade

Dr. Hikmat A. M. AbdelJaber

SampleApp.java. Page 1

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

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

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

Object-Oriented Software Engineering Re-exam, 2012 (Also Object-Oriented Analysis, Design and Programming, Re-exam, 2012)

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

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

Graphic User Interfaces. - GUI concepts - Swing - AWT

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

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

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

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

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

Section Basic graphics

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Java Programming Lecture 6

Window Interfaces Using Swing Objects

Graphical User Interface (GUI)

AP CS Unit 11: Graphics and Events

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

Systems Programming. Bachelor in Telecommunication Technology Engineering Bachelor in Communication System Engineering Carlos III University of Madrid

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

CSCI 201L Midterm Written SOLUTION Summer % of course grade

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Introduction to the JAVA UI classes Advanced HCI IAT351

Programming graphics

Swing from A to Z Some Simple Components. Preface

Adding Buttons to StyleOptions.java

COMP16121 Sample Code Lecture 1

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

Window Interfaces Using Swing Objects

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

2110: GUIS: Graphical User Interfaces

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

Swing Programming Example Number 2

Systems Programming Graphical User Interfaces

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

Java. GUI building with the AWT

CSIS 10A Assignment 7 SOLUTIONS

CSCI 201L Midterm Written SOLUTION Fall % of course grade

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

JFrame & JLabel. By Iqtidar Ali

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

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

Part I: Learn Common Graphics Components

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

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

Packages: Putting Classes Together

FirstSwingFrame.java Page 1 of 1

COSC 123 Computer Creativity. Graphics and Events. Dr. Ramon Lawrence University of British Columbia Okanagan

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

Multiple Choice Questions: Identify the choice that best completes the statement or answers the question. (15 marks)

PART 23. Java GUI Advanced JList Component. more items.

Graphical User Interfaces in Java

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

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

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

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

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

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

CSCI 201L Midterm Written Fall % of course grade

Dr. Hikmat A. M. AbdelJaber

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

Chapter 9 Designing Graphical User Interfaces (GUIs)

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

Lecture 5: Java Graphics

Unit 6: Graphical User Interface

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

Java, Swing, and Eclipse: The Calculator Lab.

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

Page 1 of 16. Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Chapter 17 Creating User Interfaces

The class definition is not a program by itself. It can be used by other programs in order to create objects and use them.

Graphical User Interface (GUI)

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

Attempt FOUR questions Marking Scheme Time: 120 mins

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

B2.52-R3: INTRODUCTION TO OBJECT ORIENTATED PROGRAMMING THROUGH JAVA

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

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

CHAPTER 2. Java Overview

PIC 20A GUI with swing

Chapter 12 GUI Basics

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

Workbook 7. Remember to check the course website regularly for announcements and errata:

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Transcription:

Goals By the end of this lesson, you should: Lecture 7 More GUI programming 1. Be able to write Java s with JTextField, JList, JCheckBox and JRadioButton components 2. Be able to implement a ButtonGroup 3. Be able to query the status of such checkboxes and radio buttons D&D 12 Semester 1 2018 2 The The JFrame subclass: ListOWords JTextField JLabel JButton JCheckBox JRadioButton ButtonGroup JList JScrollPane 3 4

Application startup Imports in ListOWords import javax.swing.swingutilities; public class RunListOWords implements Runnable { public void run() { ListOWords l = new ListOWords(); public static void main(string[] args) { SwingUtilities.invokeLater(new RunListOWords()); This is again the proper way of starting our Swing to ensure it runs in the event dispatch thread. import java.awt.flowlayout; // Controls general layout on JFrame import java.awt.event.actionlistener; import java.awt.event.actionevent; import java.awt.font; // A Font (for those at the back) import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jtextfield; import javax.swing.listselectionmodel; // for your homework import javax.swing.jbutton; import javax.swing.jlist; import javax.swing.listmodel; import javax.swing.defaultlistmodel; import javax.swing.jscrollpane; // A scrollable pane for lists etc. import javax.swing.jcheckbox; import javax.swing.jpanel; // A panel for our radio buttons import java.awt.gridbaglayout; // Layout for our radio buttons import java.awt.gridbagconstraints; import javax.swing.jradiobutton; import javax.swing.buttongroup; // groups radio buttons We need all of these imports in ListOWords.java so the Java compiler can resolve the various JComponents and AWT classes. 5 6 The fields in ListOWords public class ListOWords extends JFrame implements ActionListener { private final JLabel label; private final JButton button; private final JTextField textfield; private final JList<String> wordlist; private final JCheckBox checkboxreversestring; private final JPanel radiobuttonpanel; private final ButtonGroup radiobuttons; private final JRadioButton radiobuttonnormalcase; private final JRadioButton radiobuttonuppercase; private final JRadioButton radiobuttonlowercase; The fields all correspond to the components we ve identified on slide 4. The rest of the class consists of two methods: constructor ListOWords(). It configures the JFrame and the components, and adds the components to the frame. 2. The actionperformed() method implements the ActionListener interface. Here, it handles the ActionEvent that the Add button dispatches. 7 The constructor The constructor ListOWords() sets up more or less the whole GUI. This is very common in GUI s. The example here shows that this can result in quite a large piece of code with little structure, even for a small like this. On the following slides, we will go through the constructor in thematic steps. 8

ListOWords() (part 1) public ListOWords() { super("listing words in all sorts of ways"); setlayout(new FlowLayout()); setsize(300,500); setdefaultcloseoperation(jframe.exit_on_close); Font font = new Font("Sans serif", Font.PLAIN, 20); label = new JLabel("New word to add"); label.setfont(font); add(label); textfield = new JTextField(10); textfield.setfont(font); add(textfield); Note: Configuration of the JFrame (layout, size, close behaviour) doesn t have to happen in the class that starts the frame: Here, we have the ListOWords frame configure itself! FlowLayout arranges the components in the order in which they are added. The Font object here sets a font larger than default (so those of you in the cheap seats at the back can see it) We use that font in the JLabel and JTextField, among others. 9 ListOWords() (part 2) button = new JButton("Add"); button.addactionlistener(this); button.setfont(font); add(button); checkboxreversestring = new JCheckBox("Reverse string"); add(checkboxreversestring); The JButton configuration is the same as in our Hello World. Again, we implement the ActionListener interface in the class so just pass this. The JCheckBox takes its label from the constructor parameter Note: The checkbox doesn t use the Font object (sorry if you are in the cheap seats at the back). This is to give you an idea as to what default font size is, in comparison. 10 ListOWords() (part 3) radiobuttonpanel = new JPanel(); radiobuttonpanel.setlayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); c.gridx = 0; c.gridy = GridBagConstraints.RELATIVE; c.anchor = GridBagConstraints.WEST; radiobuttons = new ButtonGroup(); These lines get us ready for the radio buttons A JPanel with a constrained GridBagLayout lets us arrange the buttons in a neat column. Each button will be positioned with 0 offset within the panel, leftaligned (WEST), with each button positioned vertically RELATIVE to the parent component (the JPanel) or its predecessor button. We also use a ButtonGroup. This data structure from AWT will ensure that only one of the buttons in the ButtonGroup can be checked at any one time. Next: The radio buttons 11 ListOWords() (part 4) radiobuttonnormalcase = new JRadioButton("Leave case as is"); radiobuttonnormalcase.setfont(font); radiobuttonnormalcase.setselected(true); radiobuttons.add(radiobuttonnormalcase); radiobuttonpanel.add(radiobuttonnormalcase, c); radiobuttonuppercase = new JRadioButton("All upper case"); radiobuttonuppercase.setfont(font); radiobuttons.add(radiobuttonuppercase); radiobuttonpanel.add(radiobuttonuppercase, c); radiobuttonlowercase = new JRadioButton("All lower case"); radiobuttonlowercase.setfont(font); radiobuttons.add(radiobuttonlowercase); radiobuttonpanel.add(radiobuttonlowercase, c); add(radiobuttonpanel); Note we set the first of the buttons as being selected. We then add the buttons both to the ButtonGroup and to the JPanel. Finally, we add the panel to the JFrame 12

ListOWords() (part 6) wordlist = new JList<String>(new DefaultListModel<String>()); wordlist.setfont(font); wordlist.setvisiblerowcount(25); wordlist.setfixedcellheight(25); wordlist.setfixedcellwidth(280); add(new JScrollPane(wordList)); setvisible(true); The DefaultListModel tells the JList how to implement the list internally, in this case as a Vector (a data structure similar to an ArrayList) The visible row count gives us the number of elements on the list that are visible without scrolling Packaging the list in a JScrollPane lets us scroll Cell height/width are the height and width of each individual element cell Play with these values to see what happens! 13 actionperformed() public void actionperformed(actionevent event) { String newword = textfield.gettext(); if (checkboxreversestring.isselected()) { String reverseword = ""; for (int i = newword.length() 1; i >= 0; i ) { reverseword += newword.charat(i); newword = reverseword; if (radiobuttonuppercase.isselected()) { newword = newword.touppercase(); if (radiobuttonlowercase.isselected()) { newword = newword.tolowercase(); ((DefaultListModel)(wordList.getModel())).addElement(newWord); We check whether the checkbox is checked and reverse the string from the text field if required. We then check which radio button is checked and compute the upper/lowercase version as applicable. We then add the new word to the list 14 actionperformed() What do we know ((DefaultListModel)(wordList.getModel())).addElement(newWord); This looks a bit complex, right? Our problem here is that there is no uniform way to add elements to a JList. The methods for adding to a JList are defined in its list model. The list model is an interface, which is not implemented by the JList class, but by the underlying data structure that implements the list (in our case, the vector). So what we need to do here is: Get a reference to the underlying data structure with wordlist.getmodel(). This returns an object that implements a ListModel interface. Unfortunately, addelement() is not a method of ListModel. However, we know that the object returned is actually an instance of the DefaultListModel class, which has an addelement() method. So we typecast the ListModel return value to a DefaultListModel, and call addelement() on this reference. 1. We can configure the size and behavior of a JFrame subclass in its constructor. 2. Using a Font object with setfont() allows us to change the font type and size on components. 3. We can read the text in a JTextField with its gettext() method. 4. Radio buttons are typically grouped so only one of the buttons in the group can be checked. 5. Checkboxes and radio buttons check states can be queried with the isselected() method. 6. The underlying storage in a JList is controlled by a data model. 7. If we want a JList to scroll, we can package it in a JScrollPane 8. Sometimes, we need type casting! 15 16

Resources & Homework D&D Chapter 12 https://docs.oracle.com/javase/tutorial/uiswing/components/textfield.html https://docs.oracle.com/javase/tutorial/uiswing/components/button.html https://docs.oracle.com/javase/tutorial/uiswing/components/list.html https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html Homework: Refactor (rewrite) the constructor of the ListOWords class such that it uses a series of private methods to set up the different parts of the GUI. Next Lecture Even more GUI programming (Chapter 12) 17