Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

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

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
You could try...

Hope this helps,
Abhik.
[ July 24, 2003: Message edited by: Abhik Sarkar ]
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
setCaretPosition(0) or textArea.requestFocus() both works.
Thanks everyone.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic