Monday, December 30, 2024
Google search engine
HomeLanguagesJavaJava Swing – JButton with Rounded Edges

Java Swing – JButton with Rounded Edges

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                   
}


RELATED ARTICLES

Most Popular

Recent Comments