first of all I must say that I have checked these questions and didn't find my answer :
and many other questions like so
also I have checked these tutorials and examples:
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?
You've added an unecessary duplicate panel on the context pane. Instead of:
use only
It makes sense as a scrool pane is a container for a panel, so it's enough to add a container on the context pane.
BorderLayout
) will default to putting the component in theCENTER
constraint if none is supplied, and theCENTER
area can only accept a single component.setSize
, butsetExtendedState
.DISPOSE_ON_CLOSE
.