Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.

Slides:



Advertisements
Similar presentations
CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
Advertisements

GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Graphical User Interface (GUI) Programming III. Lecture Objectives Exploring more GUI programming elements in Java Using labels in GUIs Using colors to.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Chapter Day 10. © 2007 Pearson Addison-Wesley. All rights reserved4-2 Agenda Day 10 Questions from last Class?? Problem set 2 posted  10 programs from.
Graphical User Interface (GUI) Programming IV. Lecture Objectives Exploring more GUI programming elements in Java Using icons in GUIs Using scroll bars.
Chapter 121 Window Interfaces Using Swing Chapter 12.
CS102--Object Oriented Programming Lecture 19: – The Swing Package (II) Copyright © 2008 Xiaoyan Li.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L04 (Chapter 15) Creating.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
CS102--Object Oriented Programming Lecture 18: – The Swing Package Copyright © 2008 Xiaoyan Li.
© The McGraw-Hill Companies, 2006 Chapter 10 Graphics and event- driven programs.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.
Chapter Day 9. © 2007 Pearson Addison-Wesley. All rights reserved4-2 Agenda Day 8 Questions from last Class?? Problem set 2 posted  10 programs from.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Java Swing. Introduction to Swing A GUI (graphical user interface) is a windowing system that interacts with the user The Java AWT (Abstract Window Toolkit)
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
Java Programming Chapter 10 Graphical User Interfaces.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Programming With Java ICS201 University Of Ha’il1 Chapter 17 Graphical User Interfaces Swing I.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
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,
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 13 : Swing I King Fahd University of Petroleum & Minerals College of Computer Science.
An Introduction to Programming and Object Oriented Design using Java 3 rd Edition. Dec 2007 Jaime Niño Frederick Hosch Chapter 18 Integrating user interface.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
Comp 249 Programming Methodology Chapter 17 Swing I Prof. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 15 Creating User.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
COMP 150: Introduction to Object-Oriented Programming 1 Lecturer: Dr. AJ Bieszczad l Event-Driven Programming and GUIs l Swing Basics and a Simple Demo.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 5 Objectives  Learn about basic GUI components.  Explore how the GUI.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Enhanced Car Payment.
Chapter 11 - A GUI Interacting With a Problem Domain Class1 Chapter 11 A GUI Interacting With a Problem Domain Class 11.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 4 – Completing the Inventory Application.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
Creating a GUI Class An example of class design using inheritance and interfaces.
Computer Science [3] Java Programming II - Laboratory Course Lab 4 -1 : Introduction to Graphical user interface GUI Components Faculty of Engineering.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
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,
Slides prepared by Rose Williams, Binghamton University Chapter 19 Swing II.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
GUIs & Event-Driven Programming Chapter 11 Review.
GUIs and Events Rick Mercer.
CompSci 230 S Programming Techniques
Chapter 17 Swing I Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
GUI’s.
A First Look at GUI Applications
Graphical User Interface (pronounced "gooey")
Graphical User Interface (GUI) Programming III
Java Programming: From Problem Analysis to Program Design,
Chapter 17 Swing I Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage Copyright © 2016 Pearson Inc. All.
Comp 249 Programming Methodology
Window Interfaces Using Swing Objects Chapter 12
Window Interfaces Using Swing Objects Chapter 12
Presentation transcript:

Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I

© 2006 Pearson Addison-Wesley. All rights reserved17-2 Swing I: (4/4) Reading: Pages 968 – 999 Outline –The AbstractButton Class –The SetActionCommand Method –Listeners as Inner Classes –Text Areas & Text Fields –Uncaught Exceptions

© 2006 Pearson Addison-Wesley. All rights reserved17-3 The AbstractButton and Dimension Classes The classes JButton and JMenuItem are derived classes of the abstract class named AbstractButton –All of their basic properties and methods are inherited from the class AbstractButton Objects of the Dimension class are used with buttons, menu items, and other objects to specify a size –The Dimension class is in the package java.awt Dimension(int width, int height) –Note: width and height parameters are in pixels

© 2006 Pearson Addison-Wesley. All rights reserved17-4 The setActionCommand Method When a user clicks a button or menu item, an event is fired that normally goes to one or more action listeners –The action event becomes an argument to an actionPerformed method –This action event includes a String instance variable called the action command for the button or menu item –The default value for this string is the string written on the button or the menu item –This string can be retrieved with the getActionCommand method e.getActionCommand()

© 2006 Pearson Addison-Wesley. All rights reserved17-5 The setActionCommand Method The setActionCommand method can be used to change the action command for a component –This is especially useful when two or more buttons or menu items have the same default action command strings JButton nextButton = new JButton("Next"); nextButton.setActionCommand("Next Button"); JMenuItem choose = new JMenuItem("Next"); choose.setActionCommand("Next Menu Item");

© 2006 Pearson Addison-Wesley. All rights reserved17-6 Some Methods in the Class AbstractButton (Part 1 of 3)

© 2006 Pearson Addison-Wesley. All rights reserved17-7 Some Methods in the Class AbstractButton (Part 2 of 3)

© 2006 Pearson Addison-Wesley. All rights reserved17-8 Some Methods in the Class AbstractButton (Part 3 of 3)

© 2006 Pearson Addison-Wesley. All rights reserved17-9 Listeners as Inner Classes Often, instead of having one action listener object deal with all the action events in a GUI, a separate ActionListener class is created for each button or menu item –Each button or menu item has its own unique action listener –There is then no need for a multiway if-else statement When this approach is used, each class is usually made a private inner class

© 2006 Pearson Addison-Wesley. All rights reserved17-10 Listeners as Inner Classes (Part 1 of 6)

© 2006 Pearson Addison-Wesley. All rights reserved17-11 Listeners as Inner Classes (Part 2 of 6)

© 2006 Pearson Addison-Wesley. All rights reserved17-12 Listeners as Inner Classes (Part 3 of 6)

© 2006 Pearson Addison-Wesley. All rights reserved17-13 Listeners as Inner Classes (Part 4 of 6)

© 2006 Pearson Addison-Wesley. All rights reserved17-14 Listeners as Inner Classes (Part 5 of 6)

© 2006 Pearson Addison-Wesley. All rights reserved17-15 Listeners as Inner Classes (Part 6 of 6)

© 2006 Pearson Addison-Wesley. All rights reserved17-16 Text Fields A text field is an object of the class JTextField –It is displayed as a field that allows the user to enter a single line of text private JTextField name;... name = new JTextField(NUMBER_OF_CHAR); –In the text field above, at least NUMBER_OF_CHAR characters can be visible

© 2006 Pearson Addison-Wesley. All rights reserved17-17 Text Fields There is also a constructor with one additional String parameter for displaying an initial String in the text field JTextField name = new JTextField( "Enter name here.", 30); A Swing GUI can read the text in a text field using the getText method String inputString = name.getText(); The method setText can be used to display a new text string in a text field name.setText("This is some output");

© 2006 Pearson Addison-Wesley. All rights reserved17-18 A Text Field (Part 1 of 7)

© 2006 Pearson Addison-Wesley. All rights reserved17-19 A Text Field (Part 2 of 7)

© 2006 Pearson Addison-Wesley. All rights reserved17-20 A Text Field (Part 3 of 7)

© 2006 Pearson Addison-Wesley. All rights reserved17-21 A Text Field (Part 4 of 7)

© 2006 Pearson Addison-Wesley. All rights reserved17-22 A Text Field (Part 5 of 7)

© 2006 Pearson Addison-Wesley. All rights reserved17-23 A Text Field (Part 6 of 7)

© 2006 Pearson Addison-Wesley. All rights reserved17-24 A Text Field (Part 7 of 7)

© 2006 Pearson Addison-Wesley. All rights reserved17-25 Text Areas A text area is an object of the class JTextArea –It is the same as a text field, except that it allows multiple lines –Two parameters to the JTextArea constructor specify the minimum number of lines, and the minimum number of characters per line that are guaranteed to be visible JTextArea theText = new JTextArea(5,20); –Another constructor has one addition String parameter for the string initially displayed in the text area JTextArea theText = new JTextArea( "Enter\ntext here." 5, 20);

© 2006 Pearson Addison-Wesley. All rights reserved17-26 Text Areas The line-wrapping policy for a JTextArea can be set using the method setLineWrap –The method takes one boolean type argument –If the argument is true, then any additional characters at the end of a line will appear on the following line of the text area –If the argument is false, the extra characters will remain on the same line and not be visible theText.setLineWrap(true);

© 2006 Pearson Addison-Wesley. All rights reserved17-27 Text Fields and Text Areas A JTextField or JTextArea can be set so that it can not be changed by the user theText.setEditable(false); –This will set theText so that it can only be edited by the GUI program, not the user –To reverse this, use true instead (this is the default) theText.setEditable(true);

© 2006 Pearson Addison-Wesley. All rights reserved17-28 Tip: Labeling a Text Field In order to label one or more text fields: –Use an object of the class JLabel –Place the text field(s) and label(s) in a JPanel –Treat the JPanel as a single component

© 2006 Pearson Addison-Wesley. All rights reserved17-29 Numbers of Characters Per Line The number of characters per line for a JTextField or JTextArea object is the number of em spaces An em space is the space needed to hold one uppercase letter M –The letter M is the widest letter in the alphabet –A line specified to hold 20 M 's will almost always be able to hold more than 20 characters

© 2006 Pearson Addison-Wesley. All rights reserved17-30 Tip: Inputting and Outputting Numbers When attempting to input numbers from any Swing GUI, input text must be converted to numbers –If the user enters the number 42 in a JTextField, the program receives the string "42" and must convert it to the integer 42 The same thing is true when attempting to output a number –In order to output the number 42, it must first be converted to the string "42"

© 2006 Pearson Addison-Wesley. All rights reserved17-31 The Class JTextComponent Both JTextField and JTextArea are derived classes of the abstract class JTextComponent Most of their methods are inherited from JTextComponent and have the same meanings –Except for some minor redefinitions to account for having just one line or multiple lines

© 2006 Pearson Addison-Wesley. All rights reserved17-32 Some Methods in the Class JTextComponent (Part 1 of 2)

© 2006 Pearson Addison-Wesley. All rights reserved17-33 Some Methods in the Class JTextComponent (Part 2 of 2)

© 2006 Pearson Addison-Wesley. All rights reserved17-34 Uncaught Exceptions In a Swing program, throwing an uncaught exception does not end the GUI –However, it may leave it in an unpredictable state It is always best to catch any exception that is thrown even if all the catch block does is output an error message, or ask the user to reenter some input