- Package java.awt
- Additional Specification
- Java awt desktop class
- Nested Class Summary
- Method Summary
- Methods inherited from class java.lang.Object
- Method Detail
- getDesktop
- isDesktopSupported
- isSupported
- open
- edit
- browse
- Java awt desktop class
- Nested Class Summary
- Method Summary
- Methods declared in class java.lang.Object
- Method Detail
- getDesktop
- isDesktopSupported
- isSupported
- open
- edit
- browse
- addAppEventListener
- removeAppEventListener
- setAboutHandler
- setPreferencesHandler
- setOpenFileHandler
- setPrintFileHandler
- setOpenURIHandler
- setQuitHandler
- setQuitStrategy
- enableSuddenTermination
- disableSuddenTermination
- requestForeground
- openHelpViewer
- Java AWT | Desktop Class
- Java AWT | Desktop Class
Package java.awt
Contains all of the classes for creating user interfaces and for painting graphics and images. A user interface object such as a button or a scrollbar is called, in AWT terminology, a component. The Component class is the root of all AWT components. See Component for a detailed description of properties that all AWT components share.
Some components fire events when a user interacts with the components. The AWTEvent class and its subclasses are used to represent the events that AWT components can fire. See AWTEvent for a description of the AWT event model.
A container is a component that can contain components and other containers. A container can also have a layout manager that controls the visual placement of components in the container. The AWT package contains several layout manager classes and an interface for building your own layout manager. See Container and LayoutManager for more information.
Each Component object is limited in its maximum size and its location because the values are stored as an integer. Also, a platform may further restrict maximum size and location coordinates. The exact maximum values are dependent on the platform. There is no way to change these maximum values, either in Java code or in native code. These limitations also impose restrictions on component layout. If the bounds of a Component object exceed a platform limit, there is no way to properly arrange them within a Container object. The object’s bounds are defined by any object’s coordinate in combination with its size on a respective axis.
Additional Specification
Java awt desktop class
This class provides methods corresponding to these operations. The methods look for the associated application registered on the current platform, and launch it to handle a URI or file. If there is no associated application or the associated application fails to be launched, an exception is thrown.
An application is registered to a URI or file type; for example, the «sxi» file extension is typically registered to StarOffice. The mechanism of registering, accessing, and launching the associated application is platform-dependent.
Each operation is an action type represented by the Desktop.Action class.
Note: when some action is invoked and the associated application is executed, it will be executed on the same system as the one on which the Java application was launched.
Nested Class Summary
Method Summary
Launches the mail composing window of the user default mail client, filling the message fields specified by a mailto: URI.
Prints a file with the native desktop printing facility, using the associated application’s print command.
Methods inherited from class java.lang.Object
Method Detail
getDesktop
Returns the Desktop instance of the current browser context. On some platforms the Desktop API may not be supported; use the isDesktopSupported() method to determine if the current desktop is supported.
isDesktopSupported
public static boolean isDesktopSupported()
Tests whether this class is supported on the current platform. If it’s supported, use getDesktop() to retrieve an instance.
isSupported
Tests whether an action is supported on the current platform. Even when the platform supports an action, a file or URI may not have a registered application for the action. For example, most of the platforms support the Desktop.Action.OPEN action. But for a specific file, there may not be an application registered to open it. In this case, isSupported(java.awt.Desktop.Action) may return true , but the corresponding action method will throw an IOException .
open
Launches the associated application to open the file. If the specified file is a directory, the file manager of the current platform is launched to open it.
edit
Prints a file with the native desktop printing facility, using the associated application’s print command.
browse
Launches the default browser to display a URI . If the default browser is not able to handle the specified URI , the application registered for handling URIs of the specified type is invoked. The application is determined from the protocol and path of the URI , as defined by the URI class. If the calling thread does not have the necessary permissions, and this is invoked from within an applet, AppletContext.showDocument() is used. Similarly, if the calling does not have the necessary permissions, and this is invoked from within a Java Web Started application, BasicService.showDocument() is used.
public void mail(URI mailtoURI) throws IOException
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2023, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.
Java awt desktop class
This class provides methods corresponding to these operations. The methods look for the associated application registered on the current platform, and launch it to handle a URI or file. If there is no associated application or the associated application fails to be launched, an exception is thrown. Please see Desktop.Action for the full list of supported operations and capabilities.
An application is registered to a URI or file type. The mechanism of registering, accessing, and launching the associated application is platform-dependent.
Each operation is an action type represented by the Desktop.Action class.
Note: when some action is invoked and the associated application is executed, it will be executed on the same system as the one on which the Java application was launched.
Nested Class Summary
Method Summary
Launches the mail composing window of the user default mail client, filling the message fields specified by a mailto: URI.
Prints a file with the native desktop printing facility, using the associated application’s print command.
Methods declared in class java.lang.Object
Method Detail
getDesktop
Returns the Desktop instance of the current desktop context. On some platforms the Desktop API may not be supported; use the isDesktopSupported() method to determine if the current desktop is supported.
isDesktopSupported
public static boolean isDesktopSupported()
Tests whether this class is supported on the current platform. If it’s supported, use getDesktop() to retrieve an instance.
isSupported
Tests whether an action is supported on the current platform. Even when the platform supports an action, a file or URI may not have a registered application for the action. For example, most of the platforms support the Desktop.Action.OPEN action. But for a specific file, there may not be an application registered to open it. In this case, isSupported(Action) may return true , but the corresponding action method will throw an IOException .
open
Launches the associated application to open the file. If the specified file is a directory, the file manager of the current platform is launched to open it.
edit
Prints a file with the native desktop printing facility, using the associated application’s print command.
browse
Launches the default browser to display a URI . If the default browser is not able to handle the specified URI , the application registered for handling URIs of the specified type is invoked. The application is determined from the protocol and path of the URI , as defined by the URI class.
public void mail(URI mailtoURI) throws IOException
Launches the mail composing window of the user default mail client, filling the message fields specified by a mailto: URI. A mailto: URI can specify message fields including «to», «cc», «subject», «body», etc. See The mailto URL scheme (RFC 2368) for the mailto: URI specification details.
addAppEventListener
Adds sub-types of SystemEventListener to listen for notifications from the native system. Has no effect if SystemEventListener’s sub-type is unsupported on the current platform.
removeAppEventListener
Removes sub-types of SystemEventListener to listen for notifications from the native system. Has no effect if SystemEventListener’s sub-type is unsupported on the current platform.
setAboutHandler
Installs a handler to show a custom About window for your application. Setting the AboutHandler to null reverts it to the default behavior.
setPreferencesHandler
Installs a handler to show a custom Preferences window for your application. Setting the PreferencesHandler to null reverts it to the default behavior
setOpenFileHandler
setPrintFileHandler
setOpenURIHandler
Installs the handler which is notified when the application is asked to open a URL. Setting the handler to null causes all OpenURIHandler.openURI(OpenURIEvent) requests to be enqueued until another handler is set.
setQuitHandler
Installs the handler which determines if the application should quit. The handler is passed a one-shot QuitResponse which can cancel or proceed with the quit. Setting the handler to null causes all quit requests to directly perform the default QuitStrategy .
setQuitStrategy
enableSuddenTermination
public void enableSuddenTermination()
Enables this application to be suddenly terminated. Call this method to indicate your application’s state is saved, and requires no notification to be terminated. Letting your application remain terminatable improves the user experience by avoiding re-paging in your application when it’s asked to quit. Note: enabling sudden termination will allow your application to be quit without notifying your QuitHandler, or running any shutdown hooks. E.g. user-initiated Cmd-Q, logout, restart, or shutdown requests will effectively «kill -KILL» your application.
disableSuddenTermination
public void disableSuddenTermination()
Prevents this application from being suddenly terminated. Call this method to indicate that your application has unsaved state, and may not be terminated without notification.
requestForeground
public void requestForeground(boolean allWindows)
openHelpViewer
public void openHelpViewer()
Java AWT | Desktop Class
Important Points About Desktop Class : It can open a default web browser showing a specific URI It can launch default mail client with optional mailto URI It can launch a registered application to open, edit or print a specific file. This class is used to launch associated applications registered on the native desktop to handle a URI or a file.
Java AWT | Desktop Class
The Desktop class is a part of Java AWT package. This class is used to launch associated applications registered on the native desktop to handle a URI or a file.
Important Points About Desktop Class :
- It can open a default web browser showing a specific URI
- It can launch default mail client with optional mailto URI
- It can launch a registered application to open, edit or print a specific file.
Different Methods in Desktop Class
Method | Explanation |
---|---|
browse(URI u) | Launches the default browser to display a specific URI. |
edit(File f) | Launches the associated editor application and opens a file. |
getDesktop() | Returns the Desktop instance of the current browser context. |
isDesktopSupported() | returns whether this class is supported on the current platform. |
isSupported(Desktop.Action action) | returns whether an action is supported on the current platform. |
mail() | Launches the mail composing window of the user default mail client. |
mail(URI mailtoURI) | Launches the mail composing window of the user default mail client, filling the message fields specified by a mailto: URI. |
open(File f) | Launches the associated application to open the file |
print(File f) | Prints a file with the native desktop printing facility, using the associated application’s print command. |
Below programs illustrate the Desktop class in Java AWT:
- Program to Launch the browser and open a specific URI