0

I have a JTextArea wrapped in a JScrollPane, which I use to log my application's output. I'm using the default, plain font with a size of 9 for the text area, and the scroll pane's height is 48 px. This results in an even distribution of lines in the scroll pane view, but there's a problem: if you scroll all the way up or all the way down, this happens:

enter image description here

As you can see, the top line got cut off, which is why I'm wondering if there's a way to limit the scroll pane's scroll range so it, for example, can't reach the top or bottom 6 pixels. Alternative solutions are also welcome.

mKorbel
  • 108,320
  • 17
  • 126
  • 296
Konstantin
  • 2,285
  • 3
  • 19
  • 23
  • suspect something wrong with your layout (never-ever even _think_ "px") – kleopatra Nov 15 '12 at 14:23
  • @kleopatra Why so? My application isn't resizable, so going with hard coded values rather than arithmetically working them out from the JFrame's size is much easier. – Konstantin Nov 21 '12 at 08:08
  • because you (most probably, mere guessing without seeing any code) wouldn't have had the problem if you had used a suitable LayoutManager instead of hard-coding :-) Plus it will break for minor changes (f.i. font, LAF, screen resolution ...) – kleopatra Nov 21 '12 at 10:59
  • Keep in mind that what I'm hard-coding aren't component positions; it's their dimensions. Even if I did use a more suitable layout manager (I'm using a run-of-the-mill `FlowLayout`), component sizes and/or offsets would have to be defined _somewhere_. For a simple, compact application (500×250) that cannot be resized, I made the trade-off of hard-coding the components' dimensions, knowing what that entails. With that being said, I'd love to hear what you think: is there a better / _easier_ way, what should I use and how should I do it? – Konstantin Nov 21 '12 at 18:43

2 Answers2

2

You could change the margin (top/bottom) of your JTextArea by setting a custom Border using the method setBorder inherited from JComponent. The documentation for JComponent suggests the following:

Although technically you can set the border on any object that inherits from JComponent, the look and feel implementation of many standard Swing components doesn't work well with user-set borders. In general, when you want to set a border on a standard Swing component other than JPanel or JLabel, we recommend that you put the component in a JPanel and set the border on the JPanel.

That would yield the same result as limiting the scroll range, while being more straight forward.

EDIT:

OP reported that the following solution worked for him:

textAreaLog.setBorder(BorderFactory.createEmptyBorder(0, 6, 0, 6));
das_weezul
  • 5,682
  • 2
  • 25
  • 31
  • @StanislavL Call me Lucky Luke *pewpew* ;) – das_weezul Nov 15 '12 at 12:48
  • That did the trick — setting the top and bottom insets to 0 with `textAreaLog.setBorder(BorderFactory.createEmptyBorder(0, 6, 0, 6));` worked like a charm. Thanks a bunch, both of you! – Konstantin Nov 15 '12 at 13:10
  • @StanislavL way too hacky .. either on the area itself or on a containing parent, the bordersize is always hard-coded pixels. That'll blow in all but the exact same environment it was developed on (LAF, font, dpi ...) – kleopatra Nov 15 '12 at 13:41
  • @KonstantinĐ: I just added your solution to my answer – das_weezul Nov 15 '12 at 17:05
  • @kleopatra My be better to use public Insets getMargin() of JTextComponent and play with the top/bottom margin. Set them to 0 – StanislavL Nov 16 '12 at 06:22
2

Place the JTextArea in a JPanel with empty borders where top and bottom insets are 6 pixels?

StanislavL
  • 55,186
  • 9
  • 58
  • 88