Reputation: 59670
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?
Upvotes: 3
Views: 5463
Reputation: 1
That is simple and lower case.
import javax.swing.border.*;
then
BevelBorder bedge=new BevelBorder(BevelBorder.RAISED);
lbl.setBorder(bedge);
-----------------next border type
import javax.swing.border.*;
then
TitledBorder tedge=new TitledBorder(TitledBorder.CENTER);
lbl.setBorder(tedge);
Upvotes: 0
Reputation: 347314
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);
Upvotes: 5
Reputation: 47607
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();
}
});
}
}
Upvotes: 4