java.awt.Toolkit class
java.awt.Toolkit
is a class in Java that provides a way to access the system resources for the AWT (Abstract Window Toolkit) and Swing graphical user interface libraries. It is used to obtain information about the system's screen, color model, font metrics, and other resources.
Here are some of the commonly used methods in the java.awt.Toolkit
class:
getDefaultToolkit()
: This method returns the default toolkit instance for the current platform.getImage(String filename)
: This method loads an image from a file specified by the filename parameter.getScreenSize()
: This method returns the size of the screen in pixels.beep()
: This method emits a beep sound.getSystemClipboard()
: This method returns the system clipboard.getFontMetrics(Font font)
: This method returns the font metrics for the specified font.getDesktopProperty(String propertyName)
: This method returns a system-dependent desktop property.
java.awt.Toolkit
is an abstract class, which means that you cannot create an instance of this class directly. Instead, you can use the getDefaultToolkit()
method to obtain the default toolkit instance for the current platform.
Declaration of this class as follows
public abstract class java.awt.Toolkit {
protected final java.util.Map<java.lang.String, java.lang.Object> desktopProperties;
protected final java.beans.PropertyChangeSupport desktopPropsSupport;
static final boolean $assertionsDisabled;
protected java.awt.Toolkit();
protected void loadSystemColors(int[]) throws java.awt.HeadlessException;
public void setDynamicLayout(boolean) throws java.awt.HeadlessException;
protected boolean isDynamicLayoutSet() throws java.awt.HeadlessException;
public boolean isDynamicLayoutActive() throws java.awt.HeadlessException;
public abstract java.awt.Dimension getScreenSize() throws java.awt.HeadlessException;
public abstract int getScreenResolution() throws java.awt.HeadlessException;
public java.awt.Insets getScreenInsets(java.awt.GraphicsConfiguration) throws java.awt.HeadlessException;
public abstract java.awt.image.ColorModel getColorModel() throws java.awt.HeadlessException;
public abstract java.lang.String[] getFontList();
public abstract java.awt.FontMetrics getFontMetrics(java.awt.Font);
public abstract void sync();
public static synchronized java.awt.Toolkit getDefaultToolkit();
public abstract java.awt.Image getImage(java.lang.String);
public abstract java.awt.Image getImage(java.net.URL);
public abstract java.awt.Image createImage(java.lang.String);
public abstract java.awt.Image createImage(java.net.URL);
public abstract boolean prepareImage(java.awt.Image, int, int, java.awt.image.ImageObserver);
public abstract int checkImage(java.awt.Image, int, int, java.awt.image.ImageObserver);
public abstract java.awt.Image createImage(java.awt.image.ImageProducer);
public java.awt.Image createImage(byte[]);
public abstract java.awt.Image createImage(byte[], int, int);
public abstract java.awt.PrintJob getPrintJob(java.awt.Frame, java.lang.String, java.util.Properties);
public java.awt.PrintJob getPrintJob(java.awt.Frame, java.lang.String, java.awt.JobAttributes, java.awt.PageAttributes);
public abstract void beep();
public abstract java.awt.datatransfer.Clipboard getSystemClipboard() throws java.awt.HeadlessException;
public java.awt.datatransfer.Clipboard getSystemSelection() throws java.awt.HeadlessException;
public int getMenuShortcutKeyMask() throws java.awt.HeadlessException;
public int getMenuShortcutKeyMaskEx() throws java.awt.HeadlessException;
public boolean getLockingKeyState(int) throws java.lang.UnsupportedOperationException;
public void setLockingKeyState(int, boolean) throws java.lang.UnsupportedOperationException;
protected static java.awt.Container getNativeContainer(java.awt.Component);
public java.awt.Cursor createCustomCursor(java.awt.Image, java.awt.Point, java.lang.String) throws java.lang.IndexOutOfBoundsException, java.awt.HeadlessException;
public java.awt.Dimension getBestCursorSize(int, int) throws java.awt.HeadlessException;
public int getMaximumCursorColors() throws java.awt.HeadlessException;
public boolean isFrameStateSupported(int) throws java.awt.HeadlessException;
static void loadLibraries();
public static java.lang.String getProperty(java.lang.String, java.lang.String);
public final java.awt.EventQueue getSystemEventQueue();
protected abstract java.awt.EventQueue getSystemEventQueueImpl();
static java.awt.EventQueue getEventQueue();
public <T extends java.awt.dnd.DragGestureRecognizer> T createDragGestureRecognizer(java.lang.Class<T>, java.awt.dnd.DragSource, java.awt.Component, int, java.awt.dnd.DragGestureListener);
public final synchronized java.lang.Object getDesktopProperty(java.lang.String);
protected final void setDesktopProperty(java.lang.String, java.lang.Object);
protected java.lang.Object lazilyLoadDesktopProperty(java.lang.String);
protected void initializeDesktopProperties();
public void addPropertyChangeListener(java.lang.String, java.beans.PropertyChangeListener);
public void removePropertyChangeListener(java.lang.String, java.beans.PropertyChangeListener);
public java.beans.PropertyChangeListener[] getPropertyChangeListeners();
public java.beans.PropertyChangeListener[] getPropertyChangeListeners(java.lang.String);
public boolean isAlwaysOnTopSupported();
public abstract boolean isModalityTypeSupported(java.awt.Dialog$ModalityType);
public abstract boolean isModalExclusionTypeSupported(java.awt.Dialog$ModalExclusionType);
public void addAWTEventListener(java.awt.event.AWTEventListener, long);
public void removeAWTEventListener(java.awt.event.AWTEventListener);
static boolean enabledOnToolkit(long);
synchronized int countAWTEventListeners(long);
public java.awt.event.AWTEventListener[] getAWTEventListeners();
public java.awt.event.AWTEventListener[] getAWTEventListeners(long);
void notifyAWTEventListeners(java.awt.AWTEvent);
public abstract java.util.Map<java.awt.font.TextAttribute, ?> mapInputMethodHighlight(java.awt.im.InputMethodHighlight) throws java.awt.HeadlessException;
public boolean areExtraMouseButtonsEnabled() throws java.awt.HeadlessException;
static {};
}
Use example
Here's an example of how to use java.awt.Toolkit
to get the screen size of the system:
import java.awt.Dimension;
import java.awt.Toolkit;
public class Main
{
public static void main(String[] args)
{
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int screenWidth = screenSize.width;
int screenHeight = screenSize.height;
System.out.println("Screen width: " + screenWidth);
System.out.println("Screen height: " + screenHeight);
}
}
Screen width: 1366
Screen height: 768
In this example, we first obtain the default Toolkit
instance using the getDefaultToolkit()
method. We then use the getScreenSize()
method to retrieve the screen size as a Dimension
object. Finally, we print the screen size to the console using the width
and height
properties of the Dimension
object.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment