Creating a customized icon for your application

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.

Getting ready

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:

Getting ready

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.

How to do it...

With a Java Desktop Database Application open in NetBeans:

  1. Open DesktopApplicationDBView.java and then navigate to the Source mode.
  2. Inside the constructor, type:
    public DesktopApplicationDBView(SingleFrameApplication app)
    
  3. Find the following line of code with the ResourceMap:
    ResourceMap resourceMap = getResourceMap();
    
  4. And right after that line write:
    JFrame frame = getFrame();
    Image image = resourceMap.getImageIcon("window.icon").getImage();
    frame.setIconImage(image);
    
  5. Then navigate to desktopapplicationdb.resources and open DesktopApplicationDB.properties.
  6. On the last line of the properties file, add:
    window.icon=/images/green-v-icon.png
    
  7. Finally, to add the icon image we need:
    • Create a folder: Under<USER>/NetBeansProjects/DesktopApplicationDB/src folder, create USER>/NetBeansProjects/DesktopApplicationDB/src/images.
    • Rename the application icon to: green-v-icon.png.
    • Then add the image to:<USER>/NetBeansProjects/DesktopApplicationDB/src/images with the name green-v-icon.png.

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:

  1. Navigate to the Services window.
  2. Expand the Databases node.
  3. On jdbc:derby://localhost:1527/sample [app on APP], right-click and select Connect....
  4. Press F6 to run the project and check if the icon is shown, for instance by pressing Alt+Tab.

In the Linux task switcher, it is shown like this:

How to do it...

How it works...

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.

There's more...

And what about a splash screen?

Adding 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:

  1. Right-click on the DesktopApplicationDB node and select Properties.
  2. Navigate to Application and under splash screen, either click on Browse... or enter the path for the file.
  3. Click OK.

And your application will have a working splash screen.

Note

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:

Adding a Splash screen

Application Properties and About box

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:

  1. With DesktopApplicationDB open under the Projects window, right-click on DesktopApplicationDB and select Properties.
  2. Click on Application and fill the form with the desired information.
  3. Click OK.

The Common Application Properties might look like this:

Application Properties and About box

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:

Application Properties and About box
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.6.10