2

OK, so my basic premise is that I have the following setup (https://i.stack.imgur.com/IsFGJ.png):

Main GUI

And my problem is only in relation to the main image window. All the little black boxes surround POI picked out by a Harris Detector and I want the user to select a subset of the squares to send as the foundation of a training set. As can be seen (bottom right), I can turn the boxes red when they're clicked, and I do this by redrawing onto the image and then changing the top component of the JSplit pane to a new JScrollPane that's a JLabel that encapsulates an image icon.

This works great, but being a new ScrollPane, it jumps back to the top left, which is inconvenient at best. So now, before changing the JScrollPane, I'm storing the current value of the both the Horizontal and Vertical bars and then setting the new ScrollPanes bars to the respective values.

BUT, this only works for one of them. It's so odd. The first one always takes just fine, the second however defaults to 90, regardless of how it's set.

The relevant code segment is:

int scrollHoriz = view_scroll.getHorizontalScrollBar().getValue();
int scrollVerti = view_scroll.getVerticalScrollBar().getValue();
System.out.println("OLD Horizontal Position: " + view_scroll.getHorizontalScrollBar().getValue());
System.out.println("OLD Vertical Position: " + view_scroll.getVerticalScrollBar().getValue());
System.out.println("Horiz: " + scrollHoriz);
System.out.println("Verti: " + scrollVerti + "\n");

view_scroll = new JScrollPane();
view_scroll.getViewport().add(new JLabel(new ImageIcon(theImage)));
view_scroll.getHorizontalScrollBar().setUnitIncrement(12);
view_scroll.getVerticalScrollBar().setUnitIncrement(12);
view_scroll.getViewport().getView().addMouseListener(new TrainingMousePress(this));

view_scroll.getVerticalScrollBar().setValue(scrollVerti);
view_scroll.getHorizontalScrollBar().setValue(scrollHoriz);

System.out.println("NEW Horizontal Position: " + view_scroll.getHorizontalScrollBar().getValue());
System.out.println("NEW Vertical Position: " + view_scroll.getVerticalScrollBar().getValue());
System.out.println("Horiz: " + scrollHoriz);
System.out.println("Verti: " + scrollVerti);

view.remove(view.getTopComponent());
view.setTopComponent(view_scroll);
view.setDividerLocation(600);

return;

Which will print out (on click of any box):

  • OLD Horizontal Position: 216
  • OLD Vertical Position: 360
  • Horiz: 216
  • Verti: 360
  • NEW Horizontal Position: 90
  • NEW Vertical Position: 360
  • Horiz: 216
  • Verti: 360

And if you invert the two setters so:

view_scroll.getHorizontalScrollBar().setValue(scrollHoriz);
view_scroll.getVerticalScrollBar().setValue(scrollVerti);

You get:

  • OLD Horizontal Position: 569
  • OLD Vertical Position: 510
  • Horiz: 569
  • Verti: 510
  • NEW Horizontal Position: 444
  • NEW Vertical Position: 90
  • Horiz: 444
  • Verti: 474

The opposite behaviour, but 90 still gets assigned to the second bar regardless.

Any suggestions would be more than welcome. :)

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). BTW - it is commonly known that if any number contains magic, it is 42. ;) – Andrew Thompson Jun 11 '14 at 04:46

1 Answers1

3

Instead of saving the individual values of the scrollbars, save the position of the viewport:

Point saved = scrollPane.getViewport().getViewPosition();

Then you can restore the position using:

scrollPane.getViewport().setViewPosition( saved );

Also, make sure all the code is executed on the Event Dispatch Thread when you update the GUI.

camickr
  • 321,443
  • 19
  • 166
  • 288