In my code, I am adding some JSeparator
s. Everything is working fine, except that in the beginning, I have to add two JSeparator
s for them to show, instead of one! Here's the code:
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
int counter = 1;
add(new JSeparator(SwingConstants.HORIZONTAL)); //Here is the
add(new JSeparator(SwingConstants.HORIZONTAL)); //problem
JPanel p2 = new JPanel();
for (int i = 0; i < petsx; i ++) {
for (int i2 = 0; i2 < petsy; i2 ++) {
p2.add(new JLabel(new ImageIcon(pics[i][i2])));
p2.add(new JLabel(names[i][i2]));
if (counter % petsx == 0) {
add(p2);
add(new JSeparator(SwingConstants.HORIZONTAL));
p2 = new JPanel();
counter = 0;
} else {
JSeparator js = new JSeparator(SwingConstants.VERTICAL);
js.setPreferredSize(new Dimension(1, newPetHeight));
p2.add(js);
}
counter ++;
}
}
As you can see, I have to call add(new JSeparator(SwingConstants.HORIZONTAL));
twice for it to work. If I only call it once, the JSeparator
does not show up. Why is this?
Here is the compilable code: http://pastebin.com/xbe47a29