newbie scrollbar question

L

Larry.Martell

I have a JTextArea wrapped in a JScrollPane, and when I append a line
to the text area
I want the scroll bar to adjust so that the new line is seen in the
viewport. I have
some code that seems to work most of the time, but every once in a
while it does
not work properly. I think I'm probably not doing this the right way.
Here's a code
snippet that shows what I'm doing:

JPanel consolePane = new JPanel();
JTextArea consoleTextArea = new JTextArea(35, 65);
JScrollPane consoleScrollPane = new JScrollPane(consoleTextArea);
JScrollBar vsb = consoleScrollPane.getVerticalScrollBar();

When I want to update the text I do this:

consoleTextArea.append(line);
consoleTextArea.append("\n");
vsb.setMaximum(vsb.getMaximum()+1);
vsb.setValue(vsb.getMaximum()+1);

Seems like there must be an easier way to do this. Is there some
attribute that
I can set that will make this happen automagically?

TIA!
-larry
 
H

hiwa

I have a JTextArea wrapped in a JScrollPane, and when I append a line
to the text area
I want the scroll bar to adjust so that the new line is seen in the
viewport. I have
some code that seems to work most of the time, but every once in a
while it does
not work properly. I think I'm probably not doing this the right way.
Here's a code
snippet that shows what I'm doing:

JPanel consolePane = new JPanel();
JTextArea consoleTextArea = new JTextArea(35, 65);
JScrollPane consoleScrollPane = new JScrollPane(consoleTextArea);
JScrollBar vsb = consoleScrollPane.getVerticalScrollBar();

When I want to update the text I do this:

consoleTextArea.append(line);
consoleTextArea.append("\n");
vsb.setMaximum(vsb.getMaximum()+1);
vsb.setValue(vsb.getMaximum()+1);

Seems like there must be an easier way to do this. Is there some
attribute that
I can set that will make this happen automagically?

TIA!
-larry

Forget accessing the scrollbar. Use setCaretPosition() of the
JTextArea.
 
C

cddcdd

I have a JTextArea wrapped in a JScrollPane, and when I append a line
to the text area
I want the scroll bar to adjust so that the new line is seen in the
viewport. I have
some code that seems to work most of the time, but every once in a
while it does
not work properly. I think I'm probably not doing this the right way.
Here's a code
snippet that shows what I'm doing:

JPanel consolePane = new JPanel();
JTextArea consoleTextArea = new JTextArea(35, 65);
JScrollPane consoleScrollPane = new JScrollPane(consoleTextArea);
JScrollBar vsb = consoleScrollPane.getVerticalScrollBar();

When I want to update the text I do this:

consoleTextArea.append(line);
consoleTextArea.append("\n");
vsb.setMaximum(vsb.getMaximum()+1);
vsb.setValue(vsb.getMaximum()+1);

Seems like there must be an easier way to do this. Is there some
attribute that
I can set that will make this happen automagically?

TIA!
-larry

Hi, here is an example, maybe you are interested in it.
<code>


package group;

import java.awt.BorderLayout;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

/**
* @author chunfeng.zhu (e-mail address removed)
*
*/
public class ScrollFrameCopy extends javax.swing.JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel jPanel1;
private JScrollPane jScrollPane1;
private JTextArea jTextArea1;

/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
SwingUtilities.invokeLater( new Runnable( ) {
public void run() {
ScrollFrameCopy inst = new ScrollFrameCopy( );
inst.setLocationRelativeTo( null );
inst.setVisible( true );
}
} );
}

public ScrollFrameCopy() {
super( );
initGUI( );
}

private void initGUI() {
try {

setDefaultCloseOperation( WindowConstants.DISPOSE_ON_CLOSE );
{
jPanel1 = new JPanel( );
getContentPane( ).add( jPanel1, BorderLayout.CENTER );
{
jScrollPane1 = new JScrollPane( );
jPanel1.add( jScrollPane1 );
jScrollPane1.setPreferredSize( new
java.awt.Dimension( 74,

91 ) );
{
jTextArea1 = new JTextArea( );
jScrollPane1.setViewportView( jTextArea1 );
jTextArea1.setName( "jTextArea1" );
}
}
}
pack( );
setSize( 400, 300 );
} catch (Exception e) {
e.printStackTrace( );
}
}
}


</code>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top