Sayantan Chandra
Sayantan Chandra

Reputation: 79

Java Titled Border

Is there any way to set font of the title of the titled border in java swing?

    JPanel panelDOB = new JPanel();
    panelDOB.setBorder(new TitledBorder(new LineBorder(new Color(171, 173, 179)), "DATE OF BIRTH", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(109, 109, 109)));
    panelDOB.setBackground(Color.WHITE);
    panelDOB.setFont(new Font("Tw Cen MT", Font.PLAIN, 12));
    sl_panelFac.putConstraint(SpringLayout.NORTH, panelDOB, 6, SpringLayout.SOUTH, txtNationality);
    sl_panelFac.putConstraint(SpringLayout.WEST, panelDOB, 10, SpringLayout.WEST, panelFac);
    sl_panelFac.putConstraint(SpringLayout.SOUTH, panelDOB, 61, SpringLayout.SOUTH, txtNationality);
    sl_panelFac.putConstraint(SpringLayout.EAST, panelDOB, 196, SpringLayout.WEST, panelFac);
    panelFac.add(panelDOB);
    panelDOB.setLayout(new SpringLayout());

Upvotes: 0

Views: 757

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

This is answered by simply looking up TitledBorder in the Java API.

There you'll find two constructors (here's one),

public TitledBorder(Border border,
                String title,
                int titleJustification,
                int titlePosition,
                Font titleFont)

that take a font as well as a method for setting the font,

public void setTitleFont(Font titleFont)

Alternatively, there are two methods in the BorderFactory that creates a TitledBorder with your selected font, again available through the Java API.

public static TitledBorder createTitledBorder(Border border,
                                          String title,
                                          int titleJustification,
                                          int titlePosition,
                                          Font titleFont)

I'm frankly a little surprised that you didn't look into the API before coming here.

Upvotes: 5

Related Questions