How to Create a Custom RadioButton In Java Netbeans
In this Java Tutorial we will see How To Create a Custom Radio Button with Hover Effects in Java Netbeans.
What We Are Gonna Use In This Project:
- Java Programming Language.- NetBeans Editor.
Project Source Code:
package new_tutorials;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.EmptyBorder;
/**
*
* @author 1BestCsharp
*/
public class CustomRadioButton extends JFrame {
private JRadioButton customRadioButton;
private JLabel radioButtonLabel;
public CustomRadioButton()
{
setTitle("Custom Radio Button");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,100);
setLocationRelativeTo(null);
initGUI();
}
private void initGUI()
{
customRadioButton = new JRadioButton(){
private boolean hovering = false;
@Override
protected void paintComponent(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// Draw the radio button border
g2d.setColor(Color.red);
g2d.drawOval(2, 2, getWidth() - 5, getHeight() - 5);
if(isSelected())
{
// Fill the radio button with orange color when selected
g2d.setColor(Color.orange);
g2d.fillOval(5, 5, getWidth() - 10, getHeight() - 10);
}
else if(hovering)
{
// Highlight when hovering
g2d.setColor(Color.lightGray);
g2d.fillOval(5, 5, getWidth() - 10, getHeight() - 10);
}
}
@Override
public void addMouseListener(MouseListener l)
{
// Add custom mouse listener for hover effect
super.addMouseListener(new MouseAdapter()
{
@Override
public void mouseEntered(MouseEvent e)
{
hovering = true;
repaint();
}
@Override
public void mouseExited(MouseEvent e)
{
hovering = false;
repaint();
}
@Override
public void mouseClicked(MouseEvent e)
{
// Toggle selection on click
setSelected(!isSelected());
}
});
}
};
customRadioButton.setOpaque(false);
customRadioButton.setFocusPainted(false);
customRadioButton.setBorderPainted(false);
customRadioButton.setContentAreaFilled(false);
customRadioButton.setBorder(new EmptyBorder(5,5,5,5));
radioButtonLabel = new JLabel("Custom RadioButton");
radioButtonLabel.setFont(new Font("Arial", Font.BOLD, 16));
radioButtonLabel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e)
{
customRadioButton.setSelected(!customRadioButton.isSelected());
}
});
getContentPane().setLayout(new BorderLayout());
JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
panel.setBackground(Color.white);
panel.add(customRadioButton);
panel.add(radioButtonLabel);
getContentPane().add(panel, BorderLayout.CENTER);
}
public static void main(String[] args) {
new CustomRadioButton().setVisible(true);
}
}
The Final Result:
More Java Projects:
Download Projects Source Code