• ImageIcon and JLabel

    ImageIcon and JLabel

    ImageIcon Class

    In Swing ImageIcon and JLabel classes, the icons are encapsulated by the ImageIcon class, which paints an icon from an image. Two of lists constructors are show here:

         ImageIcon(String filename)

         ImageIcon(URL url)

    The first form used the image in the file named “filename”. The second form used the image in the resource identified by “url”.

    The ImageIcon class implements the Icon interface that declares the methods show here:

         Method Description
         int getIconHeight() Returns the height of the icon in pixels.
         Int getIconWidth() Returns the width of the icon in pixels.
         Void painIcon() Paints the icon at position x, y on thegraphics context g. Additional

    information about the paint operation

    can be provided in comp.

    JLabel Class

    Swing labels are instances of the JLabel class, which extends JComponent. It can display text and/or an icon. Some of its constructors are show here:

         JLabel(Icon i)

         Label(String s)

         JLabel(String s, Icon I, int align)

    Here, s and i are the text and icon used for the label. The align argument is either LEFT, RIGHT, CENTER, LEADING, or TRAILING. These constants are defined in the SwingConstants interface, along with several others used by the Swing classes. The icon and text associated with the label can be read and written by the following methods:

         Icon getIcon()

         String getText()

         void setIcon(Icon i)

         void setText(String s)

    ImageIcon and JLabel Swing Example

    ImageIcon and JLabel Swing Example



    * <APPLET CODE = “Swing01.CLASS” HEIGHT = 500 WIDTH = 500></APPLET>


    import java.awt.*;

    import javax.swing.*;

    public class Swing01 extends JApplet


    public void init()


    //Get Content Pane

    Container contentPane = getContentPane();

    ImageIcon ii = new ImageIcon(“c:\\PracticeJava\\javalogo.gif”);

    JLabel jl = new JLabel(“Java Logo”, ii, JLabel.CENTER);




    Appreciate my work :Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInPin on PinterestShare on RedditShare on StumbleUponShare on TumblrDigg thisShare on YummlyShare on VKFlattr the authorBuffer this page

Leave a Reply

Your email address will not be published. Required fields are marked *