Swing - JTextField. Adding a text field to the main window (with tooltips and all)

Similar documents
Swing - JLabel. Adding a text (and HTML) labels to a GUI

Swing - JButton. Adding buttons to the main window

MIT AITI Swing Event Model Lecture 17

Client-side GUI. A simple Swing-gui for searching for proudcts

Swing from A to Z Some Simple Components. Preface

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

AP CS Unit 11: Graphics and Events

1005ICT Object Oriented Programming Lecture Notes

Measurement of Earthquakes. New Mexico. Supercomputing Challenge. Final Report. April 3, Team #91. Miyamura High School

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

G51PGP Programming Paradigms. Lecture 009 Concurrency, exceptions

Graphical User Interfaces (GUIs)

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

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

To gain experience using GUI components and listeners.

SampleApp.java. Page 1

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

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

Java Programming Lecture 6

Midterm assessment - MAKEUP Fall 2010

CS Exam 1 Review Suggestions

Systems Programming Graphical User Interfaces

Answer on question #61311, Programming & Computer Science / Java

Window Interfaces Using Swing Objects

Final Examination Semester 3 / Year 2008

Graphics User Defined Forms, Part I

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

UTM CSC207: Midterm Examination October 28, 2011

JApplet. toy example extends. class Point { // public int x; public int y; } p Point 5.2. Point. Point p; p = new Point(); instance,

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

Window Interfaces Using Swing Objects

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

Part I: Learn Common Graphics Components

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

RAIK 183H Examination 2 Solution. November 11, 2013

Trident Z Royal. Royal Lighting Control Software Guide

SINGLE EVENT HANDLING

H212 Introduction to Software Systems Honors

GUI Forms and Events, Part II

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

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

RAIK 183H Examination 2 Solution. November 10, 2014

Graphical User Interface

CS108, Stanford Handout #22. Thread 3 GUI

DM503 Programming B. Peter Schneider-Kamp.

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

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

CS 180 Fall 2006 Exam II

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

Agenda. Container and Component

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread

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

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

CS180 Spring 2010 Exam 2 Solutions, 29 March, 2010 Prof. Chris Clifton

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

Project 1. LibraryTest.java. Yuji Shimojo CMSC 335

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

Packages: Putting Classes Together

Points Missed on Page page 1 of 8

Example 3-1. Password Validation

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

Swing Programming Example Number 2

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

State Application Using MVC

1.00 Lecture 14. Lecture Preview

Lecture 9. Lecture

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

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Midterm Test II Object Oriented Programming in Java Computer Science, University of Windsor Fall 2014 Time 2 hours. Answer all questions

Dr. Hikmat A. M. AbdelJaber

CMP 326 Midterm Fall 2015

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

Introduction to the JAVA UI classes Advanced HCI IAT351

Example: Building a Java GUI

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

Example: Building a Java GUI

Class 16: The Swing Event Model

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

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

Window Interfaces Using Swing. Chapter 12

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

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

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

Chapter 12 GUI Basics

I.1 Introduction Matisse GUI designer I.2 GroupLayout Basics Sequential and Parallel Arrangements sequential horizontal orientation

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

CS415 Human Computer Interaction

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

Final Examination Semester 2 / Year 2011

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

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

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...

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

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

This exam is closed textbook(s) and closed notes. Use of any electronic device (e.g., for computing and/or communicating) is NOT permitted.

Class 14: Introduction to the Swing Toolkit

Chapter #1. Program to demonstrate applet life cycle

Transcription:

Swing - JTextField Adding a text field to the main window (with tooltips and all)

Prerequisites - before this lecture You should have seen: The lecture on JFrame The lecture on JButton Including having seen and run the examples on the above, found at https://github.com/progund/java-extra-lectures/tree/master/swing/componen ts-examples

Color codes in RGB Colors in a computer can be described as a 24 bit number with three parts RED (first 8 bits) 1111 1111???????????????? GREEN (next 8 bits)???????? 1111 1111???????? BLUE (last 8 bits)???????????????? 1111 1111 All colors can be produced by mixing R.G.B.: Black: 000000 16 White FFFFFF 16 Purple FF00FF 16 (red mixed with blue) Mix: Purple: 1111 1111 0000 0000 1111 1111 Numbers are given in hexadecimal (00 - FF are 16 bits) 0x0 is 0000 (in four bits) 0xF is 1111, 0xFF is 1111 1111

Let s make a GUI where you enter a color code

Let s make a GUI where you enter a color code If you enter FF00FF (maximum red, no green, maximum blue) you should get purple: You select the entered color by hitting [Enter] or clicking the button. The label gets the specified color (or black if it couldn t be interpreted).

Swing class rough layout public class JTextFieldExample { private JFrame window; // more components... public WindowExample() { initcomponents(); layoutcomponents(); addlisteners(); private void initcomponents() { // initialize the components... // more methods (layoutcomponents(), addlisteners()...)

Let s make a GUI where you enter a color code New components (as instance variables as usual): JLabel (just text with the prompt text Color in hex: JTextField (the text input box) ActionListener - we ll use the same action on both the textfield and button private JFrame window; // main window as before private JButton colorbutton; // the button private JLabel prompt; // the text Color in hex private ActionListener colorchanger; // The action to take private JTextField colortextfield; // where to enter text

Let s make a GUI where you enter a color code Constructor as before (the class is called TextFieldExample): public TextFieldExample() { initcomponents(); layoutcomponents(); addlisteners();

Let s make a GUI where you enter a color code private void initcomponents() { initwindow(); // the JFrame... colorbutton = new JButton("Change color"); prompt = new JLabel("Color in hex:"); // text to show colortextfield = new JTextField("000000"); // intial text colortextfield.selectall(); // select the text colortextfield.requestfocus(); // put focus in the textfield colortextfield.settooltiptext("<html><ul>" + "<li>red: FF0000</li>" + "<li>green: 00FF00</li>" + "<li>blue: 0000FF</li>" + "</ul></html>");

Let s make a GUI where you enter a color code A tooltip is a small help text which pops up when you hover the mouse over a component. Swing components with text can be styled using HTML: colortextfield.settooltiptext("<html><ul>" + "<li>red: FF0000</li>" + "<li>green: 00FF00</li>" + "<li>blue: 0000FF</li>" + "</ul></html>");

Let s make a GUI where you enter a color code

Let s make a GUI where you enter a color code layoutcomponents becomes: private void layoutcomponents() { window.setlayout(new FlowLayout()); window.add(prompt); window.add(colortextfield); window.add(colorbutton);

Creating the ActionListener colorchanger = e -> { Color newcolor = null; try { newcolor = Color.decode("0x" + colortextfield.gettext()); catch (NumberFormatException ignore) { // only valid ints are good if (newcolor!= null) { prompt.setforeground(newcolor); else { prompt.setforeground(black); colortextfield.settext("??????"); colortextfield.requestfocus(); colortextfield.selectall(); ;

Creating the ActionListener colorchanger = e -> { Color newcolor = null; try { //prepend with 0x and get text from textfield newcolor = Color.decode( "0x" + colortextfield.gettext()); catch (NumberFormatException ignore) { // only valid ints are good if (newcolor!= null) { prompt.setforeground(newcolor); else { prompt.setforeground(black); colortextfield.settext("??????"); colortextfield.requestfocus(); colortextfield.selectall(); ;

Creating the ActionListener - complete private void addlisteners() { colorchanger = e -> { Color newcolor = null; try { newcolor = Color.decode("0x" + colortextfield.gettext()); catch (NumberFormatException ignore) { if (newcolor!= null) { prompt.setforeground(newcolor); else { // invalid number was entered prompt.setforeground(black); // reset color to black colortextfield.settext("??????"); // show the user you didn t understand the number colortextfield.requestfocus(); // make it easy to overwrite the question marks colortextfield.selectall(); ; colortextfield.addactionlistener(colorchanger); // add to both textfield colorbutton.addactionlistener(colorchanger); // and button

Source code - A novel in 25 volumes import javax.swing.*; import java.awt.flowlayout; import java.awt.event.*; import static java.awt.color.*; import java.awt.color; public class TextFieldExample { private JFrame window; private JButton colorbutton; private JLabel prompt; private ActionListener colorchanger; private JTextField colortextfield;

Source code - A novel in 25 volumes public TextFieldExample() { initcomponents(); layoutcomponents(); addlisteners(); private void initwindow() { window = new JFrame("JTextField example"); window.setdefaultcloseoperation(jframe.exit_on_close); window.setsize(800, 600);

Source code - A novel in 25 volumes private void initcomponents() { initwindow(); // JFrame initialization moved to separate method to save space here colorbutton = new JButton("Change color"); prompt = new JLabel("Color in hex:"); colortextfield = new JTextField("000000"); colortextfield.selectall(); colortextfield.requestfocus(); colortextfield.settooltiptext("<html><ul><li>red: FF0000</li><li>Green: 00FF00</li><li>Blue: 0000FF</li></ul></html>"); // line-wrap - one single line private void layoutcomponents() { window.setlayout(new FlowLayout()); window.add(prompt); window.add(colortextfield); window.add(colorbutton);

Source code - A novel in 25 volumes private void addlisteners() { colorchanger = e -> { Color newcolor = null; try { newcolor = Color.decode("0x" + colortextfield.gettext()); catch (NumberFormatException ignore) { if (newcolor!= null) { prompt.setforeground(newcolor); else { prompt.setforeground(black); colortextfield.settext("??????"); colortextfield.requestfocus(); colortextfield.selectall(); ; colortextfield.addactionlistener(colorchanger); colorbutton.addactionlistener(colorchanger);

Source code - A novel in 25 volumes public void show() { window.setvisible(true); // end-of-class-declaration

Source code - Main class public class TextFieldExampleRunner { public static void main(string[] args) { try { javax.swing.swingutilities.invokelater(new Runnable() { public void run() { TextFieldExample textex = new TextFieldExample(); textex.show(); ); catch(exception e) { System.err.println("Exception in main: " + e.getmessage());

Further reading https://docs.oracle.com/javase/tutorial/uiswing/components/textfield.html https://docs.oracle.com/javase/tutorial/uiswing/components/tooltip.html https://docs.oracle.com/javase/tutorial/uiswing/components/label.html https://docs.oracle.com/javase/8/docs/api/java/awt/color.html