I have trouble with adding scrollpane to jpanel. I can't find what is the problem, when i run the program scrollpane is not showing.
import java.awt.Dimension; import java.awt.GridLayout; import javax.swing.BorderFactory; import javax.swing.ButtonGroup; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.JScrollBar; import javax.swing.JScrollPane; class FD { private JPanel mainPanel = new JPanel(); private JRadioButton[] verticalRadioBtns = new JRadioButton[40]; private JRadioButton[] horizontalRadioBtns = new JRadioButton[6]; private ButtonGroup btngrpVertical = new ButtonGroup(); private ButtonGroup btngrpHorizontal = new ButtonGroup(); public FD() { JPanel panelVertical = new JPanel(new GridLayout(0, 1, 5, 5)); for (int i = 0; i < verticalRadioBtns.length; i++) { verticalRadioBtns[i] = new JRadioButton("radioArrayVertical Button " + i); btngrpVertical.add(verticalRadioBtns[i]); panelVertical.add(verticalRadioBtns[i]); } JPanel panelHorizontal = new JPanel(new GridLayout(1, 0, 5, 5)); for (int i = 0; i < horizontalRadioBtns.length; i++) { horizontalRadioBtns[i] = new JRadioButton("radioArray2 Button " + i); btngrpHorizontal.add(horizontalRadioBtns[i]); panelHorizontal.add(horizontalRadioBtns[i]); } panelVertical.setBorder(BorderFactory.createTitledBorder("Vertical Layout")); panelHorizontal.setBorder(BorderFactory.createTitledBorder("Horizontal Layout")); //HERE IS MY PROBLEM !!!! JScrollPane scroll = new JScrollPane(panelVertical,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); mainPanel.add(panelVertical); mainPanel.add(panelHorizontal); } public JPanel getMainPanel() { return mainPanel; } private static void createAndShowUI() { JFrame frame = new JFrame("Abbas"); frame.getContentPane().add(new FD().getMainPanel()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); //frame.setLocationRelativeTo(null); //frame.setExtendedState(JFrame.MAXIMIZED_BOTH); frame.setVisible(true); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } }