Solved

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

Posted on 2012-04-12
6
414 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
  • 4
  • 2
6 Comments
 
LVL 37

Accepted Solution

by:
zzynx earned 500 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to implement Singleton Design Pattern in Java.

786 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