Friday, April 12, 2019

Auto Resize Controls in Java Swing and Change Controls Name in Java

Auto Resize Controls in Java Swing and Change Controls Name in Java

Normally, if you design any application and want to provide facilities to Automatic Resize Controls in Java Swing according to application screen with NetBeans' platform, you can easily set some settings on controls, that will ease to use for further process.

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 Controls in Java Swing Using NetBeans:

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.
  1. Static way with right click “Change Variable Name…” setting. 
  2. Dynamic way through Coding at run time.
So, we start from first way:

1. Static way with right click “Change Variable Name…” setting:
In Static way, if we want to change Swing Control's Name, just select any control and right click on it and find option “Change Variable Name…”.

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

In this box, you will find the name of control. Now, change it whatever you want to change according to desire.

This Name will be used in Source code of Java for required process.

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:

2. Dynamic way through Coding at run time:
In dynamic 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 as follows:

 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 in Java and change name of controls in java NetBeans.

There are following more Learning tips in Java:

Messagebox in Java Swing through jOptionPane Step By Step Guide

SQL Server Connection in Netbeans through sqljdbc Driver

jRadioButton with Different Style and Look in Java Swing in Netbeans