Hello,
I am trying to put a JTable into a JScrollPane into a JSplitPane. I am running into an issue, where the JTable seems to pick a very large preferred size for itself. However, this seems to only happen when I put the JTable into a JScrollPane. My JTable has only two rows, yet the initial space allocated for my table is much larger (around 20 or so rows).
Anybody have any ideas as to why JTable is acting like this?? I have included my source code below. Simply change the private static final boolean TABLE_SCROLL to see the different behaviors.
Thanks,
-Yeath
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
public class TableTest extends JFrame
{
private static final boolean TABLE_SCROLL = true;
public TableTest()
{
super( "Table Test" );
this.getContentPane().setLayout( new BorderLayout( 0, 0 ) );
String[] columns = { "Name", "Type", "Selective", "Auto" };
String[][] data = { { "HP Laser Jet", "Image Capable", "Y", "N" },
{ "OkiData", "Dot Matrix", "N", "Y" } };
JTable table = new JTable( data, columns );
Component table_panel = createTablePanel( table );
JPanel bottom_panel = new JPanel();
JSplitPane jsp = new JSplitPane( JSplitPane.VERTICAL_SPLIT,
table_panel, bottom_panel );
this.getContentPane().add( jsp, BorderLayout.CENTER );
}
private Component createTablePanel( JTable table )
{
if( TABLE_SCROLL )
return new JScrollPane( table );
else
{
JPanel panel = new JPanel( new BorderLayout( 0, 0 ) );
panel.add( table, BorderLayout.CENTER );
panel.add( table.getTableHeader(), BorderLayout.PAGE_START );
return panel;
}
}
public static void main( String[] args )
{
TableTest test = new TableTest();
test.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
test.pack();
test.setVisible( true );
}
}