Thursday, April 11, 2019

Change jLabel Style and Design Setting in Java Netbeans

Change jLabel Style and Design Setting in Java NetBeans

In NetBeans, 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. So, let's Learn how to change jLabel style and design setting in java NetBeans.

We will proceed with following steps:
 (1) - Set Background color of jLabel
 (2) - Set Foreground Color and Font of jLabel
 (3) - Set text alignment of jLabel
 (4) - Set Border style in jLabel

Now, lets start with step by step process.

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

 (1) - Set Background color of jLabel:

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);  

 (2) - Set Foreground Color and Font of jLabel:

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 text alignment of jLabel:

You can set the alignment in two ways Horizontal and Vertical alignment. And both has following setting:
 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

 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 Border style in jLabel:

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 is different types of border style option. You can choose anyone step by step and fix whatever is looking stylish or desire.

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 jTable in java. You can easily handle it with GUI base and change jLabel style and design setting in java NetBeans.

0 comments: