EtchedBorder in Java Swing

EtchedBorder is a class in the Java Swing library that can be used to create a border with an etched or raised effect. It is a subclass of the AbstractBorder class, which provides a basic implementation for creating borders.

To use EtchedBorder, we first need to create an instance of the class and set any properties we want to customize. For example, we can set the border style to either etched or raised, and we can specify the color of the border.

Following is an example of how to create an EtchedBorder with a raised style and a gray color:

import javax.swing.*;
import javax.swing.border.*;

// create a panel with an etched border
JPanel panel = new JPanel();
Border border = BorderFactory.createEtchedBorder(EtchedBorder.RAISED, Color.GRAY);
panel.setBorder(border);

In this example, the createEtchedBorder method of the BorderFactory class is used to create a new EtchedBorder instance with a raised style and a gray color. The border object is then set as the border of a JPanel component.

We can also customize the thickness of the border by passing an additional parameter to the createEtchedBorder method:

Border border = BorderFactory.createEtchedBorder(EtchedBorder.RAISED, Color.YELLOW, Color.GRAY);

In this case, the third parameter specifies the color of the shadow that is used to create the etched effect.

The complete example code is given below;

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;

public class EtchedBorderDemo extends JFrame {

	public EtchedBorderDemo() {
		setTitle("EtchedBorder Demo");
		// set up the frame
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(400, 400);
		
		setLayout(new BorderLayout());

		// create a panel with an etched border
		JPanel panel = new JPanel();
		Border border = BorderFactory.createEtchedBorder(EtchedBorder.RAISED, Color.YELLOW, Color.GRAY);
		panel.setBorder(border);

		// add some components to the panel
		JLabel label = new JLabel("Hello, world!");
		panel.add(label);

		JButton button = new JButton("Click me!");
		panel.add(button);

		JTextField textField = new JTextField("Type something...");
		panel.add(textField);

		// add the panel to the frame
		getContentPane().add(panel, BorderLayout.NORTH);

		// display the frame
		setVisible(true);
	}

	public static void main(String[] args) {
		new EtchedBorderDemo();
	}

}

This code creates a new JFrame instance and sets its size and close operation. Then it creates a JPanel with an etched border and adds some Swing components to it (a JLabel, a JButton, and a JTextField). Finally, the panel is added to the frame and the frame is displayed.

Output:

EtchedBorder in Java Swing EtchedBorder demo 1
EtchedBorder in Java Swing 2

EtchedBorder can be used to create borders for any Swing component that supports borders, including JButton, JLabel, JTextField, and others.

Reference: https://docs.oracle.com/javase/7/docs/api/javax/swing/border/EtchedBorder.html