Open In App

Large Icons using javax.swing in Java

Improve
Improve
Like Article
Like
Save
Share
Report

Large icons generally refer to the set of icons provided by the look and feel of the user interface. The “large icons” provided by the look and feel are typically larger than the standard size of icons used in the interface and are intended to be used in places where a larger, more visually prominent icon is needed, such as in dialogs or other areas where the user’s attention needs to be drawn to a specific UI element. The exact size and set of large icons available may vary depending on the specific look and feel used in the application and can be accessed through the UI Manager class in Java. In this article, we will learn how to create an API for accessing Large Icons using the javax.swing package in java.

What is javax.swing package?

javax.swing is a package in the Java standard library that provides a set of GUI (Graphical User Interface) components and related classes for creating desktop applications. The javax.swing package is an extension of the java.awt package, and provides more advanced and flexible components than the original AWT (Abstract Window Toolkit) components. Some of the commonly used Swing components include buttons, text fields, labels, checkboxes, radio buttons, sliders, scroll panes, tables, trees, menus, and dialog boxes. These components can be combined to create complex user interfaces with a modern look.

Step by Step Implementation

Import the necessary classes

Java




import javax.swing.*;
import java.awt.*;


Create a new Java class named LargeIconAPI.

Java




public class LargeIconAPI {
    // API method to retrieve a large icon
    public static Icon getLargeIcon(String iconKey)
    {
        // Retrieve a large icon from
        // the current look and feel
        Icon icon = UIManager.getIcon(iconKey);
  
        return icon;
    }
}


Define a public static method named getLargeIcon that takes a String argument representing the key of the large icon to retrieve from the look and feel.

Java




public static Icon getLargeIcon(String iconKey)
{
  
    // Retrieve a large icon from
    // the current look and feel
    Icon icon = UIManager.getIcon(iconKey);
  
    return icon;
}


Use the UIManager.getIcon() method to retrieve the large icon from the look and feel using the iconKey argument, and return it as an Icon object.

Java




// Retrieve a large icon from the current look and feel
Icon icon = UIManager.getIcon(iconKey);
  
return icon;


Use the API method getLargeIcon in your Java application to retrieve a large icon and use it in a component or UI element. Here’s an example that retrieves a large icon and displays it in a JLabel.

Java




public class LargeIconExample {
    public static void main(String[] args)
    {
        // Retrieve a large icon from the API
        Icon icon = LargeIconAPI.getLargeIcon(
            "FileView.directoryIcon");
  
        // Create a label with the large icon
        JLabel label = new JLabel(icon);
  
        // Display the label in a JFrame
        JFrame frame = new JFrame("Large Icon Example");
        frame.getContentPane().add(label);
        frame.setDefaultCloseOperation(
            JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}


Here’s the complete code

Java




import javax.swing.*;
  
public class LargeIconAPI {
  
    // API method to retrieve a large icon
    public static Icon getLargeIcon(String iconKey) {
         
          // Retrieve a large icon from
          // the current look and feel
        Icon icon = UIManager.getIcon(iconKey);
  
        return icon;
    }
  
}
  
public class LargeIconExample {
  
    public static void main(String[] args) {
        // Retrieve a large icon from the API
        Icon icon = LargeIconAPI.getLargeIcon("FileView.directoryIcon");
  
        // Create a label with the large icon
        JLabel label = new JLabel(icon);
  
        // Display the label in a JFrame
        JFrame frame = new JFrame("Large Icon Example");
        frame.getContentPane().add(label);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
  
}


Output:

 



Last Updated : 08 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads