Search code examples
javaswingimageicon

Can someone help me understand how ImageIcon works for java?


So I have about a year a 5/12 months experience playing around with java but I have never been able to make anything outside of skeletons. I would really appreciate it if someone could help me understand how I can make an image from my computer visible using swing.

I have gone between different websites trying to find answers but none of the example codes I've tried have worked out. Stackoverflow has helped in the past to learn java through various questions other people asked so I have made an account to ask a question myself. I'm probably being very dumb but my image never appears despite what I've tried. I come back to trying to understand swing every few months after giving up on it previously and while I feel I have a grasp on some basic concepts such as something should be set as visible, how to make/add a JFrame, etc, it's always this that messes me up.

import javax.swing.*;
import java.awt.*;

public class Main extends JFrame {

public static void main(String[] args) {
        JFrame frame = new JFrame("main");
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        ImageIcon ii = new ImageIcon("C:\\Users\\plasm\\IdeaProjects\\Shdo\\src\\mario.jpg");
        JLabel lable = new JLabel(ii);
        JScrollPane jsp = new JScrollPane(lable);
        frame.getContentPane().add(jsp);
        frame. setSize(1000, 700);
        JButton button = new JButton();
        button.setSize(new Dimension(300, 300));
        button.setLocation(500, 350);
        frame.getContentPane().add(button);

        frame.setVisible(true);

    }
}

The code above is copy-pasted from https://www.daniweb.com/programming/software-development/threads/379864/add-image-and-button-to-jframe aside from the pathway, however, it only shows a basic white JFrame at the set dimensions.


Solution

  • frame.getContentPane().add(jsp); // problem
    frame. setSize(1000, 700);
    JButton button = new JButton();
    button.setSize(new Dimension(300, 300)); // does nothing
    button.setLocation(500, 350); // does nothing
    frame.getContentPane().add(button); //problem
    

    The problem is that the default layout manager for the content pane of the JFrame is a BorderLayout. You are attempting to add two compnents to the CENTER of the BorderLayout which won't work. The button replaces the scroll pane.

    Instead you should be using:

    frame.getContentPane().add(jsp, BorderLayout.CENTER);
    frame. setSize(1000, 700);
    JButton button = new JButton("Testing");
    frame.getContentPane().add(button, BorderLayout.PAGE_END);
    

    Read the section from the Swing tutorial on Layout Manager for more information and working example of a BorderLayout.

    As mentioned in the first comment. There is no need for the getContentPane(). The frame will automatically add the component to the content pane.

    Also, when doing testing it is better to do something like:

    JLabel label = new JLabel("Icon label");
    label.setIcon(ii);
    

    This way if you specify the wrong path for the image, you will at least see the text of the label and you will know the problem is in the path, not with the layout code.