0

I have a JFrame in which i have added two JPanel with JScrollPane, one in half part of JFrame vertically. then i am adding a no. of JPanel(each Containing two JLabel in flowlayout) to these two JPanel.
up to this every thing is fine , But when no. of JPanel increases, size of JPanel decreases while Scrolling does not working. could you help me to finding out why scrolling is not working.

Here is some code with only one JPanel

class scrol extends JFrame
 {

  scrol()
   {

     //getContentPane().setLayout(new GridLayout(1,2));
     JPanel p1=new JPanel();
     p1.setLayout(new GridLayout(0,1));
     p1.setPreferredSize(new Dimension(300,350));
     int n=1;
     p1.setBorder(BorderFactory.createLineBorder(new Color(200,167,0)));
     JScrollPane scrol1=new JScrollPane(p1);
     scrol1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
//scrol1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
     scrol1.setBounds(0,0,420,405);
     getContentPane().add(scrol1,BorderLayout.CENTER);
     while(n<100)
      {
       n=n+1;
       p1.add(new JPanel().add(new JLabel("hello")));
     }
     pack();

     setSize(500,500);
     setVisible(true);
  }
Ambuj
  • 30
  • 6

2 Answers2

0

Working code
You need to remove below line

 p1.setPreferredSize(new Dimension(300,350));

and you can add below code in while loop to control size although it will work without this code also

  tempP.setPreferredSize(new Dimension(10, 30));

below is working code with changes

class Scrol extends JFrame
{

Scrol()
{

 //getContentPane().setLayout(new GridLayout(1,2));
 JPanel p1=new JPanel();
 p1.setLayout(new GridLayout(0,1));
// p1.setPreferredSize(new Dimension(300,350));
 int n=1;
 p1.setBorder(BorderFactory.createLineBorder(new Color(200,167,0)));
 JScrollPane scrol1=new JScrollPane(p1);
 scrol1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
 //scrol1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
 //scrol1.setBounds(0,0,420,405);
 getContentPane().add(scrol1,BorderLayout.CENTER);
 while(n<200)
  {
   n=n+1;
   JPanel tempP=new JPanel();
   tempP.setPreferredSize(new Dimension(10, 30));
   p1.add(tempP.add(new JLabel("hello")));
 }
 pack();

 setSize(500,500);
 setVisible(true);
}

 public static void main(String[] args) {
new Scrol();
 }
 }
VaibJ
  • 84
  • 5
0

Since you only indicated that you want the scrolling to work here it is:

public class Scrol extends JFrame {

    Scrol() {

        JPanel p1 = new JPanel();
        p1.setLayout(new GridLayout(0, 1));
        p1.setBorder(BorderFactory.createLineBorder(new Color(200, 167, 0)));
        getContentPane().add(new JScrollPane(p1), BorderLayout.CENTER);

        for (int n = 1; n < 100; n++)
            p1.add(new JPanel().add(new JLabel("hello")));

        setSize(500, 500); // or pack()
        setVisible(true);
    }

    public static void main(String[] args) {

        new Scrol();
    }
}

Notes:

  • Usually you don't want to mess with components' sizes, you have a layout manager for that. I removed all size specifications since you did not specify any constraint and they usually cause unexpected result.
  • Class names should start with an uppercase.
  • You code is a mess, sort it logically (int n = 1 in the middle of setting components?).
user1803551
  • 12,965
  • 5
  • 47
  • 74
  • thanks very much, can i do same with size constraints ,because my application not working properly however scrolling is ok now – Ambuj Sep 29 '14 at 06:34
  • 1
    @Ambuj You can, but for that you will need to ask a new question with the exact requirements and detail of what is not working and how you want it to work. You will need to post the relevant code there. – user1803551 Sep 29 '14 at 13:19