How to add Icon in JLabel?

Many times we need to add image icon to the JLabel and no wonders JLabel has provision for it. JLabel has a constructor which takes icon as a argument.
But how we get the image icon. ImageIcon helps us to create icon using image as shown in below example.

    JFrame frame = new JFrame("JLabel with icon example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JLabel labelIcon = new JLabel("JLabel with image icon ");
    ImageIcon icon = new ImageIcon("C://example/icontest.jpg");
    JLabel iconLabel = new JLabel(icon);
    FlowLayout flowLayout = new FlowLayout();
    frame.getContentPane().setLayout(flowLayout);

    frame.getContentPane().add(labelIcon);
    frame.getContentPane().add(iconLabel);
  
   frame.getContentPane().setComponentOrientation(ComponentOrientation.UNKNOWN);

    frame.setSize(300,400);
    frame.pack();
    frame.setVisible(true);



If we want to have some text with icon in JLabel, then we need to use the constructor
as JLabel(String text, Icon icon, int horizontalAlignment) in which you can specify the icon with text and also the horizontal alignment. Lets see it in example below :

    JFrame frame = new JFrame("JLabel with icon example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JLabel labelIcon = new JLabel("JLabel with image icon and text : ");
    ImageIcon icon = new ImageIcon("C://example/icontest.jpg");
    JLabel iconLabel = new JLabel("I with text", icon, JLabel.CENTER);
    FlowLayout flowLayout = new FlowLayout();
    frame.getContentPane().setLayout(flowLayout);

    frame.getContentPane().add(labelIcon);
    frame.getContentPane().add(iconLabel);

    frame.getContentPane().setComponentOrientation(ComponentOrientation.UNKNOWN);

    frame.setSize(300,400);
    frame.pack();
    frame.setVisible(true);


We can also specify the position of text relative to an image icon. Text is aligned on trailing edge of the image by default in which both leading and trailing edge are determined by its ComponentOrientation property.


You can also specify the position of the text relative to the image. By default, text is on the trailing edge of the image, with the text and image vertically aligned.

We can use setIconTextGap method to specify how many pixels should be in between text and image. 4 pixel is by default.

setHorizontalTextPosition and setVerticalTextPosition methods are used to set vertical and horizontal position of the label's text, relative to its image , in which argument is passed as textPosition an int constant value. For vertical text positioning the value of the constant we can pass as SwingConstants TOP, CENTER or BOTTOM (CENTER is default position ) For horizontal constants can be SwingConstants LEFT, CENTER, RIGHT, LEADING, or TRAILING (Default is trailing).
Share on Google Plus

About Pranav

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

2 comments:

  1. Do you know how to recover permanently deleted files.MiniTool Solution Ltd give me more
    help and very useful.You can have a try.

    ReplyDelete
  2. I like this blog! In return, I want to share some cheap ray ban sunglasses with you.

    ReplyDelete