Solved

JScrollPane and JPanel

Posted on 2004-09-14
13
515 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 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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

717 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