1

In this code, I have a array of JPanel containers. I'm adding those panels on JFrame one below the other, but my Scroll Bar doesn't work.

What is the mistake?

This is code:

import javax.swing.*;

public class NewJFrame extends javax.swing.JFrame {

    private javax.swing.JPanel arr[] = new JPanel[100];

    public NewJFrame() {
        initComponents();

        for (int i = 0; i < 5; i++) {
            arr[i] = new JPanel();
            arr[i].setLayout(null);
            newPanel(arr[i]);
            arr[i].setBounds(0, i * 375, 610, 370);
            getContentPane().add(arr[i]);
        }
        JScrollPane scroll = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        getContentPane().add(scroll);
    }

    private void newPanel(JPanel panel) {
        javax.swing.JLabel jLabel1;
        javax.swing.JLabel jLabel2;
        javax.swing.JLabel jLabel3;
        javax.swing.JLabel jLabel4;
        javax.swing.JLabel jLabel5;

        javax.swing.JLabel lImage;
        javax.swing.JPanel pInfo;
        javax.swing.JPanel pImage;
        pImage = new javax.swing.JPanel();
        lImage = new javax.swing.JLabel();
        pInfo = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();

        pImage.setBackground(new java.awt.Color(255, 255, 255));
        pImage.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        pImage.setLayout(null);

        lImage.setBackground(new java.awt.Color(255, 255, 255));
        lImage.setText("jLabel1");
        pImage.add(lImage);
        lImage.setBounds(0, 0, 280, 370);

        panel.add(pImage);
        pImage.setBounds(0, 0, 280, 370);

        pInfo.setBackground(new java.awt.Color(255, 255, 255));
        pInfo.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        pInfo.setLayout(null);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 13)); // NOI18N
        jLabel1.setText("Info1:");
        pInfo.add(jLabel1);
        jLabel1.setBounds(10, 10, 90, 30);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 13)); // NOI18N
        jLabel2.setText("Info2:");
        pInfo.add(jLabel2);
        jLabel2.setBounds(10, 50, 70, 30);

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 13)); // NOI18N
        jLabel3.setText("Info3:");
        pInfo.add(jLabel3);
        jLabel3.setBounds(10, 100, 34, 16);

        jLabel4.setText("*************************************");
        pInfo.add(jLabel4);
        jLabel4.setBounds(10, 140, 300, 20);

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 13)); // NOI18N
        jLabel5.setText("Info4:");
        pInfo.add(jLabel5);
        jLabel5.setBounds(10, 160, 100, 20);

        panel.add(pInfo);
        pInfo.setBounds(280, 0, 330, 370);
    }

    @SuppressWarnings("unchecked")
    private void initComponents() {
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMaximumSize(new java.awt.Dimension(640, 700));
        setMinimumSize(new java.awt.Dimension(640, 700));
        setPreferredSize(new java.awt.Dimension(640, 700));
        setResizable(true);
        getContentPane().setLayout(null);
        pack();
    }// </editor-fold>

    public static void main(String args[]) throws Exception {

        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
johnny94
  • 311
  • 4
  • 20
  • 4
    You created a `JScrollPane` and added it to the `JFrame` but you never added anything to the `JScrollPane`. ---- The JScrollPane is basically a container that you can add more components to. Everything should go in there. – byxor Aug 18 '16 at 10:55
  • 3
    Don't use null layout and `setBounds`, there are [better alternatives](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html). – Lukas Rotter Aug 18 '16 at 10:57
  • http://stackoverflow.com/questions/5928514/how-can-i-scroll-my-jframe-using-the-jscrollbar – Dheeraj Kumar Aug 18 '16 at 10:58
  • @ Lukas Rotter I m new in coding GUI, so I am not familiar with all methods. – johnny94 Aug 18 '16 at 10:59
  • 2
    @johnny94 Yes, a null-layout seems easier at first. But the more complex you GUI will get, the harder it will be hard to maintain the null-layout, and layout managers will actually be easier to use. They also lay out components by respecting the window size. To read the tutorial I linked is definitely worth it. – Lukas Rotter Aug 18 '16 at 11:03
  • Re the layout: 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. – Andrew Thompson Aug 18 '16 at 11:23
  • 1
    @LukasRotter Re edit: panels should only be `panel`s if there is an attribute called `panel` (there isn't). That notation is only for code. – Andrew Thompson Aug 18 '16 at 11:34
  • 1
    @AndrewThompson Sorry, I thought that's what you intended. I also just realized that OP actually added the ` after "panel", and that's why it came out as "panel`s on `JFrame`". – Lukas Rotter Aug 18 '16 at 11:37
  • @LukasRotter We've now got the question to be of a form that doesn't make my eyes bleed, to read. :) – Andrew Thompson Aug 18 '16 at 11:45

0 Answers0