I want to show titled separator in java swing application. Something like
-------Text-------
I found some third party libraries providing this functionality:
But I'm interested in a way without using any third party api. Can we do this by extending JSeparator? How else can we do that?
I think you might be able to use a combination of a MatteBorder and a TitledBorder
MatteBorder mb = new MatteBorder(1, 0, 0, 0, Color.BLACK);
TitledBorder tb = new TitledBorder(mb, "Some Long Text", TitledBorder.CENTER, TitledBorder.DEFAULT_POSITION);
Use a TitledBorder. Like this for example:
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.TitledBorder;
public class TestBorder {
protected void initUI() {
JFrame frame = new JFrame(TestBorder.class.getSimpleName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
TitledBorder titledBorder = BorderFactory.createTitledBorder("Some title");
titledBorder.setTitleJustification(TitledBorder.CENTER);
panel.setBorder(titledBorder);
frame.add(panel);
frame.setSize(400, 300);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TestBorder().initUI();
}
});
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With