javaswingjscrollpanejscrollbarnull-layout-manager

Scrollbars not appering in java Swing


In my program I have added one Label, two RadioButtons, one TextField and submit button to the Panel. My radiobuttons and submit button is not displaying in current window size because i have set their position below in the JFrame window. But the Scrollbars are not appearing in the window. Tell me where I'm Wrong.

Here is my code

import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.*;
class SwingForm extends JFrame
{
    SwingForm()
    {
        super();
        Container c=getContentPane();
        c.setLayout(new BorderLayout());
        JPanel p=new JPanel();
        p.setLayout(null);
        JLabel l=new JLabel("First Name");
        l.setBounds(50, 50, 70, 20);
        JTextField j=new JTextField();
        j.setBounds(150, 50, 70, 20);
        JRadioButton j1=new JRadioButton("Male");
        j1.setBounds(50, 300, 70, 50);
        JRadioButton j2=new JRadioButton("Female");
        j2.setBounds(170, 300, 70, 50);
        JButton b=new JButton("Submit");
        b.setBounds(100, 600, 100, 100);
        ButtonGroup bg=new ButtonGroup();
        bg.add(j1);
        bg.add(j2);
        p.add(l);  
        p.add(j);
        p.add(j1);
        p.add(j2);
        p.add(b);
        int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
        int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
        JScrollPane jp=new JScrollPane(p,v,h);
        c.add(jp,BorderLayout.CENTER);
        setVisible(true);
        setSize(300,300);
    }
    public static void main(String args[])
    {
        new SwingForm();
    }
}

Here is the output Scrollbars not appering even i have radiobutton and submit button below the window

enter image description here


Solution

  • p.setLayout(null); is your (most significant issue). JScrollPane will rely on the preferredSize of it's view to make determinations about when scroll bars should be shown.

    The best way to get this to work is to use one or more appropriate layout managers and let the API do it's job

    The following example actually makes used of 3 layout managers, BorderLayout (which is default for JFrame), FlowLayout (which is default for JPanel) and GridBagLayout

    See Layout out components in a container for more details

    Hello scrollbars

    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import javax.swing.ButtonGroup;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    import javax.swing.JScrollPane;
    import javax.swing.JTextField;
    
    public class Test {
    
        public static void main(String[] args) {
            new Test();
        }
    
        public Test() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    JFrame frame = new JFrame();
                    JPanel p = new JPanel();
                    p.setLayout(new GridBagLayout());
    
                    GridBagConstraints gbc = new GridBagConstraints();
                    gbc.gridx = 0;
                    gbc.gridy = 0;
                    JLabel l = new JLabel("First Name");
                    p.add(l, gbc);
    
                    JTextField j = new JTextField(12);
                    gbc.gridx++;
                    p.add(j, gbc);
    
                    gbc.gridx = 0;
                    gbc.gridy++;
                    gbc.gridwidth = GridBagConstraints.REMAINDER;
    
                    JRadioButton j1 = new JRadioButton("Male");
                    JRadioButton j2 = new JRadioButton("Female");
                    ButtonGroup bg = new ButtonGroup();
                    bg.add(j1);
                    bg.add(j2);
                    JPanel buttons = new JPanel();
                    buttons.add(j1);
                    buttons.add(j2);
                    p.add(buttons, gbc);
    
                    gbc.gridy++;
                    gbc.insets = new Insets(100, 0, 0, 0);
    
                    JButton b = new JButton("Submit");
                    p.add(b, gbc);
    
                    JScrollPane jp = new JScrollPane(p);
                    frame.add(jp, BorderLayout.CENTER);
                    //frame.pack();
                    // This is just to force the point
                    frame.setSize(250, 100);
                    frame.setVisible(true);
                }
            });
        }
    
    }