adding Scrollbar to each JPanel(if needed) from JFrame

4.3k Views Asked by At

// This is my codes for JFrame which is then connected to other Panels.

    public class startQuiz extends JFrame {

        private static final long serialVersionUID = 1L;
        private JPanel jContentPane = null;
        private JButton jButtonGQuiz = null;
        private JFrame myFrame = null;
        private JLabel jLabelTitle = null;
        private JLabel jLabelGQuiz = null;
        private JLabel jLabelCQuiz = null;
        private JButton jButtonCQuiz = null;
        private JMenuBar jJMenuBar = null;
        private JMenu jMenuFile = null;
        private JMenuItem jMenuItemNew = null;
        private JMenuItem jMenuItemImport = null;
        private JScrollPane jScrollPane = null;
        /**
         * This method initializes jButtonGQuiz 
         *  
         * @return javax.swing.JButton  
         */
        private JButton getJButtonGQuiz() {
            if (jButtonGQuiz == null) {
                jButtonGQuiz = new JButton();
                jButtonGQuiz.setBounds(new Rectangle(345, 90, 75, 26));
                jButtonGQuiz.setText("Start");
                jButtonGQuiz.addActionListener(new java.awt.event.ActionListener() {
                    public void actionPerformed(java.awt.event.ActionEvent e) {
                        startButtonClicked();
                    }
                    public void startButtonClicked() {
                        JPanel panel = new GQuiz(myFrame);
                        myFrame.getContentPane().removeAll();
                        myFrame.getContentPane().add(panel);
                        myFrame.getContentPane().validate();
                        myFrame.getContentPane().repaint();
                    }
                });
            }
            return jButtonGQuiz;
        }

        /**
         * This method initializes jButtonCQuiz 
         *  
         * @return javax.swing.JButton  
         */
        private JButton getJButtonCQuiz() {
            if (jButtonCQuiz == null) {
                jButtonCQuiz = new JButton();
                jButtonCQuiz.setBounds(new Rectangle(344, 140, 75, 26));
                jButtonCQuiz.setText("Start");
                jButtonCQuiz.addActionListener(new java.awt.event.ActionListener() {
                    public void actionPerformed(java.awt.event.ActionEvent e) {
                        startButton1Clicked();
                    }
                    public void startButton1Clicked() {
                        JPanel panel = new CancerQuiz(myFrame);
                        myFrame.getContentPane().removeAll();
                        myFrame.getContentPane().add(panel);
                        myFrame.getContentPane().validate();
                        myFrame.getContentPane().repaint();

                    }
                });
            }
            return jButtonCQuiz;
        }

        /**
         * This method initializes jJMenuBar    
         *  
         * @return javax.swing.JMenuBar 
         */
        private JMenuBar getJJMenuBar() {
            if (jJMenuBar == null) {
                jJMenuBar = new JMenuBar();
                jJMenuBar.add(getJMenuFile());
            }
            return jJMenuBar;
        }

        /**
         * This method initializes jMenuFile    
         *  
         * @return javax.swing.JMenu    
         */
        private JMenu getJMenuFile() {
            if (jMenuFile == null) {
                jMenuFile = new JMenu();
                jMenuFile.setText("File");
                jMenuFile.add(getJMenuItemNew());
                jMenuFile.add(getJMenuItemImport());
            }
            return jMenuFile;
        }

        /**
         * This method initializes jMenuItemNew 
         *  
         * @return javax.swing.JMenuItem    
         */
        private JMenuItem getJMenuItemNew() {
            if (jMenuItemNew == null) {
                jMenuItemNew = new JMenuItem();
                jMenuItemNew.setText("New");
                jMenuItemNew.addActionListener(new java.awt.event.ActionListener() {
                    public void actionPerformed(java.awt.event.ActionEvent e) {
                        newMenuItemClicked();
                    }
                    public void newMenuItemClicked() {
                        JPanel panel = new MyTestDatePicker(myFrame);
                        myFrame.getContentPane().removeAll();
                        myFrame.getContentPane().add(panel);
                        myFrame.getContentPane().validate();
                        myFrame.getContentPane().repaint();
                    }
                });
            }
            return jMenuItemNew;
        }

        /**
         * This method initializes jMenuItemImport  
         *  
         * @return javax.swing.JMenuItem    
         */
        private JMenuItem getJMenuItemImport() {
            if (jMenuItemImport == null) {
                jMenuItemImport = new JMenuItem();
                jMenuItemImport.setText("Import");
                jMenuItemImport.addActionListener(new java.awt.event.ActionListener() {
                    public void actionPerformed(java.awt.event.ActionEvent e) {
                        importMenuItemClicked();
                    }
                    public void importMenuItemClicked() {
                        JPanel panel = new importQuestions(myFrame);
                        myFrame.getContentPane().removeAll();
                        myFrame.getContentPane().add(panel);
                        myFrame.getContentPane().validate();
                        myFrame.getContentPane().repaint();

                    }
                });
            }
            return jMenuItemImport;
        }

        /**
         * This method initializes jScrollPane  
         *  
         * @return javax.swing.JScrollPane  
         */


        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    startQuiz thisClass = new startQuiz();
                    thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    thisClass.setVisible(true);

                }
            });
        }

        /**
         * This is the default constructor
         */
        public startQuiz() {
            super();
            initialize();
            myFrame = this;
            this.setVisible(true);
        }

        /**
         * This method initializes this
         * 
         * @return void
         */
        private void initialize() {
            this.setSize(680, 320);
            this.setJMenuBar(getJJMenuBar());
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setContentPane(getJContentPane());
            this.setTitle("Quiz");
            this.setVisible(true);


        }

        /**
         * This method initializes jContentPane
         * 
         * @return javax.swing.JPanel
         */
        private JPanel getJContentPane() {
            if (jContentPane == null) {
                jLabelCQuiz = new JLabel();
                jLabelCQuiz.setHorizontalAlignment(SwingConstants.RIGHT);
                jLabelCQuiz.setLocation(new Point(64, 134));
                jLabelCQuiz.setSize(new Dimension(251, 38));
                jLabelCQuiz.setText("Cancer Quiz : ");
                jLabelGQuiz = new JLabel();
                jLabelGQuiz.setBounds(new Rectangle(66, 83, 251, 38));
                jLabelGQuiz.setHorizontalAlignment(SwingConstants.RIGHT);
                jLabelGQuiz.setText("General Quiz :");
                jLabelTitle = new JLabel();
                jLabelTitle.setBounds(new Rectangle(73, 8, 488, 46));
                jLabelTitle.setFont(new Font("Dialog", Font.BOLD, 18));
                jLabelTitle.setHorizontalAlignment(SwingConstants.CENTER);
                jLabelTitle.setText("Choose a Quiz !");
                jContentPane = new JPanel();  
                jContentPane.setLayout(null);
                jContentPane.setBorder(null);
                jContentPane.add(getJButtonGQuiz(), null);
                jContentPane.add(jLabelTitle, null);
                jContentPane.add(jLabelGQuiz, null);
                jContentPane.add(jLabelCQuiz, null);
                jContentPane.add(getJButtonCQuiz(), null);

            }
            return jContentPane;
        }

    }

//This is one of my panel that is linked to that previous JFrame. I added Scrollbar here //but it is not working. Therefore, how can I make the scrollbar to be working properly.

    import java.awt.GridBagLayout;
    import javax.swing.JPanel;
    import javax.swing.JScrollBar;
    import java.awt.Rectangle;
    import java.awt.Dimension;

    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JRadioButton;
    import javax.swing.JButton;

    import myQuiz.entity.QuizQuestion;
    import javax.swing.SwingConstants;
    import javax.swing.JScrollPane;

    public class GQuiz extends JPanel {

        private static final long serialVersionUID = 1L;
        private JLabel jLabelG1 = null;
        private JRadioButton jRadioButtonTrue = null;
        private JRadioButton jRadioButtonFalse = null;
        private JLabel jLabelG2 = null;
        private JRadioButton jRadioButtonTrue1 = null;
        private JRadioButton jRadioButtonFalse1 = null;
        private JButton jButtonSubmit = null;
        private JFrame myFrame = null;

        private int questionID;
        private String username;
        private String answer;
        /**
         * This is the default constructor
         */
        public GQuiz() {
            super();
            initialize();
        }
        public GQuiz (JFrame f){
            this();
            myFrame = f;
            }

        /**
         * This method initializes this
         * 
         * @return void
         */
        private void initialize() {
            jLabelG2 = new JLabel();
            jLabelG2.setBounds(new Rectangle(5, 104, 724, 37));
            jLabelG2.setText("2. Exercise is a good way to help prevent heart attacks.");
            jLabelG1 = new JLabel();
            jLabelG1.setBounds(new Rectangle(6, 3, 719, 32));
            jLabelG1.setHorizontalAlignment(SwingConstants.LEFT);
            jLabelG1.setText("1. Drinking two glasses of water before meals is a good way to help fill your stomach up which makes you eat less.");
            this.setSize(733, 359);
            this.setLayout(null);
            this.add(jLabelG1, null);
            this.add(getJRadioButtonTrue(), null);
            this.add(getJRadioButtonFalse(), null);
            this.add(jLabelG2, null);
            this.add(getJRadioButtonTrue1(), null);
            this.add(getJRadioButtonFalse1(), null);
            this.add(getJButtonSubmit(), null);
        }

        /**
         * This method initializes jRadioButtonTrue 
         *  
         * @return javax.swing.JRadioButton 
         */
        private JRadioButton getJRadioButtonTrue() {
            if (jRadioButtonTrue == null) {
                jRadioButtonTrue = new JRadioButton();
                jRadioButtonTrue.setBounds(new Rectangle(7, 39, 77, 26));
                jRadioButtonTrue.setText("True");
            }
            return jRadioButtonTrue;
        }

        /**
         * This method initializes jRadioButtonFalse    
         *  
         * @return javax.swing.JRadioButton 
         */
        private JRadioButton getJRadioButtonFalse() {
            if (jRadioButtonFalse == null) {
                jRadioButtonFalse = new JRadioButton();
                jRadioButtonFalse.setBounds(new Rectangle(8, 70, 74, 30));
                jRadioButtonFalse.setText("False");
            }
            return jRadioButtonFalse;
        }

        /**
         * This method initializes jRadioButtonTrue1    
         *  
         * @return javax.swing.JRadioButton 
         */
        private JRadioButton getJRadioButtonTrue1() {
            if (jRadioButtonTrue1 == null) {
                jRadioButtonTrue1 = new JRadioButton();
                jRadioButtonTrue1.setBounds(new Rectangle(6, 145, 75, 30));
                jRadioButtonTrue1.setText("True");
            }
            return jRadioButtonTrue1;
        }

        /**
         * This method initializes jRadioButtonFalse1   
         *  
         * @return javax.swing.JRadioButton 
         */
        private JRadioButton getJRadioButtonFalse1() {
            if (jRadioButtonFalse1 == null) {
                jRadioButtonFalse1 = new JRadioButton();
                jRadioButtonFalse1.setBounds(new Rectangle(6, 177, 77, 31));
                jRadioButtonFalse1.setText("False");
            }
            return jRadioButtonFalse1;
        }

        /**
         * This method initializes jButtonSubmit    
         *  
         * @return javax.swing.JButton  
         */
        private JButton getJButtonSubmit() {
            if (jButtonSubmit == null) {
                jButtonSubmit = new JButton();
                jButtonSubmit.setBounds(new Rectangle(284, 287, 83, 32));
                jButtonSubmit.setText("Submit");


            }
            return jButtonSubmit;
        }

    }  
2

There are 2 best solutions below

2
On

I don't see you use the JScrollPane in your panel, but basically this might not be the right place anyways (depending on what you want to do).

In general, you add the scroll pane to your frame instead of the panel and put the panel into the scroll pane. It's like this pseudocode:

 JFrame frame = new JFrame();
 JPanel panel = new JPanel();
 JScrollPane scroll = new JScrollPane( panel );
 frame.add( scroll);
2
On

If you want it for every JPanel you add to JFrame then you can do as follows:

  1. Create a class extending JFrame class
  2. Override add(component) method of JFrame
  3. In this method check if paramter component is JPanel or not.
  4. If JPanel then create a JScrollPane, add jPanel to it and then add scrollPane to frame.
  5. Else call super.add(component);