Change Style And Design of jLabel In Java Swing With NetBeans

In java programming, jLabel play a different types of roll in programming an application. jLabel has capability to show images on your application. It can also show the different type of appearance style in java. There is no need to add any image viewer to show image. But in this post we learn how to change jLabel style and design of jLabel in Java Swing NetBeans?


We will proceed with following steps:
(1). Set jLabel Background Color
(2). Set jLabel Foreground Color and jLabel Font Settings
(3). Set jLabel Text Alignment
(4). Set jLabel Border Style

Now, lets start with step by step process.

How to set or display image in jFrame with jLabel in java?


(1). Set jLabel Background Color :

First of all you have to understand the property of any control in java NetBeans. Maximum setting will be done through mouse click on Properties window. You can add this window on any side or open it through right click of any control. So, First we Set background color of jLabel.

If you think that is simple to go to properties window and click on “background” option and choose any color to set back style. Then you are wrong, there will no changes on it like bellow image.

jLabel background color setting
Background Color Setting
As you can see that you have to setting on two option first “opaque” and then “background” option for color selection.

Process:

➤ Go to “opaque” option and tick mark on it.

➤ After that go to “background” option and choose desire color.

Or you can use code in application as like:

 MyLbl1.setOpaque(true);
 MyLbl1.setBackground(Color.cyan);  

Auto Resizing Controls in jFrame in Java Swing


(2). Set jLabel Foreground Color and jLabel Font Settings:

For font setting just go to “font” option of Properties window and choose font name, size and type. And in same way go to “foreground” option and choose font color whatever you want just describe and mention in bellow image.

jLabel Forecolor and Font setting
Foreground Color and Font Setting

If you want to set it through code, then you have to write bellow code in jframe's "initComponents" or "WindowsOpened" event just same as following:

Font fnt = new Font("Calibri",Font.BOLD,18);
MyLbl1.setFont(fnt);
MyLbl1.setForeground(Color.GREEN);

whatever color or font you choose from this settings for your application.


(3). Set jLabel Text Alignment:

You can set the alignment in two ways Horizontal and Vertical alignment. And both has following setting:
 jLabel Horizontal Alignment
 Trailing
 Right
 Left
 Leading
 Center

there is following code of horizontal alignment, you can use without properties setting.

 MyLbl1.setHorizontalAlignment(SwingConstants.CENTER);
 MyLbl1.setHorizontalAlignment(SwingConstants.RIGHT);
 MyLbl1.setHorizontalAlignment(SwingConstants.LEFT);

or

 MyLbl1.setHorizontalAlignment(0);  //center
 MyLbl1.setHorizontalAlignment(4);  // right
 MyLbl1.setHorizontalAlignment(2); // left


Open jFrame Though Another jFrame with Button's Click in Java Swing


 jLabel Vertical Alignment
 Bottom
 Top
 Center

You can use code for vertical alignment at run time.

 MyLbl1.setVerticalAlignment(SwingConstants.TOP);
 MyLbl1.setVerticalAlignment(SwingConstants.BOTTOM);
 MyLbl1.setVerticalAlignment(SwingConstants.CENTER);

or

 MyLbl1.setVerticalAlignment(1); //top 
 MyLbl1.setVerticalAlignment(0); //middle
 MyLbl1.setVerticalAlignment(3); //bottom

When you select any one of these options, the text will adjust according to alignment setting like bellow.
Horizontal Alignment setting
Horizontal Alignment Setting

Vertical Alignment setting in Netbeans
Vertical Alignment Setting
In this way we can set the alignment of text.


(4). Set jLabel Border style:

For border setting just apply the same process and select “border” option. In this option there is following border style option will show.
Border Setting in java Netbeans
Border Setting
You can see there are different types of border style option. You can choose anyone step by step and fix whatever is looking stylish for your desire.

Design Swing GUI Application with jFrame in Java Swing


If you want to change border style at run time, you have to write code as mention bellow. We define different type of border style in code:


  Border brdr = BorderFactory.createLineBorder(Color.RED);
  MyLbl1.setBorder(brdr); // for Line Border

  Border brdr1 = BorderFactory.createBevelBorder(1);
  MyLbl1.setBorder(brdr1); // for Bevel Border

  Border brdr2 = BorderFactory.createEtchedBorder(Color.red, Color.yellow);
  MyLbl1.setBorder(brdr2); // for Etched Border


In this way you can set background, foreground, font, alignment and border style of jLable in java. You can easily handle it with GUI base and Change Style and Design of jLabel in Java Swing with NetBeans with SKOTechLearn Tips.

Use of jRadioButton with Look and Style Setting in Java Swing

0 comments: