One of the ways to customize your Java Desktop application and give it a more professional look is by adding an icon to it.
The icon will be used when the application is running, displayed in places like the task bar or when the user switches the application and the icons are displayed on the tab-switch.
For the purposes of this recipe, we will use an image in PNG format.
The image we will use for this recipe is the following:
It is necessary to have the sources of a Java Desktop recipe in your NetBeans project folder. Many of the files created in this recipe are a result of the previous recipes in this chapter being followed. This is because we wish to give continuity to the recipes.
With a Java Desktop Database Application open in NetBeans:
DesktopApplicationDBView.java
and then navigate to the Source mode.public DesktopApplicationDBView(SingleFrameApplication app)
ResourceMap resourceMap = getResourceMap();
JFrame frame = getFrame(); Image image = resourceMap.getImageIcon("window.icon").getImage(); frame.setIconImage(image);
desktopapplicationdb.resources
and open DesktopApplicationDB.properties.window.icon=/images/green-v-icon.png
<USER>/NetBeansProjects/DesktopApplicationDB/src
folder, create USER>/NetBeansProjects/DesktopApplicationDB/src/images
.So the entire path needs to be:
<USER>/NetBeansProjects/DesktopApplicationDB/src/images/green-v-icon.png
Since we are using a database application for this example, the database, Java DB in our case, must be up and running. Here is how to connect to the database if unsure:
We are loading the icon into the application from the constructor. For this, we are using the ResourceMap. It reads the parameters set inside DesktopApplicationDB.properties.
The ResourceMap is a holder that contains all the resources from our resource bundle, the DesktopAapplicationDB.properties
. When instantiated, the ResourceMap returns values according to the keys being pressed.
This is a good approach since we are not hard-coding the path of the icon directly in the code and means the icon, or other properties as well, can be changed with minimal work.
And what about a splash screen?
A splash screen is the screen that appears for a moment when the program is loading to tell the user that its execution has started.
For this example, we will use the same project we have used in this recipe, the DesktopApplicationDB.
For this to work, a splash screen image and the Java DB Sample App database must be up and running. If unsure how to do this, take a look at the How to do it… section of this recipe.
With the DesktopApplicationDB open in NetBeans projects window:
And your application will have a working splash screen.
Note that splash screens do not normally display when a project is being executed from within the IDE. It is necessary to navigate, with the command line, to the dist directory under your application's project path, in this recipe's case: NetBeansProjects/DatabaseApplicationDB/dist
, and execute the following command:
java -jar DesktopApplicationDB.jar
Application startup and splash screen shown from a Linux shell:
The About box of an application gives the user useful information on where to find the application's main homepage, vendor's name, and other tidbits.
If you wish to quickly edit this without messing with the Swing components in the Design editor, here is how to do it:
The Common Application Properties might look like this:
To view the changes, press F6 or click on the Run Main Project button.
When the Application is shown, click on Help and then About....
This is what the output looks like when the above information is entered:
3.147.6.10