Adding a Title to a Border - Java Swing

Java examples for Swing:Border

Description

Adding a Title to a Border

Demo Code

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

public class Main {
  public static void main(String[] args) {
    // Use default border.
    TitledBorder titledBorder = BorderFactory.createTitledBorder("Title");
    LineBorder border = null;/*  ww w  . j a  v a  2  s .c  o m*/
    // Create around existing border.
    titledBorder = BorderFactory.createTitledBorder(border, "Title");

    // Also available: DEFAULT_JUSTIFICATION, LEFT, RIGHT
    titledBorder.setTitleJustification(TitledBorder.CENTER);

    // Also available: DEFAULT_POSITION, ABOVE_TOP, TOP, ABOVE_BOTTOM, BOTTOM,
    // BELOW_BOTTOM
    titledBorder.setTitlePosition(TitledBorder.BELOW_TOP);

    new JButton().setBorder(titledBorder);
  }

}

Related Tutorials