• 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:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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.
 
For my next trick, I'll need the help of a tiny ad ...
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic