CIS3931 - Intro to JAVA Lecture Notes Set 11 30-June-05 GUI Programming – Assignment 5 Notes.

Slides:



Advertisements
Similar presentations
15 Copyright © 2005, Oracle. All rights reserved. Adding User Interface Components and Event Handling.
Advertisements

2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Java Graphical User Interface (GUI) using Visual Editor in eclipse CSI 1390 – Java Programming Instructor: Saeid Nourian University.
Basic Java – Interface design. Understand: How to use TextPad for Java How to define classes and objects How to create a GUI interface How event-driven.
Graphical User Interfaces (Part IV)
Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
Graphic User Interfaces Layout Managers Event Handling.
F27SB2 Programming Languages
Graphical User Interface Bonus slides Interaction Between Components & Drawing.
Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
Building Applications Dialogs Passing data between windows Validating Input using FocusListeners.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
© The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.
JEditorPane Chapter 18 - Student. (c) 2005 by Elizabeth Sugar Boese JEditorPane creation Declare a JEditorPane component. JEditorPane pane; Check to see.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L24 (Chapter 25) Networking.
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.
Building a Web Browser CS1316: Representing Structure and Behavior.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
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.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Intro to GUIs (Graphical User Interfaces) Section 2.5Intro. to GUIs: a GUI Greeter Section 3.7Graphical/Internet Java: Einstein's Equation.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Using the Netbeans GUI Builder. The Netbeans IDE provides a utility called the GUI Builder that assists you with creating Windows applications. The Netbeans.
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.
Using Text Components JTextComponent JTextTextFieldJEditorPane JTextPaneJPasswordField JTextArea Text Controls Plain Text Areas Styled Text Areas.
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,
1 Streams Files are a computer’s long term memory Need ability for programs to –get information from files –copy information from program variables to.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Canvas and Graphics CS 21a. 9/26/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L17: Canvas.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
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.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 25 – Ticket Information Application Introducing.
Lecture 20 April 13, Review for Exam All Exercises and Sample Code from Lectures Object Based Programming Object Oriented Programming String and.
Graphical User Interfaces (Part 2) 1. View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user.
OOP (Java): GUI Intro/ OOP Objectives – –use an image viewer application to introduce Java's GUI features Semester 2,
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
CS1054: Lecture 21 - Graphical User Interface. Graphical User Interfaces vs. Text User Interface.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Creating Applets. What is an applet? What is an applet? A Java program that runs in a web browser. A Java program that runs in a web browser. An applet.
May 12, 1998CS102-01Lecture 7-3 Building GUIs in Java I CS Lecture 7-3 A picture's worth a thousand words.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
CIS Intro to JAVA Lecture Notes Set July-05 GUI Programming –TextField Action Listeners, JEditorPane action listeners, HTML in a JEditorPane,
Ajmer Singh PGT(IP) JAVA IDE Programming - I. Ajmer Singh PGT(IP) GUI (Graphical User Interface) It is an interface that uses a graphic entities along.
Software Construction LAB 08 Java Programming with SWING GUI Builder.
Creating a GUI Class An example of class design using inheritance and interfaces.
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,
Lesson 28: More on the GUI button, frame and actions.
View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user interface element (the user interface.
MIT AITI 2004 Swing Event Model Lecture 17. The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the.
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
CSC 205 Programming II Lecture 5 AWT - I.
Tutorial: How to Creat a Website.
Creating and Modifying Text part 2
Using Procedures and Exception Handling
Chapter 13: Advanced GUIs and Graphics
PC02 Term 1 Project Basic Messenger. PC02 Term 1 Project Basic Messenger.
The structure of Interactive Software
Java Tutorial – Application Building
A picture's worth a thousand words
Advanced GUIs and Graphics
Graphical User Interface
Presentation transcript:

CIS Intro to JAVA Lecture Notes Set June-05 GUI Programming – Assignment 5 Notes.

In this note set… Various methods you will need for Assignment 5 –JEditorPane functions –Cut / Copy / Paste –Sub-menus –Adding an action listener when a button is created –JFileChooser –JColorChooser

JEditorPane Includes many methods that will make programming Assignment 5 much easier API : ax/swing/JEditorPane.html ax/swing/JEditorPane.html

JEditorPane Understands three different types of text –Text/plain : default type; Produced a wrapped plain text view by default –Text/HTML : Provides HTML V.3.2 support –Text/RTF : Provides limited support of the Rich Text Format Editor type must be set in order to recognize the bottom two.

JEditorPane : Loading content There are several ways to load content (text) into a JEditorPane –setText(String t) : Used to initialize from a String. The string is expected to be in the same format as the editor type. –read(InputStream in, Object desc) : Used to initialize the component from a reader. –setPage(URL page) : Used to initialize the component from a URL.

JEditorPane : setText() Example : //Create the editorPane private JEditorPane editorPane = new JEditorPane(); //Put text into the pane editorPane.setText(“This will be put in the pane”);

JEditorPane : Loading Content Easiest way to load data from a file to an editor pane –Open FileReader –Use read(InputStream in, Object desc) method of JEditorPane to push the text from the FileReader onto the screen. –Note : Object desc will usually just be NULL

JEditorPane : Loading Content Example : //Create editor pane private JEditorPane editorPane = new JEditorPane(); //Open file you want to read from FileReader fileReader = new FileReader (filepathname); //Use the read(InputStream, Object) method editorPane.read(fileReader,null); //Close the FileReader fileReader.close();

JEditorPane : Getting Text getText() : returns a String containing all the text in the current editor pane write(OutputStream) : Writes all the text in the EditorPane to a FileWriter Use this when writing the text to a file. –Open a FileWriter –Use the write(OutputStream) method to send the text to the FileWriter

JEditorPane : Getting Text Example : //Open the FileWriter FileWriter filewriter = new FileWriter(filepathname); //Call the write method editorPane.write(filewriter); //Close the FileWriter filewriter.close();

JTextComponent JEditorPane inherits all methods from JTextComponent copy() paste() cut() selectAll() See ng/text/JTextComponent.html#cut() for more details ng/text/JTextComponent.html#cut()

Sub-menus -See SubMenu.java for example -This example also shows how to create one action listener to handle multiple buttons (useful for when a group of buttons do the same thing)

Adding action listeners as a button is created Example : JMenu menu = new JMenu("File"); menu.add(new JMenuItem("New") { addActionListener (new ActionListener() { public void actionPerformed (ActionEvent event) { editorPane.setText(""); } }); } );

JFileChooser ax/swing/JFileChooser.htmlhttp://java.sun.com/j2se/1.5.0/docs/api/jav ax/swing/JFileChooser.html Provides a simple mechanism for the user to choose a file. Java Tutorial : How To Use File ChoosersHow To Use File Choosers

JFileChooser : Example //Create the JFileChooser JFileChooser filechooser = new JFileChooser("."); //If the user actually chose a file if (filechooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { //Store the file in a String String filepathname = filechooser.getSelectedFile(); }

Using the JFileChooser to open a file and write it to an editor pane //Declare method called openFile public void openFile() { //Create the JFileChooser JFileChooser filechooser = new JFileChooser("."); //Check to see if the user selected a file (hit the “ok” button) if (filechooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { //Get the filename selected by the user String filepathname = filechooser.getSelectedFile(); try { //Open a FileReader with the selected filename FileReader fileReader = new FileReader (filepathname); //Use the reader to put the text in the EditorPane editorPane.read(fileReader,null); //Close the reader fileReader.close(); } catch (IOException exception) { //Report error to user if we could not perform the file read JOptionPane.showMessageDialog(this,exception,"IOException",JOptionPane.ERROR_MESSAGE); }

JColorChooser Example //Create variable of type Color to store color choice Color color; //Create JColorChooser and store return value in the color variable JColorChoose.showDialog(this,”Choose a Font Color”, editorPane.getForeground()); this = tells JAVA to draw the frame as a child of the current JFrame “Choose a Font Color” = title of the window editorPane.getForeground() = get the current foreground color to use as the default color in the JColorChooser (or… use getBackground() if changing background color…)