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:
Explanation
- We create a class called
MyRectIcon
which signs the contractIcon
. So, we can say the instances of the MyRectIcon is an icon. - These two integer variables hold the width and height of the icon. We will use this when drawing our custom icon.
- In the constructor, we initialize the width and height of the icon.
- Icon interface asks us to override the methods
getIconWidth
&getIconHeight
. Our implementation simply returns the width and height set via the constructor. - 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:

Explanation
- 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.
- 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.
- The code snippet here creates three
JLabel
instances using our custom icon and label text. - 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. - Now three JLabels are ready, and we add all these labels to the JFrame via the
add
method. - 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:

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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
package SwingExamples; import java.awt.Color; import java.awt.Component; import java.awt.Graphics; import javax.swing.Icon; public class MyRectIcon implements Icon { //Sample 01: Member to store width and height private int IconWidth; private int IconHeight; //Sample 02: Constructor public MyRectIcon(int w, int h) { IconWidth = w; IconHeight = h; } //Sample 03: Override Methods to return height and width @Override public int getIconWidth() { return IconWidth; } @Override public int getIconHeight() { return IconHeight; } //Sample 04: Perform Drawing @Override public void paintIcon(Component c, Graphics g, int x, int y) { g.setColor(Color.red); g.drawRect(x, y, IconWidth, IconHeight); g.drawRect(x, y, IconWidth-2, IconHeight-2); g.drawRect(x, y, IconWidth-4, IconHeight-4); } } |
TestCustomIcon.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
package SwingExamples; import java.awt.Container; import java.awt.FlowLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.SwingConstants; public class TestCustomIcon { public static void main(String[] args) { //Sample 01: Create Frame JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Sample 02: Create Custom Icons MyRectIcon icon1 = new MyRectIcon(70, 30); MyRectIcon icon2 = new MyRectIcon(70, 30); MyRectIcon icon3 = new MyRectIcon(70, 30); //Sample 03: Create Label JLabel lbl1 = new JLabel("Label 1", icon1, SwingConstants.CENTER); JLabel lbl2 = new JLabel("Label 2", icon2, SwingConstants.CENTER); JLabel lbl3 = new JLabel("Label 3", icon3, SwingConstants.CENTER); lbl1.setHorizontalTextPosition(SwingConstants.LEFT); lbl2.setHorizontalTextPosition(SwingConstants.RIGHT); lbl3.setHorizontalTextPosition(SwingConstants.CENTER); //Sample 04: Add Label to Container Container Pane = frame.getContentPane(); Pane.add(lbl1); Pane.add(lbl2); Pane.add(lbl3); //Sample 05: Show the Frame Window frame.setBounds(100, 100, 600, 300); frame.setLayout(new FlowLayout()); frame.setVisible(true); } } |
Categories: Swing
Tags: getIconHeight, getIconWidth, Icon, JLabel, paintIcon