How to add line border to JLabel in Java?

Java 8Object Oriented ProgrammingProgramming

Use the createLineBorder() method to ad line border to JLabel −

Border border = BorderFactory.createLineBorder(Color.ORANGE);
label.setBorder(border);

Above, we have set the line border to color orange.

The following is an example to add line border to JLabel −

Example

package my;
import java.awt.Color;
import java.awt.Font;
import javax.swing.*;
import javax.swing.border.Border;
public class SwingDemo {
   public static void main(String args[]) {
      JFrame frame = new JFrame("Demo");
      JLabel label;
      label = new JLabel("Demo Label!", JLabel.RIGHT);
      label.setFont(new Font("Verdana", Font.PLAIN, 13));
      Border border = BorderFactory.createLineBorder(Color.ORANGE);
      label.setBorder(border);
      frame.add(label);
      frame.setSize(500,300);
      frame.setVisible(true);
   }
}

Output

raja
Published on 09-May-2019 08:50:27
Advertisements