1

How does a JLabel or JButton notify a JScrollPane that the view size has changed (for example when an icon has been set) so it can determine whether showing scrollbars are necessary?

How could I implement similar behaviour to display an image with a simple JPanel without resorting to the aforementionned components?

P.S: I've looked through the source code and so far all I see is that a Component is referred to as "view" and is passed on to a JView or JViewport which registers some listeners. From there on things seem unclear.

James P.
  • 19,313
  • 27
  • 97
  • 155
  • 2
    for better helpo sooner edit your question with a [SSCCE](http://sscce.org/) – mKorbel Apr 23 '12 at 09:20
  • @mKorbel: Would love to but this would probably require an image file to demonstrate the difference between using setIcon and using paint plus a set of the Component's dimension. – James P. Apr 23 '12 at 09:26
  • 1
    @JamesPoulson Images can be 1) obtained from the JRE 2) hot-linked from the internet 3) generated in code. From memory, techniques 2 & 3 are mentioned in the SSCCE document. – Andrew Thompson Apr 23 '12 at 09:45
  • 2
    [`FauxImage`](http://stackoverflow.com/a/8090328/230513) may be a convenient adjunct to an [sscce](http://sscce.org/). – trashgod Apr 23 '12 at 09:51
  • 1
    @AndrewThompson True. I forgot about getResource and I could always use a color fill. Will see if I can write up some example code. – James P. Apr 23 '12 at 09:52
  • 1
    @trashgod The moment I saw that comment, it reminded me of yet another technique you commonly use - that of implementing an `Icon`. Not sure if that would work for this use-case, but it is another technique to consider. – Andrew Thompson Apr 23 '12 at 09:59
  • 1
    @AndrewThompson: Good point; `extends JLabel/JButton implements Icon` is a way to leverage the text/icon alignment, seen [here](http://stackoverflow.com/a/2834484/230513). – trashgod Apr 23 '12 at 10:13

1 Answers1

4

As noted in the JScrollPane API, unless you change the policy, "both horizontal and vertical scrollbars appear whenever the component's contents are larger than the view." Once pack() has sized the Window "to fit the preferred size and layouts of its subcomponents," any subsequent changes are seen by the scroll pane when the container is validated and repainted. See Painting in AWT and Swing for more.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Ok. I must be missing something here. I've set a JLabel as the viewPortView and attempted to set it's dimensions without setting it's icon. This doesn't seem to be triggering a resize of the view. Is a call to one of the methods you've mentionned needed? When the doc says "both horizontal and vertical scrollbars appear whenever the component's contents are larger than the view." does this mean that a change depends on the contents of the JLabel? – James P. Apr 23 '12 at 09:39
  • 2
    Invoking `revalidate()` and `repaint()` may be appropriate after changing the content. The scroll pane will see the new geometry. – trashgod Apr 23 '12 at 09:57
  • I will test the methods. If they work I suspect that some call is being propagated up from the Icon. – James P. Apr 25 '12 at 12:16
  • 1
    The `setIcon()` method of `AbstractButton` invokes `revalidate()` if the size changes, as well as `repaint()` if the icon itself changes. If you have to call them yourself, something else is awry. – trashgod Apr 25 '12 at 15:05