Mega Code Archive

 
Categories / Java Tutorial / Swing
 

Creating TitledBorder through its constructors and helper method from BorderFactory

A titled border is a combination of a title string and any of the borders. You control the position of the title with a justification. You can also specify the font and color of the title. To create a titled border, you need to specify the title and the border. 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: CENTER: Place the title in the center. DEFAULT_JUSTIFICATION: Use the default setting to position the text. The value is equivalent to LEFT. LEFT: Place the title on the left edge. 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: ABOVE_BOTTOM: Place the title above the bottom line. ABOVE_TOP: Place the title above the top line. BELOW_BOTTOM: Place the title below the bottom line. BELOW_TOP: Place the title below the top line. BOTTOM: Place the title on the bottom line. DEFAULT_POSITION: Use the default setting to place the text. This value is equivalent to TOP. 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);   } }