• 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

JScrollPane: Set Scroll Position

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Call scrollRectToVisible on its content.
 
Ed Mirsky
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic