Creating TitledBorder through its constructors and helper method from BorderFactory : TitiledBorder « Swing « Java Tutorial






  1. A titled border is a combination of a title string and any of the borders.
  2. You control the position of the title with a justification.
  3. You can also specify the font and color of the title.
  4. To create a titled border, you need to specify the title and the border.
  5. If no border is specified, an etched border is used by default.
public TitledBorder(Border border)
Border titledBorder = new TitledBorder(lineBorder);

public static TitledBorder createTitledBorder(Border border)
Border titledBorder = BorderFactory.createTitledBorder(lineBorder);


public TitledBorder(String title)
Border titledBorder = new TitledBorder("Hello");

public static TitledBorder createTitledBorder(String title)
Border titledBorder = BorderFactory.createTitledBorder("Hello");


public TitledBorder(Border border, String title)
Border titledBorder = new TitledBorder(lineBorder, "Hello");

public static TitledBorder createTitledBorder(Border border, String title)
Border titledBorder = BorderFactory.createTitledBorder(lineBorder, "Hello");


public TitledBorder(Border border, String title, int justification, int position)
Border titledBorder = new TitledBorder(lineBorder, "Hello", TitledBorder.LEFT, TitledBorder.BELOW_BOTTOM);
public static TitledBorder createTitledBorder(Border border, String title, int justification, int position)
Border titledBorder = BorderFactory.createTitledBorder(lineBorder, "Hello", TitledBorder.LEFT, TitledBorder.BELOW_BOTTOM);


public TitledBorder(Border border, String title, int justification, int position, Font font)
Font font = new Font("Serif", Font.ITALIC, 12);
Border titledBorder = new TitledBorder(lineBorder, "Hello", TitledBorder.LEFT, TitledBorder.BELOW_BOTTOM, font);

public static TitledBorder createTitledBorder(Border border, String title, int justification, int position, Font font)
Font font = new Font("Serif", Font.ITALIC, 12);
Border titledBorder = BorderFactory.createTitledBorder(lineBorder, "Hello", TitledBorder.LEFT, TitledBorder.BELOW_BOTTOM, font);

public TitledBorder(Border border, String title, int justification, int position, Font font, Color color)
Font font = new Font("Serif", Font.ITALIC, 12);
Border titledBorder = new TitledBorder(lineBorder, "Hello", TitledBorder.LEFT, TitledBorder.BELOW_BOTTOM, font, Color.RED);

public static TitledBorder createTitledBorder(Border border, String title, int justification, int position, Font font, Color color)
Font font = new Font("Serif", Font.ITALIC, 12);
Border titledBorder = BorderFactory.createTitledBorder(lineBorder, "Hello", TitledBorder.LEFT, TitledBorder.BELOW_BOTTOM, font, Color.RED);

Text justification of the title string within a TitledBorder is specified by one of four class constants:

  1. CENTER: Place the title in the center.
  2. DEFAULT_JUSTIFICATION: Use the default setting to position the text. The value is equivalent to LEFT.
  3. LEFT: Place the title on the left edge.
  4. RIGHT: Place the title on the right edge.

Position title strings in any one of six different locations, as specified by one of seven class constants:

  1. ABOVE_BOTTOM: Place the title above the bottom line.
  2. ABOVE_TOP: Place the title above the top line.
  3. BELOW_BOTTOM: Place the title below the bottom line.
  4. BELOW_TOP: Place the title below the top line.
  5. BOTTOM: Place the title on the bottom line.
  6. DEFAULT_POSITION: Use the default setting to place the text. This value is equivalent to TOP.
  7. TOP: Place the title on the top line.
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Sample extends JFrame {

  public Sample() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel();
    JLabel label;


    label = new JLabel("Titled border");
    label.setBorder(BorderFactory.createTitledBorder("Titled"));
    panel.add(label);
    getContentPane().add(panel);
    pack();
  }

  public static void main(String[] args) {
    Sample s = new Sample();
    s.setVisible(true);
  }
}








14.101.TitiledBorder
14.101.1.Creating TitledBorder through its constructors and helper method from BorderFactory
14.101.2.Nested TitiledBorderNested TitiledBorder
14.101.3.Setting TitiledBorder DirectionSetting TitiledBorder Direction
14.101.4.Set title position
14.101.5.TitleBorder Title PositionTitleBorder Title Position
14.101.6.TitledBorder based on LineBorderTitledBorder based on LineBorder
14.101.7.TitleBorder JustificationTitleBorder Justification
14.101.8.Create a title border from another border
14.101.9.Change border text Justification alignment style
14.101.10.Customizing TitledBorder Look and Feel