newbie scrollbar question

Discussion in 'Java' started by Larry.Martell@gmail.com, Dec 9, 2007.

  1. Guest

    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
    , Dec 9, 2007
    #1
    1. Advertising

  2. hiwa Guest

    On Dec 10, 6:54 am, ""
    <> 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.
    hiwa, Dec 10, 2007
    #2
    1. Advertising

  3. Guest

    On Dec 10, 5:54 am, ""
    <> 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
    *
    */
    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>
    , Dec 10, 2007
    #3
  4. Guest

    On Dec 9, 5:23 pm, hiwa <> wrote:
    > On Dec 10, 6:54 am, ""
    >
    >
    >
    > <> 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.
    , Dec 10, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    333
  2. Edward H. Fabrega

    Question About Scrollbar

    Edward H. Fabrega, Oct 7, 2004, in forum: Java
    Replies:
    5
    Views:
    544
    Chris Uppal
    Oct 10, 2004
  3. Mike Novak

    MSIE and scrollbar question

    Mike Novak, Jul 3, 2003, in forum: HTML
    Replies:
    1
    Views:
    406
  4. Jeff
    Replies:
    4
    Views:
    14,515
    Neredbojias
    Nov 27, 2005
  5. Alex

    ScrollBar? Does it exist just WEB ScrollBar Control?

    Alex, Apr 1, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    259
    Alvin Bruney [MVP]
    Apr 4, 2004
Loading...

Share This Page