How can I set an EtchedBorder from BorderFactory class to a component in Java?

Java 8Object Oriented ProgrammingProgramming

Set an EtchedBorder from BorderFactory class −

EtchedBorder etchedBorder = (EtchedBorder)BorderFactory.createEtchedBorder();

Now, set it for a component −

JButton button = new JButton("Etched Border");
button.setBorder(etchedBorder);

The following is an example to set an EtchedBorder from BorderFactory class to a component −

Example

package my;
import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
public class SwingDemo {
   public static void main(String args[]) {
      JFrame frame = new JFrame("Demo");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      Border raisedBorder = new EtchedBorder(EtchedBorder.RAISED);
      EtchedBorder etchedBorder = (EtchedBorder)BorderFactory.createEtchedBorder();
      JButton raisedButton = new JButton("Raised Border");
      raisedButton.setBorder(raisedBorder);
      JButton button = new JButton("Etched Border");
      button.setBorder(etchedBorder);
      Container contentPane = frame.getContentPane();
      contentPane.add(raisedButton,BorderLayout.NORTH);
      contentPane.add(button,BorderLayout.EAST);
      frame.setSize(600, 300);
      frame.setVisible(true);
   }
}

Output


raja
Published on 04-Jun-2019 07:30:58
Advertisements