I am having trouble creating a JTable with scrollbars. I want a JTable with 2 columns and no visible scrollbars.
If I enlarge one of the columns the scrollbars should become visible and the columns resize.
I followed this answer How to make JTable both AutoResize and horizontall scrollable? and works fine which basically comes down to:
JTable table = new JTable() {
@Override
public boolean getScrollableTracksViewportWidth() {
return getPreferredSize().width < getParent().getWidth();
}
};
table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );
However, with this solution I cannot shrink the first column. Only if I enlarge the 2nd column and the scrollbars become visible I can shrink the first one.
The required behavior is that the 2 columns are automatically resizable. Meaning that the 1 column can shrink and afterwards extend without the scrollbars popping up. Only when extending one of the columns, so that the view should extend, the scrollbars should pop up.
A scenario:
- Shrink the 1st column -> 2nd one enlarges, no scrollbars
- Enlarge the 1st column -> 2nd one shrinks, still no scrollbars
- Enlarge the 2nd column -> 1 column stays the same, 2nd one enlarges and scrollbars appear
Any ideas on fixing this?
An SSCCE:
import javax.swing.JDialog;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.WindowConstants;
import javax.swing.table.AbstractTableModel;
import java.awt.BorderLayout;
import java.awt.Container;
public class TableTest {
public TableTest() {
JDialog mainDialog = new JDialog();
mainDialog.setResizable( true );
mainDialog.setDefaultCloseOperation( WindowConstants.DISPOSE_ON_CLOSE );
Container contentPane = mainDialog.getContentPane();
JTable myTable = new JTable() {
@Override
public boolean getScrollableTracksViewportWidth() {
return getPreferredSize().width < getParent().getWidth();
}
};
myTable.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );
myTable.setModel( new MyTableModel() );
JScrollPane scrollPane = new JScrollPane( myTable );
contentPane.add( scrollPane, BorderLayout.CENTER );
mainDialog.pack();
mainDialog.setVisible( true );
}
public static void main( String[] args ) {
new TableTest();
}
private class MyTableModel extends AbstractTableModel {
@Override public int getRowCount() {
return 1;
}
@Override public int getColumnCount() {
return 2;
}
@Override public Object getValueAt( int rowIndex, int columnIndex ) {
return "ARandomValue";
}
}
}