Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a Swing trick

 
Charles Dupin
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JTable in a JScrollPane. One of the records of the JTable is selected and I resize (setPreferredSize) the JTable in such a way that the selected record disapears below the bottom of the table, how can I automatically scroll the table to have the selected record back in the viewport.
Thanks
C.
[ March 09, 2003: Message edited by: Charles Dupin ]
 
Charles Dupin
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone know about this 'selection back in view' issue?
C.
 
John Immordino
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm no Swing guru, but I'll take a shot at this one. You'll need to add a Component Listener to the JFrame you're resizing to receive notification of resize events. Within your componentResized() method you'll need code to do the following (or a method call to code that does the following):
Get the selected row from your table.
If the selected row isn't -1, get the rectangle of a cell in the selected row (see getCellRect() n JTable).
Pass that Rectangle to scrollRectToVisible() (JTable inherits from JComponent).
Swing gurus please feel free to correct me or post a better way to do this.
Good Luck,
John
 
Charles Dupin
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks John,
It works and I did it like this:
You implement a ComponentListener, create the JScrollPane of the JTable and the method componentResized of the ComponentListener does the trick.

jscrollpane= new JScrollPane(jtable);
jscrollpane.addComponentListener(this);
public void componentResized(ComponentEvent e) {
jtable.scrollRectToVisible(jtable.getCellRect(jtable.getSelectedRow(), 1 ,true));
}

[ March 10, 2003: Message edited by: Charles Dupin ]
 
John Immordino
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just curious - what happens if you shift-click on the selected row so that there's no selection, then resize?
 
John Immordino
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never mind - answered my own question. No exception thrown if you ask for a Rectangle for row -1 and you pass the Rectangle to scrollRectToVisible().
 
Charles Dupin
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right,
Thanks
C.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic