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

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

Swing - JButton. Adding buttons to the main window

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

MIT AITI Swing Event Model Lecture 17

Systems Programming Graphical User Interfaces

Swing from A to Z Some Simple Components. Preface

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

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

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

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

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

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

Graphical User Interfaces (GUIs)

1005ICT Object Oriented Programming Lecture Notes

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

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

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

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

Graphical User Interface

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

G51PGP Programming Paradigms. Lecture 009 Concurrency, exceptions

Part I: Learn Common Graphics Components

DM503 Programming B. Peter Schneider-Kamp.

H212 Introduction to Software Systems Honors

Introduction to the JAVA UI classes Advanced HCI IAT351

User interfaces and Swing

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

Java Programming Lecture 6

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

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

To gain experience using GUI components and listeners.

Introduction. Introduction

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

Window Interfaces Using Swing Objects

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

Window Interfaces Using Swing Objects

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

CS 251 Intermediate Programming GUIs: Components and Layout

SINGLE EVENT HANDLING

Lecture 5: Java Graphics

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

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!

COMPSCI 230. Software Design and Construction. Swing

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

Midterm assessment - MAKEUP Fall 2010

SampleApp.java. Page 1

COMP-202 Unit 10: Basics of GUI Programming (Non examinable) (Caveat: Dan is not an expert in GUI programming, so don't take this for gospel :) )

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

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

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

Packages: Putting Classes Together

CS Exam 1 Review Suggestions

PART1: Choose the correct answer and write it on the answer sheet:

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

UTM CSC207: Midterm Examination October 28, 2011

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

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

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

Java & Graphical User Interface II. Wang Yang wyang AT njnet.edu.cn

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

Dr. Hikmat A. M. AbdelJaber

Java. GUI building with the AWT

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

Graphic User Interfaces. - GUI concepts - Swing - AWT

Graphics User Defined Forms, Part I

Programming graphics

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

Top-Level Containers

Java - Applets. public class Buttons extends Applet implements ActionListener

Lecture 9. Lecture

Dr. Hikmat A. M. AbdelJaber

Virtualians.ning.pk. 2 - Java program code is compiled into form called 1. Machine code 2. native Code 3. Byte Code (From Lectuer # 2) 4.

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

Swing/GUI Cheat Sheet

Example 3-1. Password Validation

HW#1: Pencil Me In Status!? How was Homework #1? Reminder: Handouts. Homework #2: Java Draw Demo. 3 Handout for today! Lecture-Homework mapping.

Queen s University Faculty of Arts and Science School of Computing CISC 124 Final Examination December 2004 Instructor: M. Lamb

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

Unit 7: Event driven programming

OOP Assignment V. For example, the scrolling text (moving banner) problem without a thread looks like:

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

Java, Swing, and Eclipse: The Calculator Lab.

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

Final Examination Semester 3 / Year 2008

CS111: PROGRAMMING LANGUAGE II

CSC 1214: Object-Oriented Programming

Advanced Java Unit 6: Review of Graphics and Events

SD Module-1 Advanced JAVA

SD Module-1 Advanced JAVA. Assignment No. 4

CS 251 Intermediate Programming GUIs: Event Listeners

Chapter 1 GUI Applications

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

Original GUIs. IntroGUI 1

Graphical interfaces & event-driven programming

Jonathan Aldrich Charlie Garrod

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

AP CS Unit 11: Graphics and Events

2IS45 Programming

Transcription:

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

Prerequisites - before this lecture You should have seen: The lecture on JFrame The lecture on JButton The lectuer on JTextField 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

Text components can use HTML for pimping Text components, such as labels, tool tips etc can be enhanced by HTML Put the text inside <html> tags: <html><strong>some bold text</strong></html> This gives you control over the design fonts, font sizes colors, style html structure such as <p>, <ul>, <h1> etc Even HTML links work, but require quite some extra code

Let s make a GUI where you create clickable links Enter a URL, hit enter. The label will turn into a link which will open your browser with the url loaded.

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

Let s jump right into the code for the JLabel Components (as instance variables as usual): JLabel (just text with the prompt text link will show up here ) JTextField (the text input box where the user enters a url) ActionListener - we ll use the same action on both the textfield and button private JFrame window; private JButton linkbutton; private JLabel link; // will be transformed to a link! private ActionListener linkchanger; // for both button and private JTextField linktextfield; // the text field

Constructor Constructor as before (the class is called LabelExample): public LabelExample() { initcomponents(); layoutcomponents(); addlisteners();

initwindow() - called from initcomponents() private void initwindow() { window = new JFrame("JLabel link example"); window.setdefaultcloseoperation(jframe.exit_on_close); window.setsize(800, 600);

initcomponents() private void initcomponents() { initwindow(); linkbutton = new JButton("Create link"); link = new JLabel("link will show up here"); link.setcursor(cursor.getpredefinedcursor(cursor.hand_cursor)); linktextfield = new JTextField("enter a url here, please"); linktextfield.selectall(); linktextfield.requestfocus(); linktextfield.settooltiptext(new StringBuilder("<html>A tooltip can ").append("have links: ").append("<a href=\"http://wiki.juneday.se\">").append("wiki.junday.se").append("</a></html>").tostring() );

Startup look of the window

layoutcomponents() layoutcomponents becomes: private void layoutcomponents() { window.setlayout(new FlowLayout()); window.add(link); window.add(linktextfield); window.add(linkbutton);

Creating the ActionListener linkchanger = e -> { link.setforeground(blue); link.settext("<html><a href=\"" + linktextfield.gettext() + "\">" + linktextfield.gettext() + "</a></html>"); linktextfield.requestfocus(); linktextfield.selectall(); ; linktextfield.addactionlistener(linkchanger); linkbutton.addactionlistener(linkchanger);

Creating the MouseListener - handle clicks on link link.addmouselistener(new MouseAdapter() { @Override public void mouseclicked(mouseevent me) { try { Desktop.getDesktop().browse(new URL(linkTextField.getText()).toURI()); catch (Exception e) { System.err.println("Error clicking link:" + e.getmessage()); link.settext("malformed url"); );

Source code - A novel in 25 volumes import java.awt.cursor; import java.awt.desktop; import java.awt.flowlayout; import java.awt.event.*; import java.net.url; import javax.swing.*; // to make the mouse a hand... // to open the browser... // layout components row-by-row // ActionListener, MouseAdapter // to make a URI to send to the browser // all components public class LabelExample { private JFrame window; private JButton linkbutton; private JLabel link; private ActionListener linkchanger; private JTextField linktextfield;

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

Source code - A novel in 25 volumes private void initcomponents() { initwindow(); linkbutton = new JButton("Create link"); link = new JLabel("link will show up here"); link.setcursor(cursor.getpredefinedcursor(cursor.hand_cursor)); linktextfield = new JTextField("enter a url here, please"); linktextfield.selectall(); linktextfield.requestfocus(); linktextfield.settooltiptext(new StringBuilder ("<html>a tooltip can have links: <a href=\"http://wiki.juneday.se\">").append("wiki.junday.se").append("</a></html>").tostring());

Source code - A novel in 25 volumes private void layoutcomponents() { window.setlayout(new FlowLayout()); window.add(link); window.add(linktextfield); window.add(linkbutton);

Source code - A novel in 25 volumes private void addlisteners() { linkchanger = e -> { link.settext("<html><a href=\"" + linktextfield.gettext() + "\">" + linktextfield.gettext() + "</a></html>"); linktextfield.requestfocus(); linktextfield.selectall(); ; linktextfield.addactionlistener(linkchanger); linkbutton.addactionlistener(linkchanger); link.addmouselistener(new MouseAdapter() { public void mouseclicked(mouseevent me) { try { Desktop.getDesktop().browse(new URL(linkTextField.getText()).toURI()); catch (Exception e) { System.err.println("Error clicking link:" + e.getmessage()); link.settext("malformed url"); );

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

Source code - Main class public static void main(string[] args) { try { javax.swing.swingutilities.invokelater(new Runnable() { public void run() { LabelExample labelex = new LabelExample(); labelex.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/tutorial/uiswing/events/mouselistener.html https://docs.oracle.com/javase/8/docs/api/java/awt/event/mouseadapter.html https://docs.oracle.com/javase/tutorial/uiswing/misc/desktop.html