• Advertisement
Sign in to follow this  

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

This topic is 4710 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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);

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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);
}
}


Share this post


Link to post
Share on other sites
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);
}


[/quote]

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();
}
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
use:

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

Share this post


Link to post
Share on other sites
I recall this should work:

m_ScrollPane.getViewport().setViewPosition( new Point(0, 0) );


Haven't tested it though...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement