JLabel – Text and Icons

Set Text (Plain)

jLabel1.setText("The New Text");
Reset:
    jLabel1.setText("");

Set Text (HTML)

jLabel1.setText("<html><font bgcolor=#80ff80 color=#8080ff>The</font>&nbsp;&nbsp;HTML<br><b>text</b></html>");
Reset:
    jLabel1.setText("");

Notes:
    The available HTML tags and attributes, for use in Swing, are listed in javax.swing.text.html.HTML

Set Icon

ImageIcon icon = new ImageIcon(getClass().getResource("/icon.png"));
jLabel1.setIcon(icon);
Reset:
    jLabel1.setIcon(null);

Notes:
    The code above works for .jpg, .gif, or .png files, for .bmp or .tif use:
        try {
            ImageIcon icon = new ImageIcon(ImageIO.read(getClass().getResource("/icon.bmp")));
            jLabel1.setIcon(icon);
        } catch (IOException ex) {
        }

    The resource path starting with a / lets the file be found at:
        "src/main/resources/" for Maven
        "src/" for Ant

    To load a local image file:
        icon = new ImageIcon(new URL("file:c:\\users\\myaccount\\pictures\\icon.jpg"));
        jLabel1.setIcon(icon);

Set Disabled Icon

ImageIcon icon = new ImageIcon(getClass().getResource("/disable_icon.png"));
jLabel1.setDisabledIcon(icon);
Reset:
    jLabel1.setDisabledIcon(null);

Notes:
    The code above works for .jpg, .gif, or .png files, for .bmp or .tif use:
        try {
            ImageIcon icon = new ImageIcon(ImageIO.read(getClass().getResource("/icon.bmp")));
            jLabel1.setDisabledIcon(icon);
        } catch (IOException ex) {
        }

    The resource path starting with a / lets the file be found at:
        "src/main/resources/" for Maven
        "src/" for Ant

    To load a local image file:
        icon = new ImageIcon(new URL("file:c:\\users\\myaccount\\pictures\\icon.jpg"));
        jLabel1.setDisabledIcon(icon);