Friday, April 12, 2019

Auto resize control and change name of controls in java NetBeans

Auto resize control and change name of controls in java NetBeans

Normally, if you design any application and want to provide facilities to automatic resize controls according to application screen, then in java NetBeans, you can easily set some settings on controls, that will ease to use for further process. And in this post we will also describe the process to change the name of any controls according to desire. So, here SKOTechLearn will learn how to auto resize controls and change name of controls in java NetBeans.

Change background, foreground, font, alignment and border style of jLabel

 Auto resizing process of Controls:

First, we learn Auto resizing process of Controls.

Drag some controls from “Palette” window, whatever you want to add and select all controls or select one and right click on it. It will show you many option.

Find “Auto Resizing” option. In this option contains two sub options:

You can select both or any one option. The following image show the complete process.
Auto Resize Setting
When you apply these steps and when you run application, the controls will automatic adjust with the calculation of jFrame height width and control’s height width.
Auto resize at run time

 How to change Controls Name in Java:

If you are using Netbeans for application development, you can change name through two way.

First is through Static way with right click “Change Variable Name…” setting.

Second is through coding in dynamic way at run time.

So, we start from first way:

In this way just select any control and right click on it and find option “Change Variable Name…”.

Click on that option, it will show you small screen with “Rename” name.

In this screen, you will find the name of control. Change it whatever you want to input.

This way, we can change the name.

How to upload or show image in jLabel in java?

The following image will show the step by step process

Change control name Process in java
Change Variable Name Process
Now come to second way:

In this way we can create controls dynamically at run time, you can write code for it in “WindowsOpened” event or initialize component in class.

But first you have to import some Libraries for component. Suppose, if you want to add some buttons and Labels, then import following libraries class.

 import javax.swing.JButton;
 import java.awt.Label;

After that Write code like bellow:

 public TestJFrame() {                          
    JButton MYbtn = new JButton(); // create jButton with desire Name
    Label Mylbl = new Label();    // create jLabel with desire Name
    Mylbl.setLocation(20, 20);   //set the location of jLabel
    Mylbl.setText("Dynamic Lable");
    Mylbl.setSize(100, 20);    // set the size of jLabel
    MYbtn.setLocation(20, 60); //set the location of jButton
    MYbtn.setSize(120, 25);   // set the size of jButton
    MYbtn.setText("Dynamic Button");
    MyPanel1.add(Mylbl);    //add jLabel on jPanel
    MyPanel1.add(MYbtn);    //add jButton on jPanel
After that when you run application, it will looks like bellow.
Dynamic add control with change name in java
Dynamic Control with Change Name
So friends, apply these steps and create whatever you want in java Netbeans. Now SKOTechLearn has explain how to Auto resize control and change name of controls in java NetBeans.