[java] How do I make JScrollPane scroll to the top

Started by
5 comments, last by Kristafarii 18 years, 10 months ago
Easy question this one... I've got a JScrollPane that contains a JTextArea object. The text area is larger than the scroll pane so vertical scroll bars appear. When I display the scroll pane it is defaulting to being scrolled to the bottom of the pane and not the top. How do I get the pane to display scrolled to the top. This is what I'm trying but its not working... JScrollPane scrollPane = new JScrollPane(textArea); scrollPane.getVerticalScrollBar().setValue(0); // this doesn't work getContentPane().add(scrollPane, BorderLayout.CENTER);
Advertisement
scrollPane.getVerticalScrollBar().setValue(0); // this doesn't work

That line needs to be called after the JTextarea is filled with its text. Also all components will have to have been displayed.

After setting the value, you may also have to call repaint on the JScrollPane.
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
It still doesn't seem to be working. Here's what I've got so far. Note that after the object has been created it's setVisible method is called from outside the class ie. scrollPane.setVisible().
class RulesView extends JFrame implements ActionListener{  // -- DATA MEMBERS --  private static final int DEFAULT_WIDTH = 500;  private static final int DEFAULT_HEIGHT = 300;  private JButton m_btnOk = new JButton("Ok");      // ok btn  private JScrollPane m_ScrollPane;  // -- CLASS METHODS --  // ---------------------------------------------------------------------  // CONSTRUCTOR  public RulesView()  {    super("Game Rules");    // set text area    JTextArea textArea = new JTextArea(getText());    textArea.setLineWrap(true);       // set text flow    textArea.setWrapStyleWord(true);  // wrap at word boundarys    textArea.setEditable(false);      // set to non editable    textArea.setOpaque(false);        // transparent bg    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);                       // set dimensions    setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);   // dispose of child window on close    getContentPane().setLayout(new BorderLayout(5,5));              // use border layout    // display text in center    m_ScrollPane = new JScrollPane(textArea);    getContentPane().add(m_ScrollPane, BorderLayout.CENTER);    // add action listener to ok button    m_btnOk.addActionListener(this);    // display button in south    getContentPane().add(m_btnOk, BorderLayout.SOUTH);  }  // ---------------------------------------------------------------------  // RETURNS: text for Rules  private String getText()  {    return "Multiple lines of text.....";  }  // ---------------------------------------------------------------------  // Actionlistener  public void actionPerformed(ActionEvent e)  {    setVisible(false);  }  // ---------------------------------------------------------------------  // Overrides set visible to allow scroll bars to be scrolled to top  public void setVisible(boolean visible)  {    if (visible == true)    {      m_ScrollPane.getVerticalScrollBar().setValue(0);    // scroll bar to top      m_ScrollPane.repaint();    }    super.setVisible(visible);  }}
Original post by Kristafarii
  public void setVisible(boolean visible)  {    if (visible == true)    {      m_ScrollPane.getVerticalScrollBar().setValue(0);    // scroll bar to top      m_ScrollPane.repaint();    }    super.setVisible(visible);  }



This won't work. You are still trying to set the value to 0 before the JFrame is displayed.

Try:
  public void setVisible(boolean visible) {    super.setVisible(visible);    if (visible == true) {      m_ScrollPane.getVerticalScrollBar().setValue(0);    // scroll bar to top      m_ScrollPane.repaint();    }  }
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
use:

textArea.setSelectionStart(0);
textArea.setSelectionEnd(0);
I recall this should work:
m_ScrollPane.getViewport().setViewPosition( new Point(0, 0) );

Haven't tested it though...
thanks Anon

textArea.setSelectionStart(0);
textArea.setSelectionEnd(0);

in the constructor worked..

cheers

This topic is closed to new replies.

Advertisement