• Post Reply Bookmark Topic Watch Topic
  • New Topic

JScrollPane: Set Scroll Position

 
Ed Mirsky
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

In java awt I was able to reset the scroll position of a ScrollPane with:

myScrollPane.setScrollPosition(int x, int y);
// Scrolls to the specified position within the child component.

How do I reset the scroll position to the upper left corner of my javax.swing.JScrollPane?

Thank's for any and all help,

Ed
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Call scrollRectToVisible on its content.
 
Ed Mirsky
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ilja,

I wasn't able to get your suggestion to work. My program consists of a custom JPanel called DrawingPanel. I wrap the DrawingPanel in a JScrollPane then add the DrawingPanelto my JPanel window.

I want the user to press a "MoreData" button to update the date in the window. I used the following (see jButtonMoreDataActionPerformed below), but it didn't work. Where did I go wrong?


//..... Button to get a new set of data and reset the window
private void jButtonMoreDataActionPerformed( ActionEvent evt ){
Rectangle visible = jScrollPane.getVisibleRect();
jScrollPane.scrollRectToVisible( visible );

}

// CODE FRAGMENTS TO SHOW THE SETUP OF THE WINDOW

// Create a custom JPanel to draw on
DrawingPanel drawingPanel= new DrawingPanel();

// Add the custom JPanel to a JScrollPane
JScrollPane jScrollPane = new JScrollPane( drawingPanel );

// Add the widgets to the page
JPanel jPanel = new JPanel();
SpringLayout springLayout = new SpringLayout();
jPanel.setLayout(springLayout);

// Add the scroll pane to the page
jPanel.add( jScrollPane );

// Lay out the buttons in one row and as many columns
// as necessary, with 6 pixels of padding all around.
SpringUtilities.makeCompactGrid(
jPanel, // Container
1, 2, // rows, cols
0, 0, // initialX, initialY,
0, 0); // xPad, yPad

// Add the spring layout to the grid bag
// code here

// Custom inner class
public class DrawingPanel extends javax.swing.JPanel
{
public void paintComponent(Graphics g){
super.paintComponent(g);

// stuff
}
}

Thank you for your help,

Ed
 
Ben Zung
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you have jButtonMoreDataActionPerformed method while actionPerformed is already there for you?
Nothing wrong with having it though but did you have someway to invoke this
jButtonMoreDataActionPerformed method when the button gets clicked?
 
Ed Mirsky
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've found the solution to the problem of resetting the JScrollPane view. Use

myJScrollPane.getHorizontalScrollbar().setValue(int Pos);

myJScrollPane.getVerticalScrollbar().setValue(int Pos);
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!