0
  import javax.swing.JCheckBox;
  import javax.swing.JFrame;
  import javax.swing.JLabel;
  import javax.swing.JPanel;
  import javax.swing.JScrollPane;

public class ScrollJPanelDemo extends JFrame {
   public ScrollJPanelDemo(){
    setSize(480, 200);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JLabel label = new JLabel("Select one or more options : ");
    JCheckBox jcb1 = new JCheckBox("Chandigarh");
    JCheckBox jcb2 = new JCheckBox("Mohali");
    JCheckBox jcb3 = new JCheckBox("Delhi");
    JCheckBox jcb4 = new JCheckBox("Noida");
    JCheckBox jcb5 = new JCheckBox("Mumbai");
    JCheckBox jcb6 = new JCheckBox("Kolkata");

    //creating JPanel to hold the checkboxes
    JPanel jpnl = new JPanel();
    jpnl.setLayout(null);
    jpnl.setOpaque(true);
    jcb1.setBounds(0,0,100,40);
            jcb2.setBounds(0,60,100,40);
            jcb3.setBounds(0,120,100,40);
            jcb4.setBounds(0,180,100,40);
            jcb5.setBounds(0,240,100,40);
            jcb6.setBounds(0,300,100,40);
    //adding check boxes and label to the JPanel
    jpnl.add(label);
    jpnl.add(jcb1);
    jpnl.add(jcb2);
    jpnl.add(jcb3);
    jpnl.add(jcb4);
    jpnl.add(jcb5);
    jpnl.add(jcb6);

    //creating the scroll pane that will scroll the panel.
    JScrollPane jscrlPane = new JScrollPane(jpnl);
    jscrlPane.setBounds(0,0,300,300);



        jscrlPane.setHorizontalScrollBarPolicy
       (JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS) ;
   jscrlPane.setVerticalScrollBarPolicy
   (JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

    //adding that scroll pane to the frame.
    getContentPane().add(jscrlPane);
    setVisible(true);
  }

    public static void main(String args[]){
            new ScrollJPanelDemo();             
        }

 }

I'm new in Java Swing and try to use of Scroll pane on my Java code, but it's not working. The Scroll Pane is add on the frame in vertical direction but not worked.

halfer
  • 19,824
  • 17
  • 99
  • 186
kks
  • 21
  • 2
  • 1
    Urgent? Using a real layout will likely save you time & effort. See also [*Initial Threads*](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html). – trashgod Apr 09 '15 at 14:21

2 Answers2

2

You should create your own panel that extends JPanel containing all checkboxes and in this panel override getPreferredSize() method like:

@Override
public Dimension getPreferredSize()
{
    return new Dimension( 300,300 );
}

and use it in your code:

...

// creating the scroll pane that will scroll the panel.
JScrollPane jscrlPane = new JScrollPane( new MyPanelWithCheckboxes() );
jscrlPane.setBounds( 0, 0, 300, 300 );
...
Mateusz Sroka
  • 2,255
  • 2
  • 17
  • 19
  • 2
    [Don't use `setPreferredSize()` when you really mean to override `getPreferredSize()`](http://stackoverflow.com/q/7229226/230513). – trashgod Apr 09 '15 at 14:20
  • Sorry, but this solution does not work for me at all, unfortunately: JScrollPane shows its scrollers right away in almost full length and their length does not change when content of my class that extended JPanel changes. I mean it does not work for dynamic JPanel which is exactly my case. – qraqatit Jan 20 '20 at 23:30
-1

I see OP already set desired/correct answer for him, yet that solution does not work with dynamic content (in my case vertically on Y axis) so I "repaired" or updated - if you will - Mateusz's original answer so that now it actually works even with dynamic JPanel content (like when you adding to it other components on the run which was my case).

Here is my code (works, using it myself):

import java.awt.Component;
import java.awt.Dimension;

import javax.swing.JPanel;

public class JPanelForNullLayoutScroll extends JPanel {

    int h;

    @Override
    public Dimension getPreferredSize() {

        if (getComponentCount() > 0) {
            h = 0;

            for (Component c : getComponents()) {
                h += c.getHeight();
            }

        } else {
            h = 0;
        }

        // as I do not need width recount
        //I set it to be taken directly from the component itself
        Dimension d = new Dimension(getWidth(), h);

        return d;
    }

}

You can use it then in your code by implementing it like:

int tableW = 300;
int tableH = 300;

// JPANEL WITH NULL LAYOUT
JPanelForNullLayoutScroll container = new JPanelForNullLayoutScroll();
container.setLayout(null);
container.setVisible(true);
container.setEnabled(true);
container.setBounds(0, 0, tableW, tableH);

// SCROLLER
JScrollPane scrollPane = new JScrollPane(container);
scrollPane.setAlignmentX(JLabel.LEFT_ALIGNMENT);
scrollPane.getVerticalScrollBar().setUnitIncrement(8);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setBounds(0, 0, tableW, tableH);
qraqatit
  • 492
  • 4
  • 14