JButton – Text and Icons

Set Text

jButton1.setText("The new text");
Reset:
    jButton1.setText("");

Set Text (HTML)

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

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

Set Icon

jButton1.setIcon(new ImageIcon(getClass().getResource("/icon.png")));
Reset:
    jButton1.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")));
            jButton1.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"));
        jButton1.setIcon(icon);

Set Pressed Icon

jButton1.setPressedIcon(new ImageIcon(getClass().getResource("/icon_pressed.png")));
Reset:
    jButton1.setPressedIcon(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")));
            jButton1.setPressedIcon(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"));
        jButton1.setPressedIcon(icon);

Set Rollover Icon

jButton1.setRolloverIcon(new ImageIcon(getClass().getResource("/icon_rollover.png")));
Reset:
    jButton1.setRolloverIcon(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")));
            jButton1.setRolloverIcon(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"));
        jButton1.setRolloverIcon(icon);

Set Disabled Icon

jButton1.setDisabledIcon(new ImageIcon(getClass().getResource("/icon_disable.png")));\n\n\n"
Reset:
    jButton1.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")));
            jButton1.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"));
        jButton1.setDisabledIcon(icon);

Set Selected Icon

jButton1.setSelectedIcon(new ImageIcon(getClass().getResource("/icon_select.png")));
Reset:
    jButton1.setSelectedIcon(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")));
            jButton1.setSelectedIcon(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"));
        jButton1.setSelectedIcon(icon);

Set Selected Rollover Icon

jButton1.setRolloverSelectedIcon(new ImageIcon(getClass().getResource("/icon_select_rollover.png")));
Reset:
    jButton1.setRolloverSelectedIcon(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")));
            jButton1.setRolloverSelectedIcon(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"));
        jButton1.setRolloverSelectedIcon(icon);

Set Selected Disabled Icon

jButton1.setDisabledSelectedIcon(new ImageIcon(getClass().getResource("/icon_select_disable.png")));
Reset:
    jButton1.setDisabledSelectedIcon(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")));
            jButton1.setDisabledSelectedIcon(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"));
        jButton1.setDisabledSelectedIcon(icon);