Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

a Swing trick

 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does anyone know about this 'selection back in view' issue?
C.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right,
Thanks
C.
 
We can fix it! We just need some baling wire, some WD-40, a bit of duct tape and this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic