Swing - JButton. Adding buttons to the main window

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

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

MIT AITI Swing Event Model Lecture 17

Commands. Written commands can be reused, cataloged, etc. Sometimes they also contain "undo" commands, too.

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

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

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 :) )

Systems Programming Graphical User Interfaces

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

Introduction. Introduction

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

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

1005ICT Object Oriented Programming Lecture Notes

DM503 Programming B. Peter Schneider-Kamp.

Swing from A to Z Some Simple Components. Preface

Graphical User Interface (Part-1) Supplementary Material for CPSC 233

Class 16: The Swing Event Model

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

SampleApp.java. Page 1

1.00/1.001 Introduction to Computers and Engineering Problem Solving Fall (total 7 pages)

Name: Checked: Learn about listeners, events, and simple animation for interactive graphical user interfaces.

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

DEMYSTIFYING PROGRAMMING: CHAPTER SIX METHODS (TOC DETAILED) CHAPTER SIX: METHODS 1

Original GUIs. IntroGUI 1

Java. GUI building with the AWT

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

Agenda. Container and Component

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

CS 251 Intermediate Programming GUIs: Components and Layout

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

AP CS Unit 11: Graphics and Events

CSE331 Fall 2014, Final Examination December 9, 2014 Please do not turn the page until 2:30. Rules:

Midterm assessment - MAKEUP Fall 2010

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

Java Help Files. by Peter Lavin. May 22, 2004

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

SINGLE EVENT HANDLING

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

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

RAIK 183H Examination 2 Solution. November 11, 2013

CS11 Java. Fall Lecture 4

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

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

Window Interfaces Using Swing. Chapter 12

Introduction to the JAVA UI classes Advanced HCI IAT351

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

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

Object Oriented Design & Patterns. Part 1

Java - Applets. public class Buttons extends Applet implements ActionListener

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 Interface (GUI)

Chapter 6 Using Objects

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

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

Dr. Hikmat A. M. AbdelJaber

To gain experience using GUI components and listeners.

Example: Building a Java GUI

PIC 20A GUI with swing

Example: Building a Java GUI

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

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

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

User interfaces and Swing

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Graphical User Interfaces

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

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

Heavyweight with platform-specific widgets. AWT applications were limited to commonfunctionality that existed on all platforms.

Chapter 7: A First Look at GUI Applications

Graphical User Interfaces in Java - SWING

Part I: Learn Common Graphics Components

RAIK 183H Examination 2 Solution. November 10, 2014

CHAPTER 2. Java Overview

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Points Missed on Page page 1 of 8

CSE 143 Au04 Midterm 1 Page 1 of 9

Object-Oriented Programming: Revision. Revision / Graphics / Subversion. Ewan Klein. Inf1 :: 2008/09

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

MVC: Model View Controller

State Application Using MVC

Basicsof. JavaGUI and SWING

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

What Is an Event? Some event handler. ActionEvent. actionperformed(actionevent e) { }

Programming Languages and Techniques (CIS120e)

JAVA NOTES GRAPHICAL USER INTERFACES

Window Interfaces Using Swing Objects

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

1. (5 points) In your own words, describe what an instance is.

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

Programming Language Concepts: Lecture 8

Containers and Components

Programmierpraktikum

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

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

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

Design Patterns and Frameworks Command

Chapter 13 Lab Advanced GUI Applications

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

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

Transcription:

Swing - JButton Adding buttons to the main window

An empty JFrame is not very useful // In some GUI class: window = new JFrame("Window example"); window.setsize(800,600); window.setdefaultcloseoperation(jframe.exit_on_close); window.setvisible(true);

Swing class rough layout public class WindowExample { private JFrame window; public WindowExample() { initcomponents(); private void initcomponents() { window = new JFrame("Window example"); window.setdefaultcloseoperation(jframe.exit_on_close); window.setsize(800, 600); public void show() { window.setvisible(true);

Adding two buttons public class WindowExample { // Our class has a JFrame and two JButtons private JFrame window; private JButton closebutton; private JButton togglebutton; public WindowExample() { initcomponents(); private void initcomponents() { window = new JFrame("Window example"); window.setdefaultcloseoperation(jframe.exit_on_close); window.setsize(800, 600); closebutton = new JButton("Close"); // Button text togglebutton = new JButton("Toggle"); //...

Let s add a layoutcomponents() method public class WindowExample { private JFrame window; private JButton closebutton; private JButton togglebutton; public WindowExample() { initcomponents(); layoutcomponents(); // called from the constructor private void layoutcomponents() { // Put the buttons somewhere in the JFrame... //...

The JFrame needs a layout manager public class WindowExample { private JFrame window; private JButton closebutton; private JButton togglebutton; public WindowExample() { initcomponents(); layoutcomponents(); // called from the constructor private void layoutcomponents() { window.setlayout(new FlowLayout()); // Will put components in sequence window.add(closebutton); window.add(togglebutton); //...

LayoutManagers - FlowLayout components keep their preferred size are placed row-by-row wrap if they don t fit on a single row

The two buttons show up but don t do anything Actionable components (like buttons, menus, menu items etc) need an ActionListener object to encapsulate the action to perform when acted on ActionListener is an interface in java.awt.event and has only one method declared - public void actionperformed(actionevent ae) { You put the code for the action in actionperformed() using the addactionlistener() method: private void addlisteners() { // new method, called from the constructor closebutton.addactionlistener(e -> window.dispose()); /* Or: closebutton.addactionlistener(new ActionListener() { public void actionperformed(actionevent ae) { window.dispose(); // closes the main window ); */

The constructor now looks like this public ButtonExample() { initcomponents(); layoutcomponents(); addlisteners();

Adding an action to the toggle button // Make the toggle button change its text color back and forth private void addlisteners() { closebutton.addactionlistener(e -> window.dispose()); togglebutton.addactionlistener(e -> { if (togglebutton.getforeground()!= RED) { togglebutton.setforeground(red); else { togglebutton.setforeground(black); );

Result

Source code import javax.swing.*; import java.awt.flowlayout; import java.awt.event.*; import static java.awt.color.red; import static java.awt.color.black; public class ButtonExample { private JFrame window; private JButton closebutton; private JButton togglebutton; public ButtonExample() { initcomponents(); layoutcomponents(); addlisteners();

Source code private void initcomponents() { window = new JFrame("Window example"); window.setdefaultcloseoperation(jframe.exit_on_close); window.setsize(800, 600); closebutton = new JButton("Close"); togglebutton = new JButton("Toggle"); private void layoutcomponents() { window.setlayout(new FlowLayout()); window.add(closebutton); window.add(togglebutton);

Source code private void addlisteners() { closebutton.addactionlistener(e -> window.dispose()); togglebutton.addactionlistener(e -> { if (togglebutton.getforeground()!= RED) { togglebutton.setforeground(red); else { togglebutton.setforeground(black); ); public void show() { window.setvisible(true); // end of class declaration

Further reading https://docs.oracle.com/javase/tutorial/uiswing/components/button.html https://docs.oracle.com/javase/tutorial/uiswing/layout/flow.html https://docs.oracle.com/javase/tutorial/uiswing/events/intro.html https://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html