Drag and drop into empty table not working?
This is a known gotcha from the early days of Swing, related to bug id 4310721
The symptom is that while drag and drop works fine into tables which are populated, trying to drop into a table with no rows, or drop data beneath the populated rows, does not work. The cause of this is that the table does not automatically expand to fill the area available to it in the JScrollpane viewport.
As of jdk1.6 this is fixed on JTable itself. You just need to call myJTable.setFillsViewportHeight(true)
If you do not have the great glory of 1.6 available to you, you will have to extend JTable and override the getScrollableTracksViewportHeight() method as below:
public TrackViewportJTable(TableModel tableModel, TableColumnModel tableColumnModel) {
super(tableModel, tableColumnModel);
}
public TrackViewportJTable(TableModel tableModel) {
super(tableModel);
}
public boolean getScrollableTracksViewportHeight() {
// fetch the table's parent
Container viewport = getParent();
// if the parent is not a viewport, calling this isn't useful
if (! (viewport instanceof JViewport)) {
return false;
}
// return true if the table's preferred height is smaller
// than the viewport height, else false
return getPreferredSize().height < viewport.getHeight();
}
}
The above is based on the example in Shannon Hickey’s blog.
For a more complete description of the problem, and its solution, see his blog entry here
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
Leave a Reply