The Wayback Machine - https://web.archive.org/web/20240816082536/https://www.geeksforgeeks.org/java-swing-jbutton-with-rounded-edges/
Open In App

Java Swing – JButton with Rounded Edges

Last Updated : 30 Dec, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The class JButton is an implementation of a push button and is a part of the Java Swing package. This component has a label and generates an event when pressed. It can also have an image. Swing is a part of JFC (Java Foundation Classes). Building Graphical User Interface in Java requires the use of Swings. Swing Framework contains a large set of components that allow a high level of customization and provide rich functionalities and is used to create window-based applications. However unlike the preview of JButtons, while running in NetBeans, a standalone Java Swing Application shows the buttons with a sharp-edged Metal preview design by default. This can affect the user experience when the application is deployed as a standalone app and the buttons are intended to have curved or rounded edge designs. An example of such a preview is shown below:

Normal Preview (Without LookAndFeel enhancements)

Normal Preview (Without LookAndFeel enhancements)

To come to the rescue, Swing provides platform-specific Look and Feel and also an option for pluggable Look and Feel, allowing applications to have Look and Feel independent of the underlying platform and to make the UI better and thus giving developers the opportunity to enhance the look of a Java Swing Application. “Look” refers to the appearance of GUI widgets and “feel” refers to the way the widgets behave. To read more about Java Swing “Look and Feel” you can read this article: Java Swing | Look and Feel

However, we will be focussing on only one of the casual themes i.e. JTattoo which can be used to get the JButtons with rounded edges as well as better design. JTattoo is skinnable and consists of several different Look And Feel for Java Applications. A User can customize the look by choosing among different available color schemes. To start with, one needs to Download the JTattoo jar file and link it to your project using by right-clicking the Libraries folder and selecting Add JAR/Folder as shown below.

JTattoo Download Page

JTattoo Download Page

Libraries

Libraries

Next, we proceed to the source code of the JFrame and import the following packages:

import java.awt.Color;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.UnsupportedLookAndFeelException;

Similarly, the main function must be modified as shown below :

public static void main(String args[]) throws UnsupportedLookAndFeelException, ClassNotFoundException {
        try 
        {
            // Here you can select the selected theme class name in JTatt
            UIManager.setLookAndFeel("com.jtattoo.plaf.aluminium.AluminiumLookAndFeel");
        } 
        catch (InstantiationException ex) 
        {
            Logger.getLogger(RoundButton1.class.getName()).log(Level.SEVERE, null, ex);
        } 
        catch (IllegalAccessException ex) 
        {
            Logger.getLogger(RoundButton1.class.getName()).log(Level.SEVERE, null, ex);
        }
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new RoundButton1().setVisible(true);
            }

        });
    }

After applying the above-mentioned changes the JButtons will have an Aluminium JTattoo design with curved edges as shown below :

 

The meaning and applications of the different packages and statements used above are discussed in brief below:

  • java.awt.Color: The Color class is a part of the Java Abstract Window Toolkit(AWT) package. To know more about it read: Java AWT | Color Class
  • java.util.logging: Provides the classes and interfaces of the Java platform’s core logging facilities.
  • A Logger object is used to log messages for a specific system or application component.
  • The Level class defines a set of standard logging levels that can be used to control logging output
  • javax.swing.SwingUtilities: A collection of utility methods for Swing.
  • javax.swing.UIManager: UIManager manages the current look and feel, the set of available look and feels, PropertyChangeListeners that are notified when the look and feel changes, look and feel defaults, and convenience methods for obtaining various default values.
  • javax.swing.UnsupportedLookAndFeelException: An exception that indicates the requested look & feels management classes are not present on the user’s system
  • UnsupportedLookAndFeelException: An exception that indicates the requested look & feels management classes are not present on the user’s system.
  • ClassNotFoundException: A checked exception in Java that occurs when the JVM tries to load a particular class but does not find it in the classpath.

Apart from the “AluminiumLookAndFeel” style, we can use other styles too for our application as per the choice of themes. For changing the theme we have to make changes in the given line in the main function :

UIManager.setLookAndFeel("com.jtattoo.plaf.[Your Chosen Theme ID]");

The above statement should be enclosed within a try-catch block to handle Instantiation Exception. Some popularly used JTattoo themes that support JButtons with rounded edges are mentioned below:

1. MintLookAndFeel — “com.jtattoo.plaf.mint.MintLookAndFeel”

Mint Look And Feel

Mint Look And Feel

2. McWinLookAndFeel — “com.jtattoo.plaf.mcwin.McWinLookAndFeel”

 McWin Look And Feel

 McWin Look And Feel

3. AluminiumLookAndFeel — “com.jtattoo.plaf.aluminium.AluminiumLookAndFeel” 

Aluminium Look And Feel

Aluminum Look And Feel

The source code of the JFrame for implementing the above-mentioned theme is shown below:

Java




package gfg_round_button;
  
import java.awt.Color;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.UnsupportedLookAndFeelException;
  
public class RoundButton1 extends javax.swing.JFrame {
  
    // Creates new form RoundButton
    public RoundButton1() {
        initComponents();
    }
  
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() 
    {
    }
    
    public static void main(String args[]) throws UnsupportedLookAndFeelException, ClassNotFoundException {
        // try-catch block to handle InstantiationException
        try 
        {
            // Here you can select the selected theme class name in JTatt
            UIManager.setLookAndFeel("com.jtattoo.plaf.aluminium.AluminiumLookAndFeel");
        } 
          catch (InstantiationException ex) 
        {
            Logger.getLogger(RoundButton1.class.getName()).log(Level.SEVERE, null, ex);
        } 
          catch (IllegalAccessException ex) 
        {
            Logger.getLogger(RoundButton1.class.getName()).log(Level.SEVERE, null, ex);
        }
  
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new RoundButton1().setVisible(true);
            }
  
        });
        //</editor-fold>
  
    }
  
    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JButton testbtn2;
    private javax.swing.JButton testbtn3;
    // End of variables declaration                   
}




Similar Reads

Java AWT vs Java Swing vs Java FX
Java's UI frameworks include Java AWT, Java Swing, and JavaFX. This plays a very important role in creating the user experience of Java applications. These frameworks provide a range of tools and components for creating graphical user interfaces (GUIs) that are not only functional but also visually appealing. As a Java developer, selecting the righ
11 min read
Java Swing | Translucent and shaped Window in Java
Java provides different functions by which we can control the translucency of the window or the frame. To control the opacity of the frame must not be decorated. Opacity of a frame is the measure of the translucency of the frame or component. In Java, we can create shaped windows by two ways first by using the AWTUtilities which is a part of com.su
5 min read
Java Swing | ScrollPaneLayout Class
The layout manager used by JScrollPane. JScrollPaneLayout is based on nine components: a viewport, two scrollbars, a row header, a column header, and four "corner" components. Constructor of the class: ScrollPaneLayout(): It is used to Construct a new ScrollPaneLayout. Commonly Used Methods: removeLayoutComponent(Component comp): Removes the specif
4 min read
Java Swing - JPanel With Examples
JPanel, a part of the Java Swing package, is a container that can store a group of components. The main task of JPanel is to organize components, various layouts can be set in JPanel which provide better organization of components, however, it does not have a title bar. Constructors of JPanel JPanel(): creates a new panel with a flow layoutJPanel(L
4 min read
Java Swing | Internal Frame with examples
JInternalFrame is a part of Java Swing . JInternalFrame is a container that provides many features of a frame which includes displaying title, opening, closing, resizing, support for menu bar, etc. Constructors for JInternalFrame JInternalFrame() : creates a new non- closable, non- resizable, non- iconifiable, non- maximizable JInternalFrame with n
8 min read
Java Swing | JFileChooser
JFileChooser is a part of java Swing package. The java Swing package is part of JavaTM Foundation Classes(JFC) . JFC contains many features that help in building graphical user interface in java . Java Swing provides components such as buttons, panels, dialogs, etc . JFileChooser is a easy and an effective way to prompt the user to choose a file or
11 min read
Java Swing | JTextField
JTextField is a part of javax.swing package. The class JTextField is a component that allows editing of a single line of text. JTextField inherits the JTextComponent class and uses the interface SwingConstants.The constructor of the class are : JTextField() : constructor that creates a new TextFieldJTextField(int columns) : constructor that creates
5 min read
Java Swing | JProgressBar
JProgressBar is a part of Java Swing package. JProgressBar visually displays the progress of some specified task. JProgressBar shows the percentage of completion of specified task.The progress bar fills up as the task reaches it completion. In addition to show the percentage of completion of task, it can also display some text .Constructors of JPro
4 min read
JLabel | Java Swing
JLabel is a class of java Swing . JLabel is used to display a short string or an image icon. JLabel can display text, image or both . JLabel is only a display of text or image and it cannot get focus . JLabel is inactive to input events such a mouse focus or keyboard focus. By default labels are vertically centered but the user can change the align
4 min read
Java Swing | JToolBar
JToolBar is a part of Java Swing package. JToolBar is an implementation of toolbar. The JToolBar is a group of commonly used components such as buttons or drop down menu. JToolBar can be dragged to different locations by the user Constructors of the class are: JToolBar() : creates a new toolbar with horizontal orientationJToolBar(int o) : creates a
6 min read
Practice Tags :
three90RightbarBannerImg