Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Titled separator in java swing

I want to show titled separator in java swing application. Something like

-------Text-------

I found some third party libraries providing this functionality:

  • http://www.jidesoft.com/javadoc/com/jidesoft/swing/TitledSeparator.html
  • http://www.jaxfront.org/member/java-core-api/com/jgoodies/forms/builder/PanelBuilder.html

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?

like image 434
Harry Joy Avatar asked Jul 31 '26 19:07

Harry Joy


2 Answers

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);
like image 172
MadProgrammer Avatar answered Aug 02 '26 07:08

MadProgrammer


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();
            }
        });
    }

}
like image 28
Guillaume Polet Avatar answered Aug 02 '26 09:08

Guillaume Polet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!