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
# 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));
# 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 >
