18

I have a set of vertically ordered elements. They are displayed with the following code:

JPanel myPanel = new JPanel();
myPanel.setLayout(new BoxLayout(myPanel, BoxLayout.Y_AXIS));
JButton button = new JButton("My Button");
JLabel label = new JLabel("My label!!!!!!!!!!!");
myPanel.add(button);
myPanel.add(label);

I would like to put a horizontal line between my elements (something like <hr> in html). Does anybody know how it can be done?

Roman
  • 124,451
  • 167
  • 349
  • 456
  • 4
    Yet again the answer is found in the Swing tutorial. This guy continually refuses to read the tutorial. Check out his comment in this posting: http://stackoverflow.com/questions/2561305/how-can-i-set-distance-between-elements-ordered-vertically, where he states that his time is more important than ours, which is why he doesn't bother to read the tutorial. It amazes me he still gets answers spoon fed to him. – camickr Apr 02 '10 at 16:02

2 Answers2

27

Use a JSeparator. Check out this tutorial on it.

But for a quick answer, just use the following code:

myPanel.add(button);
myPanel.add(new JSeparator());
myPanel.add(label);
Ascalonian
  • 14,409
  • 18
  • 71
  • 103
3

Create a JSeparator and add it between button and label.

Devon_C_Miller
  • 16,248
  • 3
  • 45
  • 71