Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
?
421 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
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!

 
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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 …
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

604 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