How do I generate UUID / GUID in Java?

UUID / GUID (Universally / Globally Unique Identifier) is frequently use in programming. Some of its usage are for creating random file names, session id in web application, transaction id and for record’s primary keys in database replacing the sequence or auto generated number.

To generate UUID in Java we can use the java.util.UUID class. This class was introduced in JDK 1.5. The UUID.randomUUID() method return a UUID object. To obtain the value of the random string generated we need to call the UUID.toString() method.

We can also get the version and the variant of the UUID using the version() method and variant() method respectively. Let’s see the code snippet below:

package org.kodejava.util;

import java.util.UUID;

public class RandomStringUUID {
    public static void main(String[] args) {
        // Creating a random UUID (Universally unique identifier).
        UUID uuid = UUID.randomUUID();
        String randomUUIDString = uuid.toString();

        System.out.println("Random UUID String = " + randomUUIDString);
        System.out.println("UUID version       = " + uuid.version());
        System.out.println("UUID variant       = " + uuid.variant());
    }
}

The result of our program is:

Random UUID String = 87a20cdd-25da-4dc2-b787-68054ec2c5ca
UUID version       = 4
UUID variant       = 2

How do I get the number of buttons on the mouse?

MouseInfo provides methods for getting information about the mouse, such as mouse pointer location and the number of mouse buttons.

package org.kodejava.awt;

import java.awt.*;

public class MouseNumberOfButtons {
    public static void main(String[] args) {
        // Get the number of buttons on the mouse. On systems without a
        // mouse, returns -1. HeadlessException if
        // GraphicsEnvironment.isHeadless() returns true.
        int numberOfButtons = MouseInfo.getNumberOfButtons();
        System.out.println("Mouse Number Of Buttons = " + numberOfButtons);
    }
}

The output of the code snippet above:

Mouse Number Of Buttons = 5

How do I get the location of mouse pointer?

MouseInfo provides methods for getting information about the mouse, such as mouse pointer location and the number of mouse buttons.

package org.kodejava.awt;

import java.awt.*;

public class MouseInfoDemo {
    public static void main(String[] args) {
        int i = 0;
        while (i < 10) {
            // Get the location of our mouse x and y coordinate using
            // the PointerInfo.getLocation() method which return an 
            // instance of Point.
            Point location = MouseInfo.getPointerInfo().getLocation();
            double x = location.getX();
            double y = location.getY();

            System.out.println("x = " + x);
            System.out.println("y = " + y);

            try {
                Thread.sleep(1000);
                i++;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

How do I get the name of current executed method?

package org.kodejava.lang;

public class GetCurrentMethodName {
    public static void main(String[] args) {
        // Get the current executing method name
        String methodName =
                Thread.currentThread().getStackTrace()[1].getMethodName();
        System.out.println("methodName = " + methodName);

        GetCurrentMethodName obj = new GetCurrentMethodName();
        obj.executeAMethod();
    }

    private void executeAMethod() {
        // Get the current executing method name
        String methodName =
                Thread.currentThread().getStackTrace()[1].getMethodName();
        System.out.println("methodName = " + methodName);
    }
}

This program will print out the following string:

methodName = main
methodName = executeAMethod

How do I get screen’s display mode information?

This example shows graphics devices display mode information such as the display width, height, refresh-rate and bit-depth. This information can be obtained from GraphicsDevice.getDisplayMode() method which return an instance of java.awt.DisplayMode.

We can also get the size of a screen using the following example How do I get my screen size?, but this example can handle only a single screen.

package org.kodejava.awt;

import java.awt.*;

public class GettingScreenDisplayModeInformation {
    public static void main(String[] args) {
        // Get local graphics environment
        GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();

        GraphicsDevice[] devices = env.getScreenDevices();

        int sequence = 1;
        for (GraphicsDevice device : devices) {
            System.out.println("Screen Number [" + (sequence++) + "]");
            System.out.println("Width       : " + device.getDisplayMode().getWidth());
            System.out.println("Height      : " + device.getDisplayMode().getHeight());
            System.out.println("Refresh Rate: " + device.getDisplayMode().getRefreshRate());
            System.out.println("Bit Depth   : " + device.getDisplayMode().getBitDepth());
            System.out.println();
        }
    }
}

An example of the program result:

Screen Number [1]
Width       : 2560
Height      : 1080
Refresh Rate: 60
Bit Depth   : 32

Screen Number [2]
Width       : 2560
Height      : 1080
Refresh Rate: 60
Bit Depth   : 32