How to add scrollbar in JFrame with null layout?

earthmover picture earthmover · Sep 19, 2013 · Viewed 36.3k times · Source

I want to add a vertical scroll-bar on my JFrame with null layout.

Is it possible or not? please help!

Answer

SeniorJD picture SeniorJD · Sep 19, 2013

Just set the JScrollPane as ContentPane for JFrame as it is described here:

public class TabbedPaneTest {
    public static void main(String [] a) {
        final JFrame frame = new JFrame();
        frame.setSize(500, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JScrollPane pane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

        frame.setContentPane(pane);

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                frame.setVisible(true);
            }
        });
   }
}