1. JLabel Border and BorderFactory
Java Swing API provides BorderFactory class to set up many styles of borders. The Swing component class can use these borders to know how to draw its edges. For example, the swing control can draw a line around its edge using the Line Border. The factory class exposes a function called createLineBorder and we can use it to create the LineBorder object. In the same way, we can create other borders also with the user-friendly function names. For example, to draw a dashed line as a border, we will make a call to the BorderFactory function createDashedBorder. One more example: createBevelBorder.
The Java Swing components take these borders via setBorder method and draw the surrounding border. In this example, we will create some JLabels and use varied border styles around them.
2. Font & Swing JLabel
Like the AWT components, the swing component also supports fonts. We can create Font object with three attributes: name, style, and size. The name param is to denote the Font Name like Arial, Verdana. Font style param is to mention Normal, Italic, etc. The size tells how big the letters should display. Once Font is ready, we can assign it to any Swing Component using the setFont method.
3. About This JLabel Border Example
The below picture shows the Finished example:

This example has four JLabels in it and a FlowLayout Manager is taking care of laying out these labels. Each label uses a unique border to draw around it. Since JLabel is a component, one can apply the same technique for other components as well. For example, a JTextField can use same technique to draw the border. Now, we can have a look at the Bevel Raised border in the picture above and the JLabel looks like a button, right? But it is not. We also set a Font for all the JLabels in this example.
4. Create Swing JLabels
After setting the size and position for the JFrame Window, we create 4 JLabels. These labels will later use four types of borders around it.
1 2 3 4 5 6 7 8 9 10 |
//Sample 01: Set Size and Position setBounds(100, 100, 400, 300); Container ControlHost = getContentPane(); ControlHost.setLayout(new FlowLayout()); //Sample 02: Create Four Labels JLabel lbl1 = new JLabel("Line Border"); JLabel lbl2 = new JLabel("Bevel Raised"); JLabel lbl3 = new JLabel("Soft Bevel Border"); JLabel lbl4 = new JLabel("Etched Border"); |
5. Setup JLabel Border
Once JLabels are ready, we create four types of borders from the
BorderFactory. For the method
createBevelBorder, we pass the constant
RAISED and you can pass other constant to create other border effects. Likewise, while making the Line Border, we pass the color object to the BorderFactory method
createLineBorder. The
setBorder method of JLabel will take a border and use it to draw a border around it. One can use setBorder
method on any Java Swing component based object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
//Sample 03: Create Borders Border BevelRaised = BorderFactory.createBevelBorder(BevelBorder.RAISED); Border SoftBevelLowered = BorderFactory.createSoftBevelBorder(SoftBevelBorder.LOWERED); Border EtchedBorder = BorderFactory.createEtchedBorder(); Border LineBorder = BorderFactory.createLineBorder(Color.RED); //Sample 04: Assigning Border to Labels lbl1.setBorder(LineBorder); lbl2.setBorder(BevelRaised); lbl3.setBorder(SoftBevelLowered); lbl4.setBorder(EtchedBorder); |
6. JLabel with Font
Next, we create an AWT normal style Font
object with a font size of 16. The font name is Verdana. Since Swing Components are built on top of AWT components, we can make use of the
setFont method to assign a font out JLabel. All our four JLabels, take this same Font. Below is the code:
1 2 3 4 5 6 |
//Sample 05: Set Font to Labels Font font = new Font("Verdana", Font.PLAIN, 16); lbl1.setFont(font); lbl2.setFont(font); lbl3.setFont(font); lbl4.setFont(font); |
7. Youtube – Demo
8. Code Reference
8.1 MainEntry.java
1 2 3 4 5 6 7 8 9 |
package tube.coding.examples; public class MainEntry { public static void main(String[] args) { //Sample 07: Create Instance of JFrameDemo JLabelFontBorder frame = new JLabelFontBorder("JLabel Font & Borders"); frame.setVisible(true); } } |
8.2 JLabelFontBorder.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
package tube.coding.examples; import java.awt.Color; import java.awt.Container; import java.awt.FlowLayout; import java.awt.Font; import java.awt.HeadlessException; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.border.BevelBorder; import javax.swing.border.Border; import javax.swing.border.EtchedBorder; import javax.swing.border.SoftBevelBorder; public class JLabelFontBorder extends JFrame { public JLabelFontBorder(String title) throws HeadlessException { super(title); //Sample 01: Set Size and Position setBounds(100, 100, 400, 300); Container ControlHost = getContentPane(); ControlHost.setLayout(new FlowLayout()); //Sample 02: Create Four Labels JLabel lbl1 = new JLabel("Line Border"); JLabel lbl2 = new JLabel("Bevel Raised"); JLabel lbl3 = new JLabel("Soft Bevel Border"); JLabel lbl4 = new JLabel("Etched Border"); //Sample 03: Create Borders Border BevelRaised = BorderFactory.createBevelBorder(BevelBorder.RAISED); Border SoftBevelLowered = BorderFactory.createSoftBevelBorder(SoftBevelBorder.LOWERED); Border EtchedBorder = BorderFactory.createEtchedBorder(); Border LineBorder = BorderFactory.createLineBorder(Color.RED); //Sample 04: Assigning Border to Labels lbl1.setBorder(LineBorder); lbl2.setBorder(BevelRaised); lbl3.setBorder(SoftBevelLowered); lbl4.setBorder(EtchedBorder); //Sample 05: Set Font to Labels Font font = new Font("Verdana", Font.PLAIN, 16); lbl1.setFont(font); lbl2.setFont(font); lbl3.setFont(font); lbl4.setFont(font); //Sample 06: Add Labels to Content Pane ControlHost.add(lbl1); ControlHost.add(lbl2); ControlHost.add(lbl3); ControlHost.add(lbl4); } } |
Categories: Swing
Tags: BorderFactory, createBevelBorder, createLineBorder, JLabel, setBorder, setFont