Center JSeparator

Hi,

I have a Vertically aligned JSeparator. I would like to add padding on each side of it by increasing its size. However, when I update the preferred size to say 20, the separtor line shows up at the right-most edge instead of in the center. The XAlignement is 0.5.

Any ideas?

Thanks,

John

[323 byte] By [DancingCodera] at [2007-11-26 23:45:33]
# 1
Maybe try adding an EmptyBorder.
camickra at 2007-7-11 15:17:54 > top of Java-index,Desktop,Core GUI APIs...
# 2

Even doing the following the separator is at the left end.

jSeparator3 = new JSeparator();

jSeparator3.setPreferredSize(new java.awt.Dimension(25,2));

jSeparator3.setOrientation(javax.swing.SwingConstants.VERTICAL);

jSeparator3.setBorder(new EmptyBorder(0,10,0,10));

DancingCodera at 2007-7-11 15:17:54 > top of Java-index,Desktop,Core GUI APIs...
# 3

The problem is that the UI always draws the lines at zero and one. The only way to change that is to change the UI, e.g.

import java.awt.Dimension;

import java.awt.Graphics;

import javax.swing.*;

import javax.swing.plaf.basic.BasicSeparatorUI;

public class SeparatorTest {

public static void main( String[] args ) {

Runnable doRun = new Runnable() { public void run() { new SeparatorTest(); } };

SwingUtilities.invokeLater( doRun );

}

public SeparatorTest() {

JFrame frame = new JFrame();

frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

BoxLayout bl = new BoxLayout( frame.getContentPane(), BoxLayout.X_AXIS );

frame.getContentPane().setLayout( bl );

frame.getContentPane().add( new JLabel( "One" ) );

JSeparator sep = new JSeparator( SwingConstants.VERTICAL );

sep.setUI( new BasicSeparatorUI() {

public void paint( Graphics g, JComponent c ) {

Dimension s = c.getSize();

int pos = s.width/2;

g.setColor( c.getForeground() );

g.drawLine( pos, 0, pos++, s.height );

g.setColor( c.getBackground() );

g.drawLine( pos, 0, pos, s.height );

}

} );

frame.getContentPane().add( sep );

frame.getContentPane().add( new JLabel( "Two" ) );

frame.pack();

frame.setVisible( true );

}

}

JayDSa at 2007-7-11 15:17:54 > top of Java-index,Desktop,Core GUI APIs...
# 4
Great. Thanks.
DancingCodera at 2007-7-11 15:17:55 > top of Java-index,Desktop,Core GUI APIs...
# 5
That sounds like a Java bug, then.I'd add that if you want a truly correct fix you ought to use the insets on all sides rather than just rendering the separator in the middle from top to bottom, and use the colors stored for separators in the UIManager.
itchyscratchya at 2007-7-11 15:17:55 > top of Java-index,Desktop,Core GUI APIs...