Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 414
QA_Col Avatar asked Mar 23 '16 16:03

QA_Col


1 Answers

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.

like image 169
camickr Avatar answered Sep 27 '22 15:09

camickr