Border Component in Java Swing

In Java Swing, a border is a component that can be added to a container or a component to provide a visual decoration or separation between the container and its contents. There are several types of borders available in Swing, including:

  1. LineBorder: Draws a single line around the component with a specified color and thickness.
  2. EtchedBorder: Draws a raised or lowered etched line around the component, depending on the type of border specified.
  3. BevelBorder: Draws a beveled edge around the component with either a raised or lowered appearance.
  4. TitledBorder: Draws a border around the component with a specified title.
  5. CompoundBorder: Allows us to combine two or more borders into a single border.

To add a border to a component, we can use the setBorder() method, which takes a Border object as an argument. For example, to add a line border with a red color and a thickness of 2 pixels to a JButton object, we can use the following code:

JButton button = new JButton("Click me");
button.setBorder(BorderFactory.createLineBorder(Color.RED, 2));

We can also create custom borders by extending the Border class and implementing its methods. This allows us to create borders with custom shapes or behaviors.

Reference: https://docs.oracle.com/javase/tutorial/uiswing/components/border.html

Sharing Is Caring: