Solved

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

Posted on 2012-04-12
6
411 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
Comment Utility
>> 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
Comment Utility
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
Comment Utility
>> //------------- 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 37

Expert Comment

by:zzynx
Comment Utility
0
 

Author Comment

by:somits
Comment Utility
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
Comment Utility
You're welcome.
Thanx 4 axxepting.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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…
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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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:

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now