Solved

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

Posted on 2012-04-12
6
417 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 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
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.

705 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