?
Solved

Why we've to add a scroll pane into a panel before adding a panel into frame

Posted on 2012-04-12
6
Medium Priority
?
419 Views
Last Modified: 2013-11-24
Hi:

We're writing Java programs with Swing.

In the program, we access a database, get the data, populate a table with the data.

In order to display the table, we add the table into a scroll pane, then add the scroll pane into a panel, and finally add the panel to the frame. It works.

If we added the scroll pane directly to the frame, it didn't work, i.e. the table is not displayed.

Why do we've to use the panel?
Why the data is not displayed if we add the scroll pane directly to the frame?

If you've any idea about this, please help.

Thanks so much,

Somits
0
Comment
Question by:somits
[X]
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
  • 4
  • 2
6 Comments
 
LVL 37

Accepted Solution

by:
zzynx earned 2000 total points
ID: 37844871
>> If we added the scroll pane directly to the frame, it didn't work, i.e. the table is not displayed.
Can you show the code? How did you add it?
I guess you did myFrame.add(myScrollPane);

As is explained eg. here the components added to a frame are referred to as its contents; these are managed by the frame's contentPane. To add a component to a JFrame, we must use its contentPane:

myFrame.getContentPane().add(myScrollPane);  // this way you add it in a FlowLayout

if you want a BorderLayout:

myFrame.getContentPane().setLayout(new BorderLayout());
myFrame.getContentPane().add(myScrollPane, BorderLayout.CENTER);
0
 

Author Comment

by:somits
ID: 37844991
Hi zzynx:

Thanks a lot for your replying.

I attached the code below.

Thanks,

Somits



Here is the code:

//------------- code that display data correctly


table = new JTable(data, columnNames);

            TableColumn col;
            for (int i = 0; i < table.getColumnCount(); i++) {
                  col = table.getColumnModel().getColumn(i);

                  col.setMaxWidth(250);

            } // End of for loop
      
            scrollPane = new JScrollPane( table );
            panel.add(scrollPane);
            frame.getContentPane().add(panel);
            frame.setVisible(true);

//------------- code that does not display anything

table = new JTable(data, columnNames);

            TableColumn col;
            for (int i = 0; i < table.getColumnCount(); i++) {
                  col = table.getColumnModel().getColumn(i);

                  col.setMaxWidth(250);

            } // End of for loop
      
            scrollPane = new JScrollPane( table );            
            frame.getContentPane().add(scrollPane);
            frame.setVisible(true);
0
 
LVL 37

Expert Comment

by:zzynx
ID: 37845074
>> //------------- code that display data correctly
>> ...
>>  panel.add(scrollPane);

Question here is: what is panel? I mean, what layout does it have? That's important. That's why it displays the table correctly I think.

Did you try:
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
0
Industry Leaders: 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!

 
LVL 37

Expert Comment

by:zzynx
ID: 37845086
0
 

Author Comment

by:somits
ID: 37846848
Hi zzynx:

Thanks so much for your help.

You're absolutely right. It displays the data.

Now I understand why the layout is so important in the design & coding java swing app.

Thanks so much.

Somits
0
 
LVL 37

Expert Comment

by:zzynx
ID: 37864416
You're welcome.
Thanx 4 axxepting.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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 …
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:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month10 days, 15 hours left to enroll

770 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