Thursday, June 20, 2019

Change Look of jRadioButton through Image in java Netbeans

Change Look of jRadioButton through Image in java Netbeans

In this article SKOTechLearn Tips will show you the features of jradiobutton, In java you can design your controls or swing controls according to your need. We will learn how to change different types of look of jradiobutton through image in java Netbeans.
Different look of jRadiobutton
jRadioButton's Looks

 Default RadioButton to Graphical RadioButton:

First of all you have to create some image just look like radio button. For designing it, you can use Powerpoint or googling image and modify according to you desire look.

Or you can save following:

OptionButton’s icon category 1: JOption1 for set icon  JOption1 icon for focus  JOption1 icon for select

OptionButton’s icon category 2: JOption2 for set icon  JOption2 icon for focus  JOption2 icon for select

OptionButton’s icon category 3: JOption3 for set icon  JOption3 icon for focus  JOption3 icon for select


These above images are in PNG format with transparent background, you can right click and save it and use it on your project.

After designing and creating image, just go to your Project’s Package and copy all images and paste it on package.
copy files in java package process
Copy Files in java package
When you will copy it,  come to following points.

 Add image for front view:
Now go to you jRadioButton’s Properties screen.
Find “icon” option and select your desire image.

 Add image for Focus:
In Properties screen, find “rolloverIcon” option.
And select focus icon from here.

It will change during focus on control.

 Now last, add icon when you select it:
Just same find option “selectedIcon”. And add picture you have created.

The following picture defines all steps.
Set image icon in jradiobutton
Different types of icons selection Process
So, this way you can easily change look and feel of jRadioButton in java.

 How to change look of jRadioButton through code in java? 

If you want to proceed through coding during runtime change process, here is the following way.
Define all icon in Application’s initComponents events.

public JRadioButton_Look() { 
        initComponents();
        try{
       // Here MyOptionBtn1 is jRadioButton.
        BufferedImage imgico = ImageIO.read(getClass().getResource("JBtn1.png"));
        MyOptionBtn1.setIcon(new ImageIcon(imgico));
        
         BufferedImage imgicofocus = ImageIO.read(getClass().getResource("JBtn1_Focus.png"));
        MyOptionBtn1.setRolloverIcon(new ImageIcon(imgicofocus));
        // setRolloverIcon is for when your cursor move on it or focus on it.
        
         BufferedImage imgicoselect = ImageIO.read(getClass().getResource("JBtn1_select.png"));
        MyOptionBtn1.setSelectedIcon(new ImageIcon(imgicoselect));
         // setSelectedIcon is for when you select this option.

        }catch(Exception ex1){}
       
        }

There is another long way through code where you have to write code in control’s FocusGained, FocusLost, MouseMoved, ItemStateChanged events. After that you will find same look wise process in it.

 How to transparent background of jRadioButton? 

When you drag jRadioButton, it will present in background default color after changing the icon it will contain the background default color as shown bellow.
Default background color of RadioButton
Default background color
So, the point is that how to transparent it?

The solution is that you have to unchecked the “opaque” option from “Properties” screen. And the magic will present. The background color will be invisible just same as define following.
Transparent background of RadioButton
Transparent background Process
So, you can understand easily that in Java, you can change look of jRadioButton through image with SKOTechLearn Tips.

0 comments: