6

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?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 2
    Take a look at http://stackoverflow.com/questions/2425729/jseparator-wont-show-with-gridbaglayout . It addresses a different layout, but may help. – bradimus Mar 23 '16 at 16:48
  • *"I can't see the separator."* In some PLAFs, a separator is invisible (but still takes up space). How does the component placement appear without the separators? – Andrew Thompson Mar 23 '16 at 22:37

1 Answers1

7

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.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • 1
    One can also just set the the minimum size of the separator with setMinimumSize(). This works at least for FlowLayout. – trinity420 Jul 05 '18 at 21:53