Create Light & Dark Theme Funcationality In Java Swing Application.

In this article we will see how to create dark & light theme funcationality in java swing application.

For that we will use some java swing components , Listener Interface , Event , Methods Etc..

  1.    setText() : Defines the single line of text this component will display. If the value of text is null or empty string, nothing is displayed.
  2.     setFont() : sets the font and style when displaying text strings.
  3.     setBounds(): set the position and size of any component.
  4.     setBorder(): Sets the border of this component.
  5.     ImageIcon class: An implementation of the Icon interface that paints Icons from Images.
  6.     getScaledInstance(width,height,hints) method: Creates a scaled version of this image. A new Image object is returned which will render the image at the specified width and height by default. The new Image object may be loaded asynchronously even if the original source image has already been loaded completely. Return : a scaled version of the image.

    Parameters :

    • width : the width to which to scale the image
    • height : the height to which to scale the image
    • hints : flags to indicate the type of algorithm to use for image resampling.
  7.     setIcon() method : Sets the button's default icon. This icon is also used as the "pressed" and "disabled" icon if there is no explicitly set pressed icon.
  8.     getImage(): Returns this icon's Image.
  9.     Image class : Image class is the superclass that represents graphical images as rectangular arrays of pixels.
  10.     add() : add a component on another component or JFrame.
  11.     setCursor() : sets a mouse cursor shape like hand pointer,Text Pointer,Help Pointer Etc. The hand cursor is mostly used.
  12.     setLocation(): Sets the location of the point to the specified location.
  13.     setLayout() : sets the layout manager for the component.
  14.     setResizable(): Indicates whether this frame is resizable by the user. By default, all frames are initially resizable. return : true if the user can resize this frame; false otherwise.
  15.      setVisible() : sets the visibility of the component or JFrame. It is by default false.
  16.      setDefaultCloseOperation() : setDefaultCloseOperation() - Exit the application. The default behavior is to simply hide the JFrame when the user closes the window. When we use setDefaultCloseOperation() it that time we will exit the JFrame form.
  17.     addActionListener Iterface and ActionEvent method : Generated when button is pressed , menu-item is selected , list-item is double clicked.

Example :

package com.company;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class DarkLightTheme extends JFrame{
    //JFrame components declarations start here  :
    JLabel text = new JLabel();
    JButton lightMode = new JButton();
    JButton darkMode= new JButton();

    //Create Constructor :
    DarkLightTheme(){
        ///Add light Mode Button in JFrame
        lightMode.setCursor(new Cursor(Cursor.HAND_CURSOR));
        lightMode.setBorder(null);
        lightMode.setVisible(false);
        lightMode.setBounds(600,20,70,45);
        add(lightMode);
        ImageIcon dark = new ImageIcon("D:\\lightMode.jpg");
        Image img = dark.getImage();
        Image imageScale = img.getScaledInstance(lightMode.getWidth(),lightMode.getHeight(),Image.SCALE_SMOOTH);
        ImageIcon scaleImg = new ImageIcon(imageScale);
        lightMode.setIcon(scaleImg);

        ///Add dark Mode Button in JFrame
        darkMode.setCursor(new Cursor(Cursor.HAND_CURSOR));
        darkMode.setBorder(null);
        darkMode.setBounds(600,20,70,45);
        add(darkMode);
        ImageIcon light = new ImageIcon("D:\\darkMode.jpg");
        Image img1 = light.getImage();
        Image imageScale1 = img1.getScaledInstance(darkMode.getWidth(),darkMode.getHeight(),Image.SCALE_SMOOTH);
        ImageIcon scaleImg1 = new ImageIcon(imageScale1);
        darkMode.setIcon(scaleImg1);

        //Light mode button addActionListener
        lightMode.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                lightMode.setVisible(false);
                darkMode.setVisible(true);
                getContentPane().setBackground(Color.WHITE);
                text.setForeground(Color.BLACK);
                text.setText("Light mode enabled");
            }
        });
        //Dark mode button addActionListener
        darkMode.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                darkMode.setVisible(false);
                lightMode.setVisible(true);
                getContentPane().setBackground(Color.BLACK);
                text.setForeground(Color.WHITE);
                text.setText("Dark mode enabled");
            }
        });

        //add text in JFrame
        text.setFont(new Font("Corbel", 1, 18));
        text.setText("Create Dark & Light Theme Funcationality in Java Swing Application");
        add(text);
        text.setBounds(20, 182, 550, 23);

        //Define Some JFrame Properties :
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Dark & Light Theme");
        setLocation(450, 200);
        setResizable(false);
        setSize(700, 500);
        setLayout(null);
        getContentPane().setBackground(Color.WHITE);
        setVisible(true);
    }
    //Main method start here
    public static void main(String[] args) {
        new DarkLightTheme();
    }
}

Output:

Create Light & Dark Theme Funcationality In Java Swing Application.