How to create a LineBorder with BorderFactory class in Java?

Java 8Object Oriented ProgrammingProgramming

To create line border, use the createLineBorder() method. Let us first create a label component −

JLabel label;
label = new JLabel("Demo label");

Now, create line border with BorderFactory class. Here, we have also set the color for the line border −

label.setBorder(BorderFactory.createLineBorder(Color.yellow));

The following is an example to create a LineBorder with BorderFactory class −

package my;
import javax.swing.BorderFactory;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class SwingDemo {
   public static void main(String[] args) throws Exception {
      JFrame frame = new JFrame("Demo");
      JLabel label;
      label = new JLabel("Demo label");
      label.setFont(new Font("Arial", Font.BOLD, 18));
      label.setVerticalAlignment(JLabel.CENTER);
      label.setBorder(BorderFactory.createLineBorder(Color.yellow));
      frame.add(label);
      frame.setSize(550,350);
      frame.setVisible(true);
   }
}

Output

raja
Published on 31-May-2019 11:53:01
Advertisements