Size of JTable in JScrollPane

Posted on 2001-06-04
Last Modified: 2012-06-27
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));

Question by:mira84
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

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.
LVL 92

Expert Comment

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.

Expert Comment

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.


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,
    new Insets( 5, 5, 5, 5 ), 100, 100 ) ) ;

// change the GridBagConstraints to the ideal size you
// want

this.add( pnlTest ) ;

Happy coding,

Expert Comment

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


Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
servlet filter example 37 83
servlet example 17 59
Unhandled exception type Exception 18 52
jboss 7.1 start up error 1 60
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

735 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