newbie scrollbar question  
Author Message
Larry.Martell@gmail.com





PostPosted: 2007-12-10 5:55:00 Top

java-programmer, newbie scrollbar question 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
 
hiwa





PostPosted: 2007-12-10 8:23:00 Top

java-programmer >> newbie scrollbar question On Dec 10, 6:54 am, "email***@***.com"
<email***@***.com> wrote:
> 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.
 
cddcdd





PostPosted: 2007-12-10 20:53:00 Top

java-programmer >> newbie scrollbar question On Dec 10, 5:54 am, "email***@***.com"
<email***@***.com> wrote:
> 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 email***@***.com
*
*/
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>
 
 
Larry.Martell@gmail.com





PostPosted: 2007-12-11 3:01:00 Top

java-programmer >> newbie scrollbar question On Dec 9, 5:23 pm, hiwa <email***@***.com> wrote:
> On Dec 10, 6:54 am, "email***@***.com"
>
>
>
> <email***@***.com> wrote:
> > 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.

Thanks - that works much better.