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

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

Embed Size (px)

Citation preview

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

CIS3931 - Intro to JAVA

Lecture Notes Set 1130-June-05

GUI Programming – Assignment 5 Notes.

Page 2: CIS3931 - Intro to JAVA Lecture Notes Set 11 30-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

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

JEditorPane

• Includes many methods that will make programming Assignment 5 much easier

• API : http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JEditorPane.html

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

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.

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

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.

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

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”);

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

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

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

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();

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

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

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

JEditorPane : Getting Text

Example :

//Open the FileWriter

FileWriter filewriter = new FileWriter(filepathname);

//Call the write method

editorPane.write(filewriter);

//Close the FileWriter

filewriter.close();

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

JTextComponent

• JEditorPane inherits all methods from JTextComponent

• copy()• paste()• cut()• selectAll()• See

http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/text/JTextComponent.html#cut() for more details

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

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)

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

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(""); } }); }});

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

JFileChooser

• http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JFileChooser.html

• Provides a simple mechanism for the user to choose a file.

• Java Tutorial : How To Use File Choosers

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

JFileChooser : Example

//Create the JFileChooserJFileChooser filechooser = new JFileChooser(".");

//If the user actually chose a fileif (filechooser.showOpenDialog(this) ==

JFileChooser.APPROVE_OPTION) { //Store the file in a String String filepathname = filechooser.getSelectedFile(); }

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

Using the JFileChooser to open a file and write it to an editor pane

//Declare method called openFilepublic 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); } } }

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

JColorChooser Example

//Create variable of type Color to store color choice

Color color;//Create JColorChooser and store return value in the

color variableJColorChoose.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…)