Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8213677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:12:05+00:00 2026-06-07T11:12:05+00:00

Friends, i m trying add image to my Jbutton using seticon method but it

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T11:12:08+00:00Added an answer on June 7, 2026 at 11:12 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Friends, I am trying to rotate image using css3. On w3schools i found :
I am trying to hide my Add friend link if - current_user and @user
I'm trying to get a list of user's friends using fb connect for the
Hi friends, I am trying to get the string data and image data from
friends, i am trying to upload file to php server using following tutorial http://getablogger.blogspot.com/2008/01/android-how-to-post-file-to-php-server.html
I'm trying to use checkboxes to add users to a specific group but I'm
edit - I solved my add friend button issue, now I'm trying to get
I'm trying to add a large body of text into a String. For this,
I'm trying to write my own code for a 'friends-of-friends' algorithm. This algorithm acts
Hi friends. I'm trying to add a product to a SQLlite Database when a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.