How to create Titled Border for a component in Java?

Java 8Object Oriented ProgrammingProgramming

To create a titled border for a component in Java, use the createTitledBorder() method. Let’s say we have a panel and we need to set a titled border to it. Here’s our panel −

JPanel panel = new JPanel();

Now, set the border and set the text for the tites border −

panel.setBorder(BorderFactory.createTitledBorder(
   BorderFactory.createEtchedBorder(), "My Demo Table", TitledBorder.LEFT, TitledBorder.TOP));

The following is an example to create a titled border −

Example

package my;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.TitledBorder;
public class SwingDemo {
   public static void main(String[] args) {
      JFrame frame = new JFrame();
      JPanel panel = new JPanel();
      panel.setBorder(BorderFactory.createTitledBorder(
         BorderFactory.createEtchedBorder(), "My Demo Table", TitledBorder.LEFT, TitledBorder.TOP));
      String[][] rec = {
         { "001", "David", "AUS" },
         { "002", "Steve", "AUS" },
         { "003", "Yuvraj", "IND" },
         { "004", "Kane", "NZ" },
         { "005", "Ben", "ENG" },
         { "006", "Eion", "ENG" },
         { "007", "Miller", "SA" },
         { "008", "Rohit", "IND" }
      };
      String[] header = { "Id", "Player", "Team" };
      JTable table = new JTable(rec, header);
      panel.add(new JScrollPane(table));
      frame.add(panel);
      frame.setSize(550, 400);
      frame.setVisible(true);
   }
}

Output

raja
Published on 06-Jun-2019 09:07:22
Advertisements