Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

Creating Custom Icon via ICon Interface

1. Custom Drawn Icon

Java Swing provides Icon interface and any class which implements it is an icon & this way we can create custom icon. The Icon interface will ask you to implement three methods to decide the size of the icon and to perform icon drawing. In this example, we will create rectangle icon and use it with the JLabel component.

2. Implement Icon Interface

Now, we will create a class called MyRectIcon which implements the Icon interface to perform custom icon drawing. We will use this class in the JLabel’s constructor which expects for the Icon instance. The code example is below:

Fig 1. Create Custom Swing Icon by Implementing Icon Interface
Fig 1. Create Custom Swing Icon by Implementing Icon Interface

Explanation

  1. We create a class called MyRectIcon which signs the contract Icon. So, we can say the instances of the MyRectIcon is an icon.
  2. These two integer variables hold the width and height of the icon. We will use this when drawing our custom icon.
  3. In the constructor, we initialize the width and height of the icon.
  4. Icon interface asks us to override the methods getIconWidth & getIconHeight. Our implementation simply returns the width and height set via the constructor.
  5. In the paintIcon override, we set the pen color as red and draw three rectangles by adjusting the width and height at same origin.

Now our custom class is ready. In the next section, we will create a frame window and use our custom icon with the JLabel Controls.

3. Using Custom Icon in JLabel

Java Swing’s JLabel has overloaded constructor and one of the constructors accepts Icon as well as label text. We will use our icon class MyRectIcon with the constructor, Have a look at the below code:

Fig 2. Using the Custome Icon class
Fig 2. Using the Custome Icon class

Explanation

  1. In the main entry method, we create a frame window and set close action as quitting the application. This means, when user clicks the X button, java runtime will close the application.
  2. Next, we create three objects our custom icon class. All instances are in same size with height as 30 pixels and width as 70 pixels. We know that our icon class will use this dimension to draw a rectangle as an icon.
  3. The code snippet here creates three JLabel instances using our custom icon and label text.
  4. The call to setHorizontalTextPosition will help in aligning the JLabel text with the icon. In our code snippet, we tried three alignments Left, Right and Center. For Example, text position right means, the text will appear next to the icon.
  5. Now three JLabels are ready, and we add all these labels to the JFrame via the add method.
  6. Finally, we display the frame window by calling the setVisible method with boolean true argument. A FlowLayout Manager takes care of laying out the JLabel which uses our custom icon.

4. Running the Custom Icon Example

Running the example will produce the following output:

Fig 3. Output of Custom Icon Example
Fig 3. Output of Custom Icon Example

Here, the red boxes show the icon drawn by the swing graphics API. We can also notice how the text alignment is working with our icon. The first one in the example is set with Left alignment (Code snippet 4) and hence the label text is towards the left of the icon. The same way other text & icon alignments are aligning as expected.

5. Code Reference

MyRectIcon.java

TestCustomIcon.java

Categories: Swing

Tags: , , , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.