Java Program to add Titled Border to Panel in Swing

Java 8Object Oriented ProgrammingProgramming

To set Titled Border to Panel, let us first create a Panel for our Java Swing Application:

JPanel panel = new Jpanel(new BorderLayout());

Now, set the titled border:

panel.setBorder(new TitledBorder("Displaying Titled Border"));

The following is an example to add Titled Border to Panel in Swing:


import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
public class SwingDemo {
   public static void main(String[] args) {
      JPanel panel = new JPanel(new BorderLayout());
      panel.setBorder(new TitledBorder("Displaying Titled Border"));
      panel.add(new JButton("Demo Button"), BorderLayout.SOUTH);
      JOptionPane.showMessageDialog(null, panel);


Updated on 30-Jul-2019 22:30:26