Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is there a Way to set JScrollPane

Status
Not open for further replies.

brinker

Programmer
May 31, 2001
48
CA
Hello,

I have a pretty easy question, providing that somebody knows the solution. If I have a JScrollPane that contains a JTextArea and JTextArea keeps getting data appended to it. Is there a way (ie. a fxn call) that I can use to automatically scroll to the bottom.

thanks

Brinker
 
This is what I am doing right now, where changeLog is a JTextArea.


changeLog.append("\n"+theFile.toString());
JScrollBar js= statusScrollPane.getVerticalScrollBar();
js.setValue(js.getMaximum());

this generally works, except that the scrollpane does not scroll right to the bottom (ie. leaves a few lines unrevealed).
 
Hi, somehow I don't see it happening in my pc. The JTextArea is automatically scrolled to the button without me having to set the JScrollBar at all (might be due to different JDK version... I am using 1.4). You can try out this program to see if it works or not. Just type in any words into the JTextField and press enter. The word will be automatically appended to the JTextArea.


// JSPDemo.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JSPDemo extends JFrame implements KeyListener
{
JTextArea tarea;
JScrollPane jsp;
JTextField tField;
public JSPDemo()
{
getContentPane().setLayout(null);
setSize(210,260);

tarea = new JTextArea();
jsp = new JScrollPane(tarea);
jsp.setBounds(new Rectangle(0,0,200,200));

tField = new JTextField();
tField.setBounds(new Rectangle(0,205,200,20));

tField.addKeyListener(this);

getContentPane().add(jsp,null);
getContentPane().add(tField,null);
}

public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == 10)
{
tarea.append(tField.getText()+"\n");
tField.setText("");
}
}

public void keyReleased(KeyEvent e)
{
}

public void keyTyped(KeyEvent e)
{
}

public static void main(String[] args)
{
JSPDemo d = new JSPDemo();
d.show();
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top