javaswingjpaneljseparator

I can't see Swing JSeparator


I have this code:

  JPanel jpMainExample = new JPanel(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));
  jpMainExample.add(new JLabel("JLabel"));
  jpMainExample.add(new JTextField("JTextField"));
  jpMainExample.add(new JSeparator(JSeparator.VERTICAL));
  jpMainExample.add(new JRadioButton("JRadioButton"));
  jpMainExample.add(new JSeparator(SwingConstants.VERTICAL));
  jpMainExample.add(new JComboBox<>(new String[] {"JComboBox"}));
  jpOUT.add(jpMainExample);

But, I can't see the separator.

enter image description here

What is wrong?


Solution

  • The preferredSize of the separator is (2, 0). A FlowLayout respects the preferred size. Since the height is 0, there is nothing to paint.

    So you need to use a different layout manager that will resize the component to fill the space available vertically.

    Check out the section from the Swing tutorial on How to Use Separators for a working example. It shows how to use a BoxLayout.