0

I have a JScrollPane and when I load my application the bar is sitting on top of one of my buttons. What I would like to do is add some space to the side of my button so that the scroll bar draws over the space and not my button.

Example code that I tried:

JPanel eButton = new JPanel(new BorderLayout());
JPanel spaceFiller = new JPanel();
spaceFiller.setSize(30, 10);
eButton.add(editButton, BorderLayout.EAST);
eButton.add(spaceFiller, BorderLayout.WEST);

The problem with this code is that it still overwrites my button and no space is added. What is the best way to make sure that JScrollPane doesn't overlap the components in my JFrame?

Thanks

Grammin
  • 11,808
  • 22
  • 80
  • 138
  • 1) What `JScrollBar`? 2) For better help sooner, post an [SSCCE](http://pscode.org/sscce.html). 3) Use an `EmptyBorder` instead of the `spaceFiller` `JPanel`. – Andrew Thompson May 19 '11 at 13:29
  • I could ask "what `JScrollPane`?", but instead I'll ask "where's that SSCCE?". – Andrew Thompson May 19 '11 at 13:50
  • If you don't like that URL at *my* domain, try the one at [SSCCE.org](http://http://sscce.org/). It is still the same document written by me, on a domain for which I am the web master, but it is "someone else's site" given that I do not own the domain name. ;) To put that another way, it would be great if you stopped whining, and started productively answering the questions that might help solve the stated problem. But of course, it is not a perfect world. – Andrew Thompson May 19 '11 at 14:35

2 Answers2

2

To ensure that the size of the JPanel is respected you should use setPreferredSize() instead of setSize().

Vincent Ramdhanie
  • 102,349
  • 23
  • 137
  • 192
1

In your sample code, didn't you reverse EAST and WEST? Shouldn't it be like:

eButton.add(editButton, BorderLayout.WEST); 
eButton.add(spaceFiller, BorderLayout.EAST); 

That would make more sense, sicne the scrollbar will appear on the right side (EAST).

Please note that the solution you suggest, even though it may work (after exchanging EAST and WEST) looks more like a hack than a real solution.

jfpoilpret
  • 10,449
  • 2
  • 28
  • 32