2 Replies Latest reply: Oct 7, 2004 10:48 AM by 807587 RSS

    multiple JScrollPanes in a singe JFrame

    807587
      Hi

      I want both JPanels to scroll. Pretty simple code, just add 9 buttons to two JPanels, out the JPanels in JScrollPanes and put the JScrollPanes in the JFrame. I've tried resizing the JFrame and setSize for the Panels/ScrollPanes but I can only get the top (doesn't matter which panel is added first) of the JFrame to show the scroll bar. The bottom Panel doesn't show the scroll bar.
      import java.io.*;
      import java.util.*;
      import javax.swing.*;
      import javax.swing.JLabel;
      import javax.swing.JPanel;
      import javax.swing.border.*;
      import javax.swing.event.*;
      import java.awt.*;
      import java.awt.event.*;
      import java.util.StringTokenizer;
      import java.lang.*;
      
      public class testSP   {
      
      
         public static void main(String args[]) {
      
            JFrame j = new JFrame();
      
      
            JPanel jjj = new JPanel();
          //jjj.setSize(new Dimension(600,600));
            jjj.setLayout(new GridLayout(0,1));
            JButton b0 = new JButton("0");
            JButton b1 = new JButton("1");
            JButton b2 = new JButton("2");
            JButton b3 = new JButton("3");
            JButton b4 = new JButton("4");
            JButton b5 = new JButton("5");
            JButton b6 = new JButton("6");
            JButton b7 = new JButton("7");
            JButton b8 = new JButton("8");
            b0.setSize(new Dimension(100,100));
            b1.setSize(new Dimension(100,100));
            b2.setSize(new Dimension(100,100));
            b3.setSize(new Dimension(100,100));
            b4.setSize(new Dimension(100,100));
            b5.setSize(new Dimension(100,100));
            b6.setSize(new Dimension(100,100));
            b7.setSize(new Dimension(100,100));
            b8.setSize(new Dimension(100,100));
            jjj.add(b0);
            jjj.add(b1);
            jjj.add(b2);
            jjj.add(b3);
            jjj.add(b4);
            jjj.add(b5);
            jjj.add(b6);
            jjj.add(b7);
            jjj.add(b8);
            JScrollPane jjjSP = new JScrollPane(jjj);
           //jjjSP.setSize(new Dimension(600,600));
      
            JPanel kkk = new JPanel();
          //kkk.setSize(new Dimension(600,600));
            kkk.setLayout(new GridLayout(0,1));
            JButton k0 = new JButton("0");
            JButton k1 = new JButton("1");
            JButton k2 = new JButton("2");
            JButton k3 = new JButton("3");
            JButton k4 = new JButton("4");
            JButton k5 = new JButton("5");
            JButton k6 = new JButton("6");
            JButton k7 = new JButton("7");
            JButton k8 = new JButton("8");
            k0.setSize(new Dimension(100,100));
            k1.setSize(new Dimension(100,100));
            k2.setSize(new Dimension(100,100));
            k3.setSize(new Dimension(100,100));
            k4.setSize(new Dimension(100,100));
            k5.setSize(new Dimension(100,100));
            k6.setSize(new Dimension(100,100));
            k7.setSize(new Dimension(100,100));
            k8.setSize(new Dimension(100,100));
            kkk.add(k0);
            kkk.add(k1);
            kkk.add(k2);
            kkk.add(k3);
            kkk.add(k4);
            kkk.add(k5);
            kkk.add(k6);
            kkk.add(k7);
            kkk.add(k8);
            JScrollPane kkkSP = new JScrollPane(kkk);
          //kkkSP.setSize(new Dimension(600,600));
      
      
            j.getContentPane().add(kkkSP, BorderLayout.SOUTH);
            j.getContentPane().add(jjjSP, BorderLayout.CENTER);
      
            j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            j.setSize(275,395);
            j.setVisible(true);
      
         }
      
      
      
      }
        • 1. Re: multiple JScrollPanes in a singe JFrame
          794342
          import javax.swing.*;
          import java.awt.*;
          class testSP
          {
             public static void main(String args[])
             {
                JFrame j = new JFrame();
                JPanel jjj = new JPanel(new GridLayout(0,1));
                JPanel kkk = new JPanel(new GridLayout(0,1));
                JButton[] btn = new JButton[20];
                for(int x = 0; x < btn.length; x++)
                {
                  btn[x] = new JButton(""+(x+1));
                  btn[x].setSize(new Dimension(100,100));
                  if(x < 10)jjj.add(btn[x]);
                  else kkk.add(btn[x]);
                }
                JScrollPane jjjSP = new JScrollPane(jjj);
                jjjSP.setPreferredSize(new Dimension(300,200));//<--------------
                JScrollPane kkkSP = new JScrollPane(kkk);
                kkkSP.setPreferredSize(new Dimension(300,200));//<-----------
                j.getContentPane().add(kkkSP, BorderLayout.SOUTH);
                j.getContentPane().add(jjjSP, BorderLayout.CENTER);
                j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                j.pack();
                j.setVisible(true);
             }
          }
          • 2. Re: multiple JScrollPanes in a singe JFrame
            807587
            Thanks for the help. Your code is what I'm looking for. One other question. When I stretch the JFrame w/ the mouse, the kkkSP and it's buttons (11-20) stay a fixed size, but the jjjSP and it's buttons stretch even to the point of removing the scroll bar. I don't see anything that forces kkkSP and it's buttons to be fixed size. I suppose one of the SP's has to stretch.

            I answered this w/ a little testing, the CENTER componenent of the JFrame will stretch

            Thanks