The Artima Developer Community
Sponsored Link

Java Answers Forum
JScrollPane - making it track text

1 reply on 1 page. Most recent reply: Jun 29, 2002 12:51 PM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Tom

Posts: 1
Nickname: tsunami
Registered: Jun, 2002

JScrollPane - making it track text Posted: Jun 28, 2002 1:43 PM
Reply to this message Reply
Advertisement
Have created a JScrollPane with a JTextArea.

Is it possible to have the scroll bar move when new text is appended to the text area ? Ie. so it tracks the text and always shows the last line (the bottom of the text area).


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: JScrollPane - making it track text Posted: Jun 29, 2002 12:51 PM
Reply to this message Reply
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());
}
}

Flat View: This topic has 1 reply on 1 page
Topic: Question abotu Runtime.exec() Previous Topic   Next Topic Topic: Change the Icon of the JFrame

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use