0

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

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Rohit Suthar
  • 967
  • 9
  • 22
  • 3
    `p.setLayout(null);` is your first, second, third and last problem - `JScrollPane` relies on the components `preferredSize` to make determinations about when scroll bars should be shown. As a general recommendation, I would strongly recommend you take the time to learn how to [layout out components within a container](https://docs.oracle.com/javase/tutorial/uiswing/layout/index.html) – MadProgrammer Apr 19 '18 at 04:19
  • *"Scrollbars not appering"* `p.setLayout(null);` Yep. That'll do it! Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Apr 19 '18 at 04:20
  • Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height - to show how the extra space should be used. – Andrew Thompson Apr 19 '18 at 04:23
  • Thank you for the solution it works after setting GridLayout() for the panel. – Rohit Suthar Apr 19 '18 at 04:29

1 Answers1

1

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);
            }
        });
    }

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366