Question

In my code, I am adding some JSeparators. Everything is working fine, except that in the beginning, I have to add two JSeparators 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

Was it helpful?

Solution

I have no problems...

enter image description here

I suspect that you're possibly doing one or more of the following

  1. The first separator may be getting aligned to the very top of the container, right against the title bar, making it "appear" as if it's not being added..
  2. You're not creating your UI in the EDT
  3. You're showing the frame before you finish creating the pane
  4. Something else you're not telling us

.

public class TestSeperator {

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

    public TestSeperator() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestSeperatorPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestSeperatorPane extends JPanel {

        public TestSeperatorPane() {
            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();
            int petsx = 4;
            int petsy = 4;
            for (int i = 0; i < petsx; i++) {
                for (int i2 = 0; i2 < petsy; i2++) {
                    p2.add(new JLabel(":)"));
                    p2.add(new JLabel("A"));
                    if (counter % petsx == 0) {
                        add(p2);
                        add(new JSeparator(SwingConstants.HORIZONTAL));
                        p2 = new JPanel();
                        counter = 0;
                    } else {
                        JSeparator js = new JSeparator(SwingConstants.VERTICAL);
                        // This is a bad idea...
                        //js.setPreferredSize(new Dimension(1, newPetHeight));
                        js.setBorder(new EmptyBorder(6, 0, 6, 0));
                        p2.add(js);
                    }
                    counter++;
                }
            }
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top