[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

JScrollPane and JPanel

Posted on 2004-09-14
13
Medium Priority
?
520 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
[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
  • 6
  • 4
  • 3
13 Comments
 
LVL 6

Assisted Solution

by:expertmb
expertmb earned 400 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
Independent Software Vendors: 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!

 

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
 

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 1600 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

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

656 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