Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to set the initial position of a vertical scrollbar in a JScrollPane?  RSS feed

 
Yingie Pitts
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below is how I created a JScrollPane.
...
JTextArea reasonText = new JTextArea();
reasonText.setText(....)
JSrollPane reasonScroller = new JScrollPane(reasonText, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
...
After I run the program, the position of the vertical scroller is always at the end of the textarea. But I want it to be at the begining of the textarea. Does anyone know how to do it?
Any insights would be appreciated.
 
Jon Dornback
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
reasonScroller.getVerticalScrollBar().setValue(reasonScroller.getVerticalScrollBar().getMinimum())
or with aptly named variables:
JScrollBar scrollbar = reasonScroller.getVerticalScrollBar();
int min = scrollbar.getMinimum();
scrollbar.setValue(min)
 
Yingie Pitts
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Jon. It is a very correct way to position the scrollbar, however it did not work for me. I tried to change the JTextArea to TextArea then the scrollbar was shown at the beginning. But I don't want to use TextArea here.
Are there any other ways to handle a scrollbar's position in a JScrollPane?
 
Abhik Sarkar
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
You could try...

Hope this helps,
Abhik.
[ July 24, 2003: Message edited by: Abhik Sarkar ]
 
VIJAY Yadlapati
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your problem is to make Ist line of JTextArea visible, try any of the following.
1) textArea.setSelectionStart(0);
textArea.setSelectionEnd(0);
2) textArea.setText("...");
textArea.requestFocus();
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_HOME);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_HOME);
 
Yingie Pitts
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
setCaretPosition(0) or textArea.requestFocus() both works.
Thanks everyone.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!