• Post Reply Bookmark Topic Watch Topic
  • New Topic

Scaling ScrollPane with TextArea breaks ScrollBar

 
Carey Brown
Bartender
Posts: 2231
27
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After acquiring a new laptop with a high-def (2560x1440) screen, all of my own Java programs display too small. I was trying to implement a "scaling" class. It mostly works but in this case, a JScrollPane with a JTextArea, it seems to scale properly but in the process the slider in the JScrollBar is no longer visible. Any ideas? I've tried things like "validate()" but it doesn't help.



 
Rob Spoor
Sheriff
Posts: 20817
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is in the preferred size. That's what is used to determine whether or not to display scrollbars. I've added a printout to see what the preferred size is at the time SimpleScale.setPreferredSize is called, and for the JTextArea it's 0x17. If you scale that by 1.8 you get 0x31, which still does not require scrollbars. But here's the problem - once you call setPreferredSize on a component, that size will be returned, no matter what happens to the component. So even if you add a million rows, the preferred size remains 0x17. What you would want is to use scale the preferred size every time the component's preferred size is called. However, without overriding every single component, that's not really easy.

What you could probably do is add one or more listeners to the component. Once these get triggered, you reset the preferred size to null, which leads to the component calculating its own preferred size. Then you scale the preferred size again. The question is which listeners you should use, because you want to make this code generic. Whichever you use you should be careful, because changing anything in your code will also call the listeners.

Some candidates you could investigate: PropertyChangeListener, ComponentListener, ContainerListener.


I've also thought about perhaps using a scaled Graphics object, but there are at least two issues with this:
1) The Graphics object passed to the painting methods isn't under your control except if you do custom painting in the parent container, which you may not control.
2) I don't think events work well with scaled Graphics.
 
Carey Brown
Bartender
Posts: 2231
27
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob. I commented out line 85 of SimpleScale and it worked!
 
Rob Spoor
Sheriff
Posts: 20817
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!