170 likes | 184 Views
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
E N D
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
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
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 http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/text/JTextComponent.html#cut() for more details
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 • 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
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…)