Adding image to Jbutton with foreground label

17,405

Simply use

studentsButton.setHorizontalTextPosition(AbstractButton.CENTER);
studentsButton.setVerticalTextPosition(AbstractButton.BOTTOM);

This will simply place the Text below the Image. And the output will be like this :

Here is one code example for your help having OUTPUT as output:

import java.awt.*;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.*;
import javax.imageio.ImageIO;

public class ButtonImageExample
{
    private JButton imageButton;
    private ImageIcon image;

    private void displayGUI()
    {
        JFrame frame = new JFrame("Button Image Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel contentPane = new JPanel();
        try
        {
            image = new ImageIcon(ImageIO.read(
                    new URL("http://i.imgur.com/6mbHZRU.png")));
        }
        catch(MalformedURLException mue)
        {
            mue.printStackTrace();
        }
        catch(IOException ioe)
        {
            ioe.printStackTrace();
        }       

        imageButton = new JButton("Button Text");
        imageButton.setIcon(image);
        imageButton.setHorizontalTextPosition(AbstractButton.CENTER);
        imageButton.setVerticalTextPosition(AbstractButton.BOTTOM);

        contentPane.add(imageButton);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new ButtonImageExample().displayGUI();
            }
        });
    }
}

LATEST EDIT : REGARDING ADDING BACKGROUND IMAGE THROUGH JLABEL

import java.awt.*;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.*;
import javax.imageio.ImageIO;

public class ButtonImageExample
{
    private ImageIcon image, imageForLabel;
    private JLabel imageLabel;

    private JTextField userField;
    private JPasswordField passField;
    private JButton loginButton;

    private void displayGUI()
    {
        JFrame frame = new JFrame("Button Image Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout(5, 5));
        try
        {
            image = new ImageIcon(ImageIO.read(
                    new URL("http://i.imgur.com/jwyrvXC.gif")));
            imageForLabel = new ImageIcon(ImageIO.read(
                    new URL("http://i.imgur.com/09zgEvG.jpg")));           
        }
        catch(MalformedURLException mue)
        {
            mue.printStackTrace();
        }
        catch(IOException ioe)
        {
            ioe.printStackTrace();
        }       

        imageLabel = new JLabel(imageForLabel);
        JPanel basePanel = new JPanel();
        // setOpaque(false) is used to make the JPanel translucent/transparent.
        basePanel.setOpaque(false);
        basePanel.setLayout(new BorderLayout(5, 5));
        JPanel topPanel = new JPanel();     
        topPanel.setOpaque(false);
        topPanel.setLayout(new GridLayout(2, 2, 5, 5));
        JLabel userLabel = new JLabel("USERNAME : ", JLabel.CENTER);
        userLabel.setForeground(Color.WHITE);
        userField = new JTextField(10);
        JLabel passLabel = new JLabel("PASSWORD : ", JLabel.CENTER);
        passLabel.setForeground(Color.WHITE);
        passField = new JPasswordField(10);

        topPanel.add(userLabel);
        topPanel.add(userField);
        topPanel.add(passLabel);
        topPanel.add(passField);

        JPanel bottomPanel = new JPanel();
        bottomPanel.setOpaque(false);
        loginButton = new JButton("Click to LOGIN");
        loginButton.setIcon(image);
        loginButton.setHorizontalTextPosition(AbstractButton.CENTER);
        loginButton.setVerticalTextPosition(AbstractButton.BOTTOM);
        bottomPanel.add(loginButton);

        basePanel.add(topPanel, BorderLayout.CENTER);
        basePanel.add(bottomPanel, BorderLayout.PAGE_END);

        imageLabel.setLayout(new GridBagLayout());
        imageLabel.add(basePanel);

        contentPane.add(imageLabel, BorderLayout.CENTER);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new ButtonImageExample().displayGUI();
            }
        });
    }
}

Here is the output of the same :

JLabelAsBackground

Share:
17,405
Mach Mitch
Author by

Mach Mitch

Updated on June 04, 2022

Comments

  • Mach Mitch
    Mach Mitch almost 2 years

    Friends, i m trying add image to my Jbutton using seticon method but it hide the text label on the button. Here is the code :

          try {
            Image img = ImageIO.read(getClass().getResource("image.jpg"));
            studentsButton.setIcon(new ImageIcon(img));         
          } catch (IOException ex) {
          }
    

    And i m using swing in eclipse without init()/paint()/graphics, its simple frame in main method.

  • Mach Mitch
    Mach Mitch almost 12 years
    Could you also tell me how to change background image for a JPanel without using subclass?
  • Guillaume Polet
    Guillaume Polet almost 12 years
    @MachMitch use a JLabel for that
  • nIcE cOw
    nIcE cOw almost 12 years
    As suggested by @GuillaumePolet, you can use JLabel for this, set it's Layout and add components to it. Give me some time for the example.
  • Mach Mitch
    Mach Mitch almost 12 years
    really nice example but i have already designed my layout and now adding everything to JLabel would be very difficult since i have used 16 JPanels already.....it will be easy to provide panel a background image instead of messing with the whole layout