Charles Bell
Posts: 519
Nickname: charles
Registered: Feb, 2002
|
|
Re: JScrollPane - making it track text
|
Posted: Jun 29, 2002 12:51 PM
|
|
Look at the println method in the following code. Everytime you need to append a line of text to the TextArea use the method println(String s)
which appends theline of text to the end of the text editor, then sets the caret position to the length of the text in the text area. This causes the scrollpane to scroll down to the end.
/* SimpleFileWriter.java * @author: jbell * @version: Jul 19, 2001 */
import java.awt.event.*; import java.io.*; import javax.swing.*;
public class SimpleFileWriter implements ActionListener{
JFrame frame; JTextArea editor;
/** Instantiates a SimpleFileWriter object. */ public static void main(String[] args){
SimpleFileWriter simplefilewriter = new SimpleFileWriter(); simplefilewriter.init();
}
/** Initializes the button controls, a textarea for an editor to display or enter * text, and adds the components to a frame window and displays it. */ public void init(){ frame = new JFrame("SimpleFileWriter"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //button controls JPanel controlpanel = new JPanel(); JButton openbutton = new JButton("Open A File"); JButton writebutton = new JButton("Write To File"); JButton clearbutton = new JButton("Clear"); JButton exitbutton = new JButton("Exit"); exitbutton.addActionListener(this); openbutton.addActionListener(this); writebutton.addActionListener(this); clearbutton.addActionListener(this); controlpanel.add(writebutton); controlpanel.add(openbutton); controlpanel.add(clearbutton); controlpanel.add(exitbutton); frame.getContentPane().add(controlpanel,"North"); //editor editor = new JTextArea(30,60); //initializes a JTex Editor with width 60 and height 30 JScrollPane scrollpane = new JScrollPane(editor); frame.getContentPane().add(scrollpane,"Center"); frame.pack(); frame.show(); }
/** Brings up a simple FileSave dialog for the user to enter a file name, * and creates a file based on that name in the selected directory. */ public File getAFileForSave(){ File file = null; File currentdirectory = new File("."); JFileChooser filechooser = new JFileChooser(currentdirectory); int replycode = filechooser.showSaveDialog(frame); if (replycode == JFileChooser.APPROVE_OPTION){ file = filechooser.getSelectedFile(); } return file;
}
/** Brings up as imple file open dialog and returns the file selected by the user. */ public File getAFileToOpen(){ File file = null; File currentdirectory = new File("."); JFileChooser filechooser = new JFileChooser(currentdirectory); int replycode = filechooser.showOpenDialog(frame); if (replycode == JFileChooser.APPROVE_OPTION){ file = filechooser.getSelectedFile(); } return file; }
/** Writes the string to the input file. */ public void writeStringToFile(File file, String s){
try{ FileWriter filewriter = new FileWriter(file); filewriter.write(s); filewriter.close(); }catch (FileNotFoundException fnfe){ System.err.println("FileNotFoundException: " + fnfe.getMessage()); }catch (IOException ioe){ System.err.println("IOException: " + ioe.getMessage()); }
}
/** Appends the contents of the input file to the text editor. */ public void writeFileToEditor(File file){
try{ FileReader filereader = new FileReader(file); BufferedReader bufferedreader = new BufferedReader(filereader); String lineread = ""; while ((lineread = bufferedreader.readLine()) != null){ println(lineread); } filereader.close(); }catch (FileNotFoundException fnfe){ System.err.println("FileNotFoundException: " + fnfe.getMessage()); }catch (IOException ioe){ System.err.println("IOException: " + ioe.getMessage()); }
}
/** Provides actions to user input. */ public void actionPerformed(ActionEvent actionevent){ String actioncommand = actionevent.getActionCommand(); if (actioncommand.compareTo("Exit") == 0) { System.exit(0); }else if (actioncommand.compareTo("Clear") == 0) { editor.setText(""); }else if (actioncommand.compareTo("Open A File") == 0) { File f = getAFileToOpen(); writeFileToEditor(f); }else if (actioncommand.compareTo("Write To File") == 0) { String s = editor.getText(); File f = getAFileForSave(); writeStringToFile(f,s); showMessage("Contents of the Text Editor saved to file " + f.getName()); } }
/** Conveniently displays a message to the user and waits for confirmation. */ public void showMessage(String s){ JOptionPane.showMessageDialog(null,s); } private void println(String s){ editor.append(s + "\n"); editor.setCaretPosition(editor.getText().length()); } }
|
|