Hey guys I wanted to create a JScrollPane but it won’t work… and I don’t know why… here’s my code…
public class test extends JFrame{
public test(){
setSize(1000,600);
}
private static JButton[] remove;
private static JPanel p = new JPanel();
public static void main(String[]args){
p.setLayout(null);
JFrame t=new test();
remove = new JButton[25];
for(int i=0;i<25;i++){
remove[i]=new JButton("Remove");
remove[i].setBounds(243,92+35*i,85,25);
p.add(remove[i]);
}
JScrollPane scrollPane = new JScrollPane(p);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
t.add(scrollPane);
t.setVisible(true);
}
Umm and Im pretty sure the frame isn’t big enough for these 25 buttons… But if i delete that p.setLayout(null); A horizontal scroll bar will be created automatically… I don’t really know what is wrong with my code… Pls help thank you very much!
You need to set p’s preferredSize for this to work.
Or you could have p extend JPanel and then override the getPreferredSize() method to return the proper dimension.
And I agree — get rid of your null layouts. Learn about and use the layout managers if you want to use Swing correctly and have robust Swing GUI’s.
e.g.,