JButton Chapter 11 Lecture 3.

Slides:



Advertisements
Similar presentations
1 Chapter 13 Creating User Interfaces. 2 Objectives F To create graphical user interfaces with various user-interface components: JButton, JCheckBox,
Advertisements

Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Java GUI Libraries Swing Programming. Swing Components Swing is a collection of libraries that contains primitive widgets or controls used for designing.
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 1 Lecture 9 Object Oriented Programming in Java Advanced Topics Abstract Windowing.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 16 Creating User Interfaces.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 15 Creating User.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L03 (Chapter 15) Creating.
1 Creating User Interfaces. 2 Motivations A graphical user interface (GUI) makes a system user-friendly and easy to use. Creating a GUI requires creativity.
Graphical User Interface Components: Part 1
1 Class 8. 2 Chapter Objectives Use Swing components to build the GUI for a Swing program Implement an ActionListener to handle events Add interface components.
More on Creating GUIs in Java using Swing David Meredith Aalborg University.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
Java Programming Chapter 10 Graphical User Interfaces.
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 13 Creating User.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
Dale Roberts GUI Programming using Java - Event Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Graphical User Interface Components: Part 1 Chapter 11.
SWING. AbstractButton Swing Buttons are subclasses of the AbstractButton class, which extends JComponent. Abstract class javax.swing.AbstractButton AbstractButton.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Dale Roberts GUI Programming using Java - GUI Components Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/15) MVC and Swing Joel Adams and Jeremy Frens Calvin College.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
For (int i = 1; i
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 15 Creating User.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
GUI Basics. Agenda What GUI How to make in java Creating frames Frequently used GUI components Layout Managers.
Creating User Interfaces Event-Driven Programming.
Computer Science [3] Java Programming II - Laboratory Course Lab 4 -1 : Introduction to Graphical user interface GUI Components Faculty of Engineering.
Java GUI with Swing (Part I) Java – How to Program By Deitel & Deitel and iswing/index.html
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.
 Figure illustrates a hierarchy containing many event classes from the package java.awt.event.  Used with both AWT and Swing components.  Additional.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Lesson 28: More on the GUI button, frame and actions.
1 Chapter 13 – More GUI Components Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides users with basic.
1 Lecture 8: User Interface Components with Swing.
Jozef Goetz Credits: Copyright  Pearson Education, Inc. All rights reserved. expanded by J. Goetz, 2016.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
Event Driven (Asynchronous) Programming. Event handling in Unity Subclass a class that contains event handling methods, and then override those methods.
Chapter 6 Building Java GUIs. MVC Model View Controller The model passes its data to the view for rendering The view determines which events are passed.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
Advanced OOP MCS-3 OOP BSCS-3 Lecture # 7. GUI Components - > Jframe ->JPasswordField (javax.Swing.JPasswordField) GUI Components - > Jframe ->JPasswordField.
Lecture 6 Object Oriented Programming Using Java By Rashid Ahmad Department of Computer Science University of Peshawar.
© Copyright by Pearson Education, Inc. All Rights Reserved. Appendix I GUI Components and Event Handling Android How to Program, 3/e.
GUI Programming using Java - Event Handling
Lecture 15 Basic GUI programming
Lecture 15.1 Event Delegation.
GUIs and Events Rick Mercer.
CompSci 230 S Programming Techniques
Introduction to Graphics
Lecture 16 More GUI programming
Appendix I GUI Components and Event Handling
Advanced User Interfaces
Graphical User Interface (pronounced "gooey")
Ellen Walker Hiram College
Event-driven programming for GUI
Chapter 7 Creating User Interfaces
MSIS670: Object-Oriented Software Engineering
Chapter 13 Creating User Interfaces
IFS410: Advanced Analysis and Design
Introduction to Event Handling
COP 4610L: Applications in the Enterprise Spring 2005
COP 4610L: Applications in the Enterprise Spring 2005
CiS 260: App Dev I Chapter 6: GUI and OOD.
Presentation transcript:

JButton Chapter 11 Lecture 3

Jbutton A button is a component the user clicks to trigger a specific action. Types of buttons include: Command buttons Checkboxes Toggle buttons Radio buttons

Swing button hierarchy JComponent AbstractButton JButton JToggleButton All the button types are subclassese of AbstractButton (javax.swing) --- declares the common features of Swing buttons JCheckBox JRadioButton

JButton A command button generates an ActionEvent when the user clicks the button. Command buttons are created with class Jbutton. The text on the face of a Jbuttonis called a button label. A GUI can have many JButtons Button label should be UNIQUE in the currently displayed GUI

Constructor Summary JButton()   Creates a button with no set text or icon. JButton(Action a)  Creates a button where properties are taken from the Action supplied. JButton(Icon icon)   Creates a button with an icon. JButton(String text)    Creates a button with text. JButton(String text, Icon icon)            Creates a button with initial text and an icon.

Example – ButtonFrame.java import java.awt.FlowLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JOptionPane;

Button variables declaration private JButton plainJButton; // button with just text private JButton fancyJButton; // button with icons

Creates buttons plainJButton = new JButton( "Plain Button" ); // button with text add( plainJButton ); // add plainJButton to Jframe ----------------------------------------------------- fancyJButton = new JButton( "Fancy Button", bug1 ); // set image fancyJButton.setRolloverIcon( bug2 ); // set rollover image add( fancyJButton ); // add fancyJButton to JFrame Rollover : The icon is displayed when the user positions the mouse over the button setRolloverIcon is inherited from the AbstractButton

Example - continued Create an object of private inner class ButtonHandler and register it as event handler for each button: ButtonHandler handler = new ButtonHandler(); fancyJButton.addActionListener( handler ); plainJButton.addActionListener( handler );

Class ButtonHandler // inner class for button event handling private class ButtonHandler implements ActionListener { // handle button event public void actionPerformed( ActionEvent event ) JOptionPane.showMessageDialog( ButtonFrame.this, String.format( "You pressed: %s", event.getActionCommand() ) ); } // end method actionPerformed } // end private inner class ButtonHandler