James Gosling: idealism, the Internet and Java, Pt I

Wednesday, 6 January 2021

Java - Swing custom components - Rounded JButton

Unlike AWT, Java Swing tool kit provides feasibilty to create custom components and containers.

To understand Java GUI toolkits more clearly, please read my other articles on Swing and AWT. 

In this article I focus more on building custom components.

Let us see how to create simple Rounded JButton. The below program demonstrates How rounded JButton can be created and added to a JFrame.

 

Program:


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class RoundedJButton extends JButton {

/**
*/
private static final long serialVersionUID = 1L;

public RoundedJButton(String title) {
super(title);
Dimension size = getPreferredSize();
size.width = size.height = Math.max(size.width, size.height);
setPreferredSize(size);

setContentAreaFilled(false);
}

@Override
protected void paintComponent(Graphics g) {
if (getModel().isArmed()) {
g.setColor(Color.lightGray);
} else {
g.setColor(getBackground());
}
g.fillOval(0, 0, getSize().width - 1, getSize().height - 1);

super.paintComponent(g);
}

@Override
protected void paintBorder(Graphics g) {
g.setColor(getForeground());
g.drawOval(0, 0, getSize().width - 1, getSize().height - 1);
}

Shape shape;

@Override
public boolean contains(int x, int y) {
if (shape == null || !shape.getBounds().equals(getBounds())) {
shape = new Ellipse2D.Float(0, 0, getWidth(), getHeight());
}
return shape.contains(x, y);
}

public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {

JFrame frame = new JFrame();
frame.setTitle("It's all about Java...");
frame.setSize(150, 150);
GridLayout layout = new GridLayout();
layout.setRows(1);
layout.setHgap(10);
layout.setVgap(10);
frame.setLayout(layout);

JTextField field1 = new JTextField();
field1.setText("This is a textfield!");
frame.getContentPane().add(field1);

RoundedJButton button = new RoundedJButton("Browse");
button.setBackground(Color.gray);
button.setForeground(Color.GREEN);
frame.getContentPane().add(button);

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
frame.pack();
frame.setVisible(true);
}
});
}
}

There are two parts in the program. First part creates RoundedJButton component by extending JButton. and second part creates JFrame and appends two components. 1. RoundedJButton and 2. JTextField

First Part:


RoundedJButton overrides paintComponent(Graphics g) and paintBorder(Graphics g) to get the circled shape to the Button. 

Second Part:

Second part contains main method which creates JFrame object and adds 2 components to JFrame dialog.

Also applies System look and feel by using following

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

I will explain clearly about pluggable look and feels also known as PLAF in future articles.

That's all for now!

Happy coding... :) 

No comments:

Post a Comment

Popular posts

Demonstration of Java NullPointerException

NullPointerException causes and reasons Since Java is object oriented programming language, every thing is considered to be an object. and t...