Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JScrollPane doesn't work for my JPanel

first of all I must say that I have checked these questions and didn't find my answer :

1 , 2 , 3 , 4 , 5 , 6 , 7

and many other questions like so

also I have checked these tutorials and examples:

1 , 9 , 10 , 11

and many other sites. but I couldn't fix my problem.

and this is the simple kind of my code:

public class Question extends JFrame {
public Question() {
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    setLayout(new BorderLayout());
    setSize(d.width, d.height);
    setResizable(false);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    JPanel panel = new JPanel();
    panel.setPreferredSize(new Dimension(d.width, d.height));
    panel.setBorder(BorderFactory.createLineBorder(Color.red));
    panel.setLayout(new BoxLayout(panel, 1));
    for (int i = 0; i < 100; i++) {
        panel.add(new JButton("kjdh"));
    }
    JScrollPane scrollPane = new JScrollPane(panel);
    scrollPane.setPreferredSize(new Dimension(500, 500));
    getContentPane().add(scrollPane);
    getContentPane().add(panel);
    setVisible(true);

}

public static void main(String[] args) {
    new Question();
} 
}

but the JScrollPane doesn't appear. I have tested many things. I have changed the way adding panel and scrollPane to my frame but it didn't work. can any one help me plz?

like image 444
Paniz Avatar asked Nov 30 '22 12:11

Paniz


1 Answers

  1. Don't set a preferred size on the panel. See Should I avoid the use of setPreferred/Maximum/MinimumSize methods in Java Swing? for the reasons why.
  2. Add only the scroll pane to the content pane.
    1. A content pane using the default layout (BorderLayout) will default to putting the component in the CENTER constraint if none is supplied, and the CENTER area can only accept a single component.
    2. Besides that, the panel has already been added to the scroll pane, it will already appear inside it, and can only appear in a single container.
  3. Don't extend frame, just use an instance of one.
  4. Don't setSize, but setExtendedState.
  5. GUIs should be constructed and updated on the EDT.
  6. A better close operation is DISPOSE_ON_CLOSE.

import java.awt.*;
import javax.swing.*;

public class Question {

    public Question() {
        JFrame f = new JFrame();
        f.setLayout(new BorderLayout());
        f.setResizable(false);
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel panel = new JPanel();
        panel.setBorder(BorderFactory.createLineBorder(Color.red));
        panel.setLayout(new BoxLayout(panel, 1));
        for (int i = 0; i < 100; i++) {
            panel.add(new JButton("kjdh"));
        }
        JScrollPane scrollPane = new JScrollPane(panel);
        f.getContentPane().add(scrollPane);
        f.pack();
        f.setExtendedState(JFrame.MAXIMIZED_BOTH);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                new Question();
            }
        };
        SwingUtilities.invokeLater(r);
    }
}
like image 108
Andrew Thompson Avatar answered Dec 05 '22 19:12

Andrew Thompson