• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem with setting scrollbar value

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When explicitly setting the scrollBar you have to be careful to observe the constraints outlined in the discussion section of the BoundedRangeModel interface api. It's a bit tricky till you get the hang of it.

Of course there's nothing wrong with doing all the work but if you can let java do some of it there is an easier approach which may suit your needs. Override the 'getPreferredSize' method in your graphic component (drawingPanel) and return the Dimension with which you want the component to be displayed. The JScrollPane will ask the method for this each time you tell the component to 'revalidate' itself. The JScrollPane will then do the heavy lifting and set the scrollBar on its own.
 
reply
    Bookmark Topic Watch Topic
  • New Topic