1 / 17

CIS3931 - Intro to JAVA

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

frickm
Download Presentation

CIS3931 - Intro to JAVA

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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

  3. 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

  4. 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.

  5. 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.

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

  7. 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

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

  9. 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

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

  11. 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

  12. 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)

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

  14. 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

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

  16. 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); } } }

  17. 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…)

More Related