Hi there,
I'm using a horizontal scrollbar within a JScrollPane and I need to set the value of the scrollbar everytime the user presses a zoom button. However, when the value of the scrollbar is greater than its middle value, despite the fact that I give the scrollbar the correct value, the scrollbar seems to resort to some predefined or stored value and ignore the value I give it. This value appears to be some kind of threshold maximum value that the scrollbar can't exceed for a given zoom level (e.g. at 10x zoom the scrollbar always jumps to the middle of the panel). The problem is I can't work out where it is getting these values from, because at no point in my program am I using them so I thought it must be some sort of system interference. This mainly happens with zooming in (increasing values) but sometimes happens with zooming out (decreasing values), and only when the scrollbar value is above the middle point. I also know it is not due to the value surpassing the max value or panel width as I set these to larger values in the same block of code.
I wonder if anyone is able to offer some advice as to what might be interfering with the scrollbar value and why it might be limiting the value the scrollbar can take in a certain position.
I don't want to paste all my code here as it's very long so I'll just include the relevant bits:
// the scrollpane is created in the constructor
scrollPane = new JScrollPane(drawingPanel);
scrollPane.getHorizontalScrollBar().setValue(0);
scrollPane.getHorizontalScrollBar().setBlockIncrement(500);
scrollPane.getHorizontalScrollBar().setUnitIncrement(20);
scrollPane.getHorizontalScrollBar().setMaximum((clength/zoom)+20);
scrollPane.getHorizontalScrollBar().setMinimum(0);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
scrollPane.setWheelScrollingEnabled(true);
ScrollingHandler scrolling = new ScrollingHandler();
scrollPane.getHorizontalScrollBar().addAdjustmentListener(scrolling);
// an inner class to act when user presses either 'Zoom In' or 'Zoom Out' buttons
class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==zoomButtons[0]) // zoom in
{
if(zoom>10)
{
System.out.println("zoom in button has been pressed...");
int previousZoom = zoom;
zoom-=10;
d2=new Dimension((clength/zoom)+20, drawingPanelHeight);
drawingPanel.setPreferredSize(d2);
scrollPane.getHorizontalScrollBar().setMaximum((clength/zoom)+20);
System.out.println("value 0f scroll bar at " + previousZoom + "x zoom: " + Integer.toString(scrollPane.getHorizontalScrollBar().getValue()));
int currentScrollValue = scrollPane.getHorizontalScrollBar().getValue();
int newScrollValue = (currentScrollValue * previousZoom)/zoom;
System.out.println("new value of scroll bar should be: " + newScrollValue);
System.out.println("max value of scrollbar = " + scrollPane.getHorizontalScrollBar().getMaximum());
scrollPane.getHorizontalScrollBar().setValue(newScrollValue);
System.out.println("value 0f scroll bar at " + zoom + "x zoom: " + Integer.toString(scrollPane.getHorizontalScrollBar().getValue()) + "\n");
drawingPanel.revalidate();
drawingPanel.repaint();
buttonLabel.setText(zoomLabel + zoom);
overviewPanel.revalidate();
overviewPanel.repaint();
}
}
else if(e.getSource()==zoomButtons[1]) // zoom out
{
if(zoom<100)
{
System.out.println("zoom out button has been pressed...");
int previousZoom = zoom;
zoom+=10;
d2=new Dimension((clength/zoom)+20, drawingPanelHeight);
drawingPanel.setPreferredSize(d2);
scrollPane.getHorizontalScrollBar().setMaximum((clength/zoom)+20);
System.out.println("value 0f scroll bar at " + previousZoom + "x zoom: " + Integer.toString(scrollPane.getHorizontalScrollBar().getValue()));
int currentScrollValue = scrollPane.getHorizontalScrollBar().getValue();
int newScrollValue = (currentScrollValue * previousZoom)/zoom;
System.out.println("new value of scroll bar should be: " + newScrollValue);
System.out.println("max value of scrollbar = " + scrollPane.getHorizontalScrollBar().getMaximum());
scrollPane.getHorizontalScrollBar().setValue(newScrollValue);
System.out.println("value 0f scroll bar at " + zoom + "x zoom: " + Integer.toString(scrollPane.getHorizontalScrollBar().getValue()) + "\n");
drawingPanel.revalidate();
drawingPanel.repaint();
buttonLabel.setText(zoomLabel + zoom);
overviewPanel.revalidate();
overviewPanel.repaint();
}
}
}
}
I really appreciate your help, I'm really stuck on this!
