Question

To change all TitledBorder fonts, I am using UIManager:

UIManager.put("TitledBorder.font", new Font("Tahoma", Font.BOLD, 11));

But what to put to TitledBorder.border property to change only the color of the border (or maybe even it's width)?

Cheers

Was it helpful?

Solution

Just as using UIManager to change all TitledBorder font at once, to change TitledBorder borders use this function:

UIManager.put("TitledBorder.border", new LineBorder(new Color(200,200,200), 1));

It will change (set) the border property to the border object passed in a second parameter. All border types (even the factory class) description can be found here: http://docs.oracle.com/javase/tutorial/uiswing/components/border.html

This sample passes LineBorder object which takes color and width in a constructor just as you asked.

OTHER TIPS

Well, you can always specify any property in TitledBorder itself. Here is a fully customized example of Swing TitledBorder:

public static void main ( String[] args )
{
    LineBorder border = new LineBorder ( Color.RED, 3, true );
    TitledBorder tborder = new TitledBorder ( border, "Titled border", TitledBorder.CENTER,
            TitledBorder.DEFAULT_POSITION, new Font ( "Arial", Font.BOLD, 14 ), Color.BLUE );

    JFrame frame = new JFrame ();

    JLabel label = new JLabel ( "Some content label" );
    label.setBorder ( BorderFactory
            .createCompoundBorder ( BorderFactory.createEmptyBorder ( 50, 50, 50, 50 ),
                    BorderFactory.createCompoundBorder ( tborder,
                            BorderFactory.createEmptyBorder ( 15, 15, 15, 15 ) ) ) );
    frame.add ( label );

    frame.pack ();
    frame.setLocationRelativeTo ( null );
    frame.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
    frame.setVisible ( true );
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top