Using BorderFactory to Create Borders
To add a border to a component or container, you can use the java BorderFactory which provides many types of Swing borders. The three steps to creating a border are:- Create a border by instantiating the Border.
- Choose border style with the BorderFactory.create... ().
- Modify the JComponent (component) with the method setBorder.
LineBorder
This method BorderFactory.createLineBorder(Color, int) creates a single border. The first parameter represents the color and the second, the width of the line.The following code creates a Border in JLabel and JPanel.
import java.awt.BorderLayout;Output
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;
public class BorderFactoryExample extends JPanel{
public BorderFactoryExample(){
JFrame f = new JFrame("BorderFactoryExample");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
JPanel panel = new JPanel();
JLabel label = new JLabel(" lineBorder");
label.setPreferredSize(new Dimension(100, 100));
panel.add(label);
panel.setPreferredSize(new Dimension(200,125));
//create a black line border and minimum width 1
Border lineborder = BorderFactory.createLineBorder(Color.black, 1);
//associate with JLabel
label.setBorder(lineborder);
f.getContentPane().setLayout(new BorderLayout());
f.add(new JLabel(" "), BorderLayout.NORTH);
//adjust to center
f.add(panel, BorderLayout.CENTER);
f.pack();
}
public static void main(String[] args) {
new BorderFactoryExample();
}
}
EtchedBorder
Etched means engraved in French, either from the outside: EtchedBorder.RAISED, or from the inside: EtchedBorder.LOWERED.RAISED
Border EtchedBorderRaised = BorderFactory.createEtchedBorder(EtchedBorder.RAISED);
Border EtchedBorderLowered = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
RaisedBevelBorder
Creates a border as a loose button with light shades and darker shades for shadows.
Creates a border as a pressed button with light and darker shades for shadows.
Border LoweredBevelBorder = BorderFactory.createLoweredBevelBorder();
Create a border with 5 parameters, the first 4 process the width of the left, right, top and bottom line, and the last one to color the edges.
Border matteborder = BorderFactory.createMatteBorder(1, 1, 3, 1, Color.blue);
You can also make it prettier by adding an icon that redesigns around the border:
ImageIcon icon = new ImageIcon("etoile.png");
Border matteborder = BorderFactory.createMatteBorder(1, 1, 3, 1, icon);
TitledBorder
TitledBorder has five constructors, we chose to make an example with the one that includes all the parameters that allow you to add a title to a border with a specific border style, position, font and color.
Border LoweredBevelBorder = BorderFactory.createLoweredBevelBorder();
Border EtchedBorderRaised = BorderFactory.createTitledBorder(LoweredBevelBorder, "title",
TitledBorder.LEFT,TitledBorder.TOP,
new Font("Arial", Font.PLAIN , 13), Color.black);
EtchedBorder
This style combines two styles into one. For example, we want to draw a LineBorder inside RaisedBevelBorder:
Border lineborder = BorderFactory.createLoweredBevelBorder();
Border RaisedBevelBorder = BorderFactory.createLineBorder(Color.blue);
Border EtchedBorderRaised = BorderFactory.createCompoundBorder(lineborder,
RaisedBevelBorder);
Oracle Documentation: BorderFactory
java2s: create Use BorderFactory