How to set the titled justification (position title text) of the titled border in Java

Java 8Object Oriented ProgrammingProgramming

To set the titled justification of the titled border, use the setTitleJustification() method. The following is an example to set the title justification of the titled border −

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 linedBorder = new LineBorder(Color.YELLOW);
      TitledBorder titledBorder = BorderFactory.createTitledBorder(linedBorder, "Demo Title");
      titledBorder.setTitleJustification(TitledBorder.LEFT);
      JLabel label = new JLabel();
      label.setBorder(titledBorder);
      Container contentPane = frame.getContentPane();
      contentPane.add(label, BorderLayout.CENTER);
      frame.setSize(550, 300);
      frame.setVisible(true);
   }
}

Output


raja
Published on 06-Jun-2019 10:00:48
Advertisements