Syntax error How to set TitiledBorder Direction in Java?

How to set TitiledBorder Direction in Java?



To set TitleBorder direction, you need to use the constants and set it for border. For example, for direction center −

TitledBorder border = BorderFactory.createTitledBorder("Border");
border.setTitlePosition(TitledBorder.CENTER);

Above, we have set the setTitlePosition() for the direction.

The following is an example to set TitledBorder direction in Java −

Example

package my;
import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
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);
      TitledBorder border = BorderFactory.createTitledBorder("Border");
      border.setTitlePosition(TitledBorder.CENTER);
      TitledBorder border2 = new TitledBorder(
         border, "Above_Bottom CENTER Border", TitledBorder.CENTER,
      TitledBorder.ABOVE_BOTTOM);
      JLabel label = new JLabel();
      label.setBorder(border2);
      Container contentPane = frame.getContentPane();
      contentPane.add(label, BorderLayout.CENTER);
      frame.setSize(550, 300);
      frame.setVisible(true);
   }
}

Output

Updated on: 2019-07-30T22:30:26+05:30

158 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements