Search code examples
javaswingjpaneljscrollpanejtabbedpane

JScrollPane not working while adding a panel to the tabbedpane


I have created a JPanel with GroupLayout. I have added it as tab to the tabbed pane like below.

panel= new panel(this);
JScrollPane scrollPane = new JScrollPane();
 scrollPane.setViewportView(panel);
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        scrollPane.setPreferredSize(new Dimension(screenSize.width,screenSize.height));
        tabbedPane.addTab("panel", null, scrollPane, null);
        setLayout(groupLayout);

I could see scrollbar and it is scrolling fine in my system, In other systems with screen size bigger/smaller than this scrollbar is not appearing for scrolling.

Panel I've created as

import java.awt.Color;

public class panel extends JPanel {

    private static final long serialVersionUID = 1L;

    private JTextField date = new JTextField();
    private JTextArea reason = new JTextArea();
    private JRadioButton yes = new JRadioButton("Yes");
    private JRadioButton no = new JRadioButton("No");
    private final JLabel by = new JLabel("Followup By");
    private final JComboBox fo = new JComboBox();
    private final JPanel panel_1 = new JPanel();
    private JButton save;
    private final JPanel panel = new JPanel();
    private final JScrollPane scrollPane = new JScrollPane();
    private final JTextArea textArea = new JTextArea();
    /**
     * Create the panel.
     */
    public panel() {
        setBackground(Color.WHITE);

        ButtonGroup ButtonGroup = new ButtonGroup();

        GroupLayout groupLayout = new GroupLayout(this);
        groupLayout.setHorizontalGroup(
            groupLayout.createParallelGroup(Alignment.LEADING)
                .addGroup(groupLayout.createSequentialGroup()
                    .addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 777, GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(61, Short.MAX_VALUE))
        );
        groupLayout.setVerticalGroup(
            groupLayout.createParallelGroup(Alignment.TRAILING)
                .addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(panel_1, GroupLayout.DEFAULT_SIZE, 414, Short.MAX_VALUE)
                    .addGap(142))
        );
        panel_1.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Fo", TitledBorder.LEADING, TitledBorder.TOP, null, SystemColor.textHighlight));
        panel_1.setLayout(null);

         JLabel lblIsAFURequired = new JLabel("");
         JLabel lblProposedDateOfFU = new JLabel("");
         JLabel reason = new JLabel("");

        lblIsAFURequired.setBounds(10, 169, 132, 14);
        panel_1.add(lblIsAFURequired);
        ButtonGroup.add(yes);
        yes.setBounds(155, 165, 54, 23);
        panel_1.add(yes);
        ButtonGroup.add(no);
                no.setBounds(211, 165, 60, 23);
                panel_1.add(no);

                no.setSelected(true);
                lblProposedDateOfFU.setBounds(10, 194, 156, 14);
                panel_1.add(lblProposedDateOfFU);
                date.setBounds(161, 191, 86, 20);
                panel_1.add(date);

                date.setColumns(10);
                date.setEnabled(false);
                date.setEditable(false);
                by.setBounds(10, 219, 86, 14);
                panel_1.add(by);
                fo.setEnabled(false);
                fo.setBounds(161, 216, 67, 20);
                panel_1.add(fo);
                fo.setModel(new DefaultComboBoxModel(new String[] {"Doctor A", "Doctor B"}));
                reason.setBounds(10, 244, 132, 14);
                panel_1.add(reason);
        /*      reason.setEditable(false);
                reason.setEnabled(false)*/;
                reason.setBounds(10, 266, 735, 59);
                panel_1.add(reason);
                reason.setBackground(UIManager.getColor("Button.background"));
                reason.setBorder(new LineBorder(new Color(0, 0, 0)));

                reason.setLineWrap(true);
                reason.setWrapStyleWord(true);

        save = new JButton("Save ");
        save.setBounds(310, 363, 186, 25);
        panel_1.add(save);
        save.setForeground(new Color(0, 0, 128));
        save.setBackground(new Color(245, 245, 220));
        panel.setBounds(10, 25, 748, 133);
        panel_1.add(panel);
        panel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Consultation Summary", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(51, 153, 255)));
        GroupLayout gl_panel = new GroupLayout(panel);
        gl_panel.setHorizontalGroup(
            gl_panel.createParallelGroup(Alignment.LEADING)
                .addGap(0, 692, Short.MAX_VALUE)
                .addGroup(gl_panel.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 660, Short.MAX_VALUE)
                    .addContainerGap())
        );
        gl_panel.setVerticalGroup(
            gl_panel.createParallelGroup(Alignment.LEADING)
                .addGap(0, 133, Short.MAX_VALUE)
                .addGroup(gl_panel.createSequentialGroup()
                    .addGap(2)
                    .addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE)
                    .addContainerGap())
        );
        textArea.setWrapStyleWord(true);
        textArea.setLineWrap(true);
        textArea.setBorder(null);
        textArea.setBackground(Color.WHITE);

        scrollPane.setViewportView(textArea);
        panel.setLayout(gl_panel);

                no.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        if(no.isSelected()){
                            date.setEnabled(false);
                            date.setEditable(false);
                            date.setText("");
                            fo.setEditable(false);
                            fo.setEnabled(false);
                            reason.setEnabled(false);
                            reason.setEnabled(false);
                            reason.setBackground(SystemColor.menu);
                        }
                    }
                });

        yes.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(yes.isSelected()){
                    date.setEnabled(true);
                    date.setEditable(true);
                    fo.setEditable(true);
                    fo.setEnabled(true);
                    reason.setEnabled(true);
                    reason.setEnabled(true);
                    reason.setBackground(SystemColor.white);
                                }
            }
        });
        setLayout(groupLayout);

    }

}

Any help would be great


Solution

  • Your layout code is way too confusing. Some code appears to use a layout manager and some code doesn't. Swing was designed to be used with a layout manager.

    1. If you want the scrollpane to occupy the entire screen then just add the scroll pane to the frame. By default a JFrame uses a BorderLayout and the scroll pane will be placed in the CENTER which means the scroll pane will automatically resize to fill the entire frame.

    2. Don't use setBounds() or setPreferredSize() anywhere in your code. The scrollbars of the scroll pane will appear automatically when the preferred size of the panel is greater than the size of the scroll pane. The layout manager will calculate the preferred size for you, but it can't do its job if you use either of the above methods.

    I would also recommend you don't use an IDE to layout the components.