Chapter 7-2 (Book Chapter 14)

Slides:



Advertisements
Similar presentations
F27SB2 Programming Languages
Advertisements

Problem Solving 6 GUIs and Event Handling ICS-201 Introduction to Computing II Semester 071.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 : Event-Driven Programming and GUI Objects.
IEEM 110 Computing in Industrial Applications Basic User Interface in Java.
Chapter 7 Event-Driven Programming and Basic GUI Objects.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 14 GUI and Event-Driven Programming.
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.
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.
Introduction to GUI Java offers a great number of pre-defined classes to support the development of graphical user interfaces –These are broken down into.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
Applets and Frames CS 21a: Introduction to Computing I First Semester,
CSE 219 Computer Science III Graphical User Interface.
Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog.
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.
עקרונות תכנות מונחה עצמים תרגול 4 - GUI. Outline  Introduction to GUI  Swing  Basic components  Event handling.
GUI programming Graphical user interface-based programming.
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
Graphical User Interface in Java
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
GUI Clients 1 Enterprise Applications CE00465-M Clients with Graphical User Interfaces.
Even-Driven Programming and basic Graphical User Interface.
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.
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.
1 Class 6. 2 Objectives Objectives Enable your applications to perform actions in response to JButton clicks Get the text the user enters in a textfield.
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.
DCS2133 Object Oriented Programming Graphical User Interface & Event-Driven Programming.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
A simple swing example GETTING STARTED WITH WIND CHILL.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7-2 ( Book Chapter 14) GUI and Event-Driven Programming.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 4 – Completing the Inventory Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 5.1 Test-Driving the Enhanced Inventory Application.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 17.1 Test-Driving the Student Grades Application.
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.
Frame Windows Application program, not applet Construct and show frame JFrame frame = new JFrame(); *** frame.show(); *** Set default close operation..
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
A Quick Java Swing Tutorial
Multiple buttons and action calls
GUIs and Events Rick Mercer.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
Chapter 8 Event Handling.
CompSci 230 S Programming Techniques
Web Design & Development Lecture 11
A First Look at GUI Applications
Graphical User Interface (pronounced "gooey")
A Quick Java Swing Tutorial
CSE 114 – Computer Science I Event Programming
Ellen Walker Hiram College
Chapter 13: Advanced GUIs and Graphics
Graphical user interface-based programming
Chapter 7-3 (Book Chapter 14)
Chapter 7-2 (Book Chapter 14)
Chapter 7 (Book Chapter 14)
A Quick Java Swing Tutorial
Chapter 5 Processing Input with Applets
GUI and Event-Driven Programming
Graphical User Interface
Presentation transcript:

Chapter 7-2 (Book Chapter 14) Introduction to OOP with Java 4th Ed, C. Thomas Wu Chapter 7-2 (Book Chapter 14) GUI and Event-Driven Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. © The McGraw-Hill Companies, Inc.

GUI Classes for Handling Text Intro to OOP with Java, C. Thomas Wu GUI Classes for Handling Text The Swing GUI classes JLabel, JTextField, and JTextArea deal with text. A JLabel object displays uneditable text (or image). A JTextField object allows the user to enter a single line of text. A JTextArea object allows the user to enter multiple lines of text. It can also be used for displaying multiple lines of uneditable text. Notice that JLabel objects are not limited to text. Using them is actually the easiest and quickest way to display an image. JTextArea has dual pursposes. It can be used to display noneditable multi-line text, similar to using multiple JLabel objects. It can also be used to allow the user to enter multiple lines of text, similar to using multiple JTextField objects. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ©The McGraw-Hill Companies, Inc.

Intro to OOP with Java, C. Thomas Wu JTextField We use a JTextField object to accept a single line to text from a user. An action event is generated when the user presses the ENTER key. The getText method of JTextField is used to retrieve the text that the user entered. JTextField input = new JTextField( ); contentPane.add(input); The getText method is most commonly used inside the actionPerformed method of an action listener. input.addActionListener(eventListener); ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ©The McGraw-Hill Companies, Inc.

JTextField We want to write a program that displays the text of the clicked button (OK or CANCEL), or the text entered in the text field ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

JTextField import javax.swing.*; import java.awt.*; import java.awt.event.*; class Ch14TextFrame1 extends JFrame implements ActionListener { private static final int FRAME_WIDTH = 300; private static final int FRAME_HEIGHT = 200; private static final int FRAME_X_ORIGIN = 150; private static final int FRAME_Y_ORIGIN = 250; private JButton cancelButton; private JButton okButton; private JTextField inputLine; public static void main(String[] args) { Ch14TextFrame1 frame = new Ch14TextFrame1(); frame.setVisible(true); } public Ch14TextFrame1() { Container contentPane; setSize (FRAME_WIDTH, FRAME_HEIGHT); setResizable (false); setTitle ("Program Ch14TextFrame1"); setLocation (FRAME_X_ORIGIN, FRAME_Y_ORIGIN); contentPane = getContentPane( ); contentPane.setLayout(new FlowLayout()); ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

JTextField inputLine = new JTextField(); inputLine.setColumns(22); inputLine.setFont(new Font("Courier", Font.PLAIN, 14)); contentPane.add(inputLine); inputLine.addActionListener(this); okButton = new JButton("OK"); contentPane.add(okButton); cancelButton = new JButton("CANCEL"); contentPane.add(cancelButton); //register this frame as an action listener of the two buttons cancelButton.addActionListener(this); okButton.addActionListener(this); //register 'Exit upon closing' as a default close operation setDefaultCloseOperation( EXIT_ON_CLOSE ); } ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

JTextField public void actionPerformed(ActionEvent event) { if (event.getSource() instanceof JButton) { JButton clickedButton = (JButton) event.getSource(); String buttonText = clickedButton.getText(); setTitle("You clicked " + buttonText); } else { //the event source is inputLine setTitle("You entered '" + inputLine.getText() + "'"); } ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Intro to OOP with Java, C. Thomas Wu JLabel We use a JLabel object to display a label. A label can be a text or an image. When creating an image label, we pass ImageIcon object instead of a string. JLabel textLabel = new JLabel(); textLabel.setText("Please enter your name"); textLabel.setSize(150,25); contentPane.add(textLabel); //we can also set the text at the time of object creation. JLabel Prompt = new JLabel ("Please enter your age"); ... A JLabel is strictly for displaying noneditable text or image. A JLabel object does not generate any events. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ©The McGraw-Hill Companies, Inc.

Intro to OOP with Java, C. Thomas Wu JLabel with an Image JLabel imgLabel = new JLabel(new ImageIcon("cat.gif")); contentPane.add(imgLabel); JLabel (with a text) JLabel (with an image) JTextField The sample Ch7TextFrame2 class includes five GUI components: two JLabel objects, one JTextField object, and two JButton objects. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ©The McGraw-Hill Companies, Inc.

Intro to OOP with Java, C. Thomas Wu JTextArea We use a JTextArea object to display or allow the user to enter multiple lines of text. The setText method assigns the text to a JTextArea, replacing the current content. The append method appends the text to the current text. JTextArea textArea = new JTextArea( ); . . . textArea.setText("Hello\n"); textArea.append("the lost "); textArea.append("world"); Hello the lost world Notice how the append method works. If you want to place the text to be appended on a new line, you have ouput the newline control character \n at the appropriate point. JTextArea ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ©The McGraw-Hill Companies, Inc.

JTextArea We want to create a frame with a JTextField and a JTextArea When the user enters a text in the JTextField, and then presses Enter or clicks the ADD button,the entered text will be appended to the JTextArea When the user clicks CLEAR the text in the JTextArea will be cleared JTextArea JTextField ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

JTextArea import javax.swing.*; import java.awt.*; import java.awt.event.*; class Ch14TextFrame3 extends JFrame implements ActionListener { private static final int FRAME_WIDTH = 300; private static final int FRAME_HEIGHT = 250; private static final int FRAME_X_ORIGIN = 150; private static final int FRAME_Y_ORIGIN = 250; private static final String EMPTY_STRING = ""; private static final String NEWLINE = "\n"; private JButton clearButton; private JButton addButton; private JTextField inputLine; private JTextArea textArea; public static void main(String[] args) { Ch14TextFrame3 frame = new Ch14TextFrame3(); frame.setVisible(true); } ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

JTextArea public Ch14TextFrame3() { Container contentPane; setSize (FRAME_WIDTH, FRAME_HEIGHT); setResizable (false); setTitle ("Program Ch14TextFrame3"); setLocation (FRAME_X_ORIGIN, FRAME_Y_ORIGIN); contentPane = getContentPane( ); contentPane.setLayout(new FlowLayout()); textArea = new JTextArea(); textArea.setColumns(22); textArea.setRows(8); textArea.setBorder(BorderFactory.createLineBorder(Color.red)); textArea.setEditable(false); contentPane.add(textArea); ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

JTextArea inputLine = new JTextField(); inputLine.setColumns(22); contentPane.add(inputLine); inputLine.addActionListener(this); //create and place two buttons on the frame addButton = new JButton("ADD"); contentPane.add(addButton); addButton.addActionListener(this); clearButton = new JButton("CLEAR"); contentPane.add(clearButton); clearButton.addActionListener(this); //register 'Exit upon closing' as a default close operation setDefaultCloseOperation( EXIT_ON_CLOSE ); } ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

JTextArea public void actionPerformed(ActionEvent event) { if (event.getSource() instanceof JButton) { JButton clickedButton = (JButton) event.getSource(); if (clickedButton == addButton) { addText(inputLine.getText()); } else { clearText( ); } } else { //the event source is inputLine addText(inputLine.getText()); } } //------------------------------------------------------- private void addText(String userInput) { textArea.append(userInput + NEWLINE); inputLine.setText(EMPTY_STRING); } //--------------------------------------------------------- private void clearText( ) { textArea.setText(EMPTY_STRING); inputLine.setText(EMPTY_STRING); } }

Intro to OOP with Java, C. Thomas Wu Ch14TextFrame3 The state of a Ch14TextFrame3 window after six words are entered. Run the program and explore it. This frame has one JTextField object and one JTextArea object. Every time a text is entered in the text field and the ENTER key is pressed or the ADD button is clicked, the entered text is append to the text area. The border of red color is adorned to the text area to demarcate the region. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ©The McGraw-Hill Companies, Inc.

Adding Scroll Bars to JTextArea Intro to OOP with Java, C. Thomas Wu Adding Scroll Bars to JTextArea By default a JTextArea does not have any scroll bars. To add scroll bars, we place a JTextArea in a JScrollPane object. JTextArea textArea = new JTextArea(); . . . JScrollPane scrollText = new JScrollPane(textArea); contentPane.add(scrollText); Here's a sample to wrap the text area with scroll bars. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ©The McGraw-Hill Companies, Inc.

Ch14TextFrame3 with Scroll Bars Intro to OOP with Java, C. Thomas Wu Ch14TextFrame3 with Scroll Bars A sample Ch14TextFrame3 window when a JScrollPane is used. Initially, there will be no scroll bars. Run the program and confirm this behavior. When the displayed text goes beyond the horizontal or vertical boundary, the corresponding scroll bar appears. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ©The McGraw-Hill Companies, Inc.