0

I have a JDialog that is being kicked off by a JFrame. This Dialog has a JScrollPane inside of it, with some content that is added before the JDialog's setVisible function is called with 'true'.

My problem is, with the content added, it causes the Scrollbars to change their location when setVisible is called. Since setVisible is blocking, I cannot make any calls to change the value of the scrollbars after setVisible.

I do not want to use invokeLater as its functionality is indeterminate in timing (if there is simply no other way, fine, but I don't like 'do it when you please' if I can avoid it).

Is there a way to ensure a call post-setVisible that does not rely on user interaction without using invokeLater?

Aaron Marcus
  • 153
  • 2
  • 13
  • I'm confused by the pack comment... I'll get a demo up as soon as I can, but rather busy at the moment to split off and create an example. – Aaron Marcus Sep 16 '14 at 18:49

1 Answers1

0

This should work.

private void scrollTo(final JComponent viewToScroll, final Rectangle bounds) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            viewToScroll.scrollRectToVisible(bounds);
        }
    });
}

However, if you are prefer to tweak with the execution order and timing a bit you could listen to when the viewToScroll component is added to the parent hierarchy (HierarchyListener.hierarchyChanged) then call the above method (then remove the listener).

javajon
  • 1,660
  • 16
  • 18
  • Thanks for the post, I know how to get at it using invokeLater. Using that call, I can just directly set the value of the scrollbar to 0 and it works just fine. However, I dislike using invokeLater if I can avoid it (indeterminate execution annoys me) and was hoping to find a way to do this without using invokeLater. – Aaron Marcus Sep 16 '14 at 18:48
  • Completely spaced out on your second paragraph when I wrote that. is the viewToScroll component the execution that causes the final adjustment of the scrollbar in a setVisible? – Aaron Marcus Sep 16 '14 at 18:52