LineBorder in Java Swing

In Java Swing, a LineBorder is a type of border that draws a line around a component. It is one of the predefined border classes in the javax.swing.border package.

To create a LineBorder for a component, we can use the LineBorder constructor and specify the color of the border and its thickness in pixels.

Following is an example code snippet that creates a JButton with a red line border of thickness 2 pixels:

import java.awt.Color;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;

public class LineBorderDemo {
	public static void main(String[] args) {
		JFrame frame = new JFrame("LineBorder Demo");
		JButton button = new JButton("Click me");
		button.setBorder(BorderFactory.createLineBorder(Color.RED, 2));
		frame.getContentPane().add(button);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}
}

In this example, the createLineBorder method of the BorderFactory class is used to create a LineBorder object with a red color and a thickness of 2 pixels. The setBorder method of the JButton class is then used to set the button’s border to the LineBorder object.

Output:

LineBorder in Java swing

We can adjust the color and thickness of the LineBorder as our need. Additionally, we can set the LineBorder as the border for other Swing components such as JTextField, JLabel, and JPanel.