mira84
asked on
Size of JTable in JScrollPane
Hi,
Visualisation of JTable located in JScrollPane is allways same and to big for my needs. I try to chage size using JViewport 's setExtentSize but no use. How should it be done properly? Is this Layout manager issue? Here is the sample how I tried:
JPanel a = new JPanel();
JTable t = new JTable(0,5);
JScrollPane sp = new JScrollPane(t);
sp.getViewport().setExtent Size(new Dimension(100,100));
a.add(sp);
this.add(a);
Visualisation of JTable located in JScrollPane is allways same and to big for my needs. I try to chage size using JViewport 's setExtentSize but no use. How should it be done properly? Is this Layout manager issue? Here is the sample how I tried:
JPanel a = new JPanel();
JTable t = new JTable(0,5);
JScrollPane sp = new JScrollPane(t);
sp.getViewport().setExtent
a.add(sp);
this.add(a);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I don't think your problem is with the table in the scroll pane, it's more related to the placement of the scroll pane and the layout manager of its parent.
Hi mira84,
The problem is in the Layout Manager of JPanel. By default the Layout Manager of JPanel is FlowLayout. In order to have the desire size of the JScrollPane you should use GridBagLayout in JPanel.
Example:
JPanel pnlTest = new JPanel( new GridBagLayout() ) ;
JTable tblTest = new JTable( 0, 5 ) ;
JScrollPane scrTest = new JScrollPane( tblTest ) ;
pnlTest.add( scrTest, new GridBagConstraints( 0, 0,
1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
GridBagConstraints.NONE,
new Insets( 5, 5, 5, 5 ), 100, 100 ) ) ;
// change the GridBagConstraints to the ideal size you
// want
this.add( pnlTest ) ;
Happy coding,
clim1219
The problem is in the Layout Manager of JPanel. By default the Layout Manager of JPanel is FlowLayout. In order to have the desire size of the JScrollPane you should use GridBagLayout in JPanel.
Example:
JPanel pnlTest = new JPanel( new GridBagLayout() ) ;
JTable tblTest = new JTable( 0, 5 ) ;
JScrollPane scrTest = new JScrollPane( tblTest ) ;
pnlTest.add( scrTest, new GridBagConstraints( 0, 0,
1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
GridBagConstraints.NONE,
new Insets( 5, 5, 5, 5 ), 100, 100 ) ) ;
// change the GridBagConstraints to the ideal size you
// want
this.add( pnlTest ) ;
Happy coding,
clim1219
Hi,
use the method in the JTable class
setPreferredScrollableView PortSize(n ew Dimension(width,height));
I think this will change the size of the JTable.
use the method in the JTable class
setPreferredScrollableView
I think this will change the size of the JTable.