How To Use and Set Border In Java NetBeans
In this Java Tutorial we will see How To Set Borders to JLabel, JPanel, JTextFiled In Java NetBeans .
in java you can set different borders to different elemnts and you can set the size and the color you want to this border.
and to do that you need to create the border before using LineBorder.createGrayLineBorder() OR BorderFactory.createLineBorder(Color.NAME, SIZE) OR
BorderFactory.createMatteBorder(top ,left, bottom, right, Color.NAME)
WATCH THIS JAVA TUTORIAL
Project Source Code:
// import those packages to make it work
import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
// create and set a border to the green jpanel
Border jpBorder1 = LineBorder.createGrayLineBorder();
Border jpBorder2 = BorderFactory.createLineBorder(Color.black, 2);
//jPanel2.setBorder(jpBorder2);
// create and set a border with size to the JLabel
Border lblBorder = BorderFactory.createLineBorder(Color.BLUE, 25);
// create a border with diffrent size in the top ,left, bottom, right
Border border = BorderFactory.createMatteBorder(2, 4, 8, 16, Color.yellow);
jLabel1.setBorder(border);
Border border2 = BorderFactory.createMatteBorder(0, 0, 5, 0, Color.black);
jPanel2.setBorder(border2);
jTextField1.setBorder(border2);
OutPut:
Download Projects Source Code