How to create a compound border for a component in Java?

Java 8Object Oriented ProgrammingProgramming

Composed border consists of two or more borders i.e. border around a border. We can create it for a component in Java using the createCompoundBorder() method.

Let’s say the following is our component −

JLabel label;
label = new JLabel("This has compound border (border around a border)!");

Now, set the compound border −

label.setBorder(BorderFactory.createCompoundBorder(BorderFactory
   .createRaisedBevelBorder(), BorderFactory.createLoweredBevelBorder()));

The following is an example to create a compound border for a component −

Example

package my;
import javax.swing.BorderFactory;
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("This has compound border (border around a border)!");
      label.setFont(new Font("Arial", Font.BOLD, 18));
      label.setVerticalAlignment(JLabel.CENTER);
      label.setBorder(BorderFactory.createCompoundBorder(BorderFactory
      .createRaisedBevelBorder(), BorderFactory.createLoweredBevelBorder()));
      frame.add(label); frame.setSize(550,350); frame.setVisible(true);
   }
}

Output

raja
Published on 06-Jun-2019 12:38:34
Advertisements