4

I'm trying to place a component on the corner of the window. However, my window has a scrollbar and the scrollbar is placed on top of the component.

So I'm trying to change the position of the component so that it is adjacent to the scrollbar rather than have it be under it.

Thus, I need the standard width of a scrollbar (vertical) and the standard height of a scrollbar (horizontal).

I could try getVerticalScrollBar.getWidth() at runtime, but unfortunately I need to place the coordinates before I create the scrollbar themselves.

Also, creating an empty JScrollBar and calling getWidth() is returning 0.

Thanks for your help.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
ThePrince
  • 818
  • 2
  • 12
  • 26

3 Answers3

6

Not sure if it helps you right now after a year ago, but:

int scrollBarSize = ((Integer)UIManager.get("ScrollBar.width")).intValue(); I found it here on this discusion: http://www.coderanch.com/t/341287/GUI/java/Calculating-width-JList-vertical-scrollbar

2

Don't get the size or set the size but rather let the layout managers do the work for you. Consider

  • adding the scrollbars by default to the JScrollPane via the setVerticalScrollBarPolicy(...) and setHorizontalScrollBarPolicy(...) method pair so that the layout managers take the scrollbars into consideration when laying everything out from the get go.
  • Avoid null layouts at all costs as this will take away one of the most powerful tools for creating flexible workable Swing GUI's.
  • If these recommendations don't help, then consider creating and posting an sscce.
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
0

I'm not sure about other OS's, but on windows XP it is 17 pixels.
What I would do is temporarily include this line in your program:

System.out.println(jScrollPane.getVerticalScrollBar().getWidth())

then use the value printed.

The Guy with The Hat
  • 10,836
  • 8
  • 57
  • 75