Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

JLabel Border and Font

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:

JLabel Border and Font Example
JLabel Border and Font 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.

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.

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:

7. Youtube – Demo

8. Code Reference

8.1 MainEntry.java

8.2 JLabelFontBorder.java

Categories: Swing

Tags: , , , , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.