Java Program to set title position in Java


To set title position, use the setTitlePosition() method in Java. Let’s say we have to position the title above the border's top line. For that, use the constant ABOVE_TOP for the border −

setTitlePosition(TitledBorder.ABOVE_TOP);

The following is an example to set title position −

Example

package my;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
public class SwingDemo {
   public static void main(String args[]) {
      JFrame frame = new JFrame("Demo");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      LineBorder lineBorder = new LineBorder(Color.orange);
      TitledBorder titledBorder = BorderFactory.createTitledBorder(lineBorder, "Demo Title");
      titledBorder.setTitlePosition(TitledBorder.ABOVE_TOP);
      JLabel label = new JLabel();
      label.setBorder(titledBorder);
      Container contentPane = frame.getContentPane();
      contentPane.add(label, BorderLayout.CENTER);
      frame.setSize(550, 300);
      frame.setVisible(true);
   }
}

Output

Updated on: 30-Jul-2019

736 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements