Link to home
Start Free TrialLog in
Avatar of mira84
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().setExtentSize(new Dimension(100,100));

a.add(sp);
this.add(a);
ASKER CERTIFIED SOLUTION
Avatar of boazbl1
boazbl1

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mick Barry
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.
Avatar of clim1219
clim1219

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
Hi,
        use the method in the JTable class
           setPreferredScrollableViewPortSize(new Dimension(width,height));
        I think this will change the size of the JTable.