Solved

Size of JTable in JScrollPane

Posted on 2001-06-04
4
4,383 Views
Last Modified: 2012-06-27
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);
0
Comment
Question by:mira84
4 Comments
 
LVL 1

Accepted Solution

by:
boazbl1 earned 50 total points
ID: 6151939
the JTable will fill all of the JScrollPane. try to change the size of the JScrollPane using Layout Manager.
0
 
LVL 92

Expert Comment

by:objects
ID: 6154364
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.
0
 
LVL 1

Expert Comment

by:clim1219
ID: 6154743
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
0
 

Expert Comment

by:YamunaiThuraivan
ID: 6170353
Hi,
        use the method in the JTable class
           setPreferredScrollableViewPortSize(new Dimension(width,height));
        I think this will change the size of the JTable.

0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now