Use jbutton to scroll through a jscrollpane
Howdy, I'm currently writing a java program which replicates a GPS, on the points of interest screen for Airports, Restaurants etc I have each entry set as a separate jbutton, organized using a gridlayout (1 column listing all entries) and finally the user is able to scroll through the entries (jscrollpanel). I have 2 arrow buttons for up/down and I want to use them to scroll through the list of entries. Is this possible?
Cheers
Josh
Re: Use jbutton to scroll through a jscrollpane
So you have a row of JButtons you want to scroll through? What happens when you add them all to a JPanel, then add the JPanel to a JScrollPane?
Recommended reading: How to Use Scroll Panes (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
Re: Use jbutton to scroll through a jscrollpane
Quote:
Originally Posted by
KevinWorkman
Sorry, I didn't word my question very well. The jButtons have already been added to a jPanel, and the jPanel to the jScrollpane. I have two separate jButtons to the right of this jScrollpane which I want to use to scroll the jScrollpane up/down, is this possible?
Re: Use jbutton to scroll through a jscrollpane
Get the appropriate JScrollBar of the JScrollPane, and set its value based upon its current value.
In pseudo-code that can be implemented in a JButton action listener:
Code :
myScrollPane.getVerticalScrollBar().setValue( myScrollPane.getVerticalScrollBar().getValue() + myScrollPane.getVerticalScrollBar().getUnitIncrement());
Re: Use jbutton to scroll through a jscrollpane
Thanks, that helped heaps. I got it to work by doing this:
Code :
JButton upButton = new JButton("Up");
upButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
scrollPane.getVerticalScrollBar().setBlockIncrement(50);
scrollPane.transferFocusDownCycle();
scrollPane.getVerticalScrollBar().setValue( scrollPane.getVerticalScrollBar().getValue() - scrollPane.getVerticalScrollBar().getBlockIncrement());
}
});
JButton downButton = new JButton("Down");
downButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
scrollPane.getVerticalScrollBar().setBlockIncrement(50);
scrollPane.transferFocusDownCycle();
scrollPane.getVerticalScrollBar().setValue( scrollPane.getVerticalScrollBar().getValue() + scrollPane.getVerticalScrollBar().getBlockIncrement());
}
});