JButton – Alignment


Set Text

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

Set Text (HTML)

jButton1.setText("The new  HTML
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 resource path starting with a / lets the file be found at:
        "src/main/resources/" for Maven
        "src/" for Ant

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

    Icon files needs to be a .jpg, .gif, or .png (.bmp files won't work)

Set Pressed Icon

jButton1.setPressedIcon(new ImageIcon(getClass().getResource("/icon_pressed.png")));
Reset:
    jButton1.setPressedIcon(null);

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

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

    Icon files needs to be a .jpg, .gif, or .png (.bmp files won't work)

Set Rollover Icon

jButton1.setRolloverIcon(new ImageIcon(getClass().getResource("/icon_rollover.png")));

Reset:
    jButton1.setRolloverIcon(null);

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

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

    Icon files needs to be a .jpg, .gif, or .png (.bmp files won't work)

Set Disabled Icon

jButton1.setDisabledIcon(new ImageIcon(getClass().getResource("/icon_disable.png")));\n\n\n"

Reset:
    jButton1.setDisabledIcon(null);

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

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

    Icon files needs to be a .jpg, .gif, or .png (.bmp files won't work)

Set Selected Icon

jButton1.setSelectedIcon(new ImageIcon(getClass().getResource("/icon_select.png")));
              

Reset:
    jButton1.setSelectedIcon(null);

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

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

    Icon files needs to be a .jpg, .gif, or .png (.bmp files won't work)

Set Selected Rollover Icon

jButton1.setRolloverSelectedIcon(new ImageIcon(getClass().getResource("/icon_select_rollover.png")));

Reset:
    jButton1.setRolloverSelectedIcon(null);

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

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

    Icon files needs to be a .jpg, .gif, or .png (.bmp files won't work)


Set Selected Disabled Icon

jButton1.setDisabledSelectedIcon(new ImageIcon(getClass().getResource("/icon_select_disable.png")));
Reset:
    jButton1.setDisabledSelectedIcon(null);

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

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

    Icon files needs to be a .jpg, .gif, or .png (.bmp files won't work)