Solved

JScrollPane and JPanel

Posted on 2004-09-14
13
503 Views
Last Modified: 2010-03-31
Hi,

I got this problem...I try to put some small JPanel into a bigger JPanel, and put the bigger one into a JScrollPane in case if the bigger one cannot show all components inside...but now I cannot see all the components which are inside of the bigger JPanel...and no scroll bar displayed...Please help me to exam my codes...


JPanel basicPanel = new JPanel();
JPanel comPanel[] = new JPanel[6];
JScrollPane scPanel = new JScrollPane(basicPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

basicPanel.setLayout(null);
basicPanel.setSize(850, 400);
basicPanel.setBackground(Color.black);

for (int i = 0; i < 6; i++)
{
 JLabel tmpLabel = new JLabel("Panel " + String.valueOf(i));
 comPanel[i] = new JPanel();
 comPanel[i].add(tmpLabel);
 basicPanel.add(comPanel[i]);
 comPanel[i].setBounds(0, i*100, 300, 100);
}



so what should I do???



Xenia
0
Comment
Question by:xenia27
  • 6
  • 4
  • 3
13 Comments
 
LVL 6

Assisted Solution

by:expertmb
expertmb earned 100 total points
ID: 12061699
it will display scrollbar always
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS
0
 
LVL 92

Expert Comment

by:objects
ID: 12061701
why aren't you using a layout manager?
0
 
LVL 92

Expert Comment

by:objects
ID: 12061702
Make sure you are adding scPanel to your component hierarchy, and not basicPanel.
0
 

Author Comment

by:xenia27
ID: 12061733
OK...I try JScrollPane.VERTICAL_SCROLLBAR_ALWAYS and JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS
I do saw the scroll panel but I cannot scroll down as it should be...Any idea?

Just need to align all the smaller JPanel by myself...
0
 
LVL 6

Expert Comment

by:expertmb
ID: 12061735
import javax.swing.*;
import java.awt.*;
class scrlpaneTest extends JFrame{

      public static void main(String[] s){
            scrlpaneTest t = new scrlpaneTest();
            t.setSize(400,400);
            t.setVisible(true);
      }

      scrlpaneTest(){
            JPanel basicPanel = new JPanel();
            JPanel comPanel[] = new JPanel[6];
            JScrollPane scPanel = new JScrollPane(basicPanel,
            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

            basicPanel.setLayout(null);
            basicPanel.setSize(850, 400);
            basicPanel.setBackground(Color.black);

            getContentPane().add(scPanel, BorderLayout.CENTER);
            for (int i = 0; i < 6; i++)
            {
             JLabel tmpLabel = new JLabel("Panel " + String.valueOf(i));
             comPanel[i] = new JPanel();
             comPanel[i].add(tmpLabel);
             basicPanel.add(comPanel[i]);
             comPanel[i].setBounds(0, i*100, 300, 100);
            }
      }
}
0
 
LVL 92

Expert Comment

by:objects
ID: 12061747
> I do saw the scroll panel but I cannot scroll down as it should be...Any idea?

that is because your scroll pane thinks the panel is smaller than the viewport.

A layout manager make all this a lot easier, as it handles all the required sizing for you.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:xenia27
ID: 12061759
so should I set viewport???
0
 
LVL 92

Expert Comment

by:objects
ID: 12061776
personally I'd use a layout manager
0
 

Author Comment

by:xenia27
ID: 12061785
so which Layout manager is preferred???
0
 
LVL 92

Accepted Solution

by:
objects earned 400 total points
ID: 12061788
if not using a layout manager try setting the preferred size of your panel:

basicPanel.setPreferredSize(new Dimension(850, 400));
0
 
LVL 92

Expert Comment

by:objects
ID: 12061813
a BoxLayout would seem suitable for you.
Did setting the preferred size help?
0
 
LVL 6

Expert Comment

by:expertmb
ID: 12061839
problem with the basicPanel setSize or preffered size
try to give different value for X you will see the scroll bar effect
basicPanel.setPreferredSize(new Dimension(100, X));

moreover its null layout which is making the panels to appear till the size of the panel height and width,
after that panels will not be visible.
0
 

Author Comment

by:xenia27
ID: 12061875
OK...it works...I  mean setPreferredSize...^^...Thanks~
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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 “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

760 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

22 Experts available now in Live!

Get 1:1 Help Now