1

So I am trying to put an image on a JButton by the means of the JButton constructor

JButton button = new JButton(ImageIcon image)

I have a few icons are defined as

ImageIcon OpenIcon = new ImageIcon("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\open.jpeg");
ImageIcon SaveIcon = new ImageIcon("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\save.jpeg");
ImageIcon CutIcon = new ImageIcon("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\cut.jpeg");
ImageIcon CopyIcon = new ImageIcon("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\copy.jpeg");
ImageIcon PasteIcon = new ImageIcon("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\paste.jpeg");

The icon files are in a folder called

C:\Users\Wonseok\Documents\CompSciClass\Homework Files\icons.

The buttons that these icons are on are defined as

JButton OpenButton = new JButton(OpenIcon);
JButton SaveButton = new JButton(SaveIcon);
JButton CutButton = new JButton(CutIcon);
JButton CopyButton = new JButton(CopyIcon);
JButton PasteButton = new JButton(PasteIcon);

I have these in a JPanel. My problem is that the icons aren't showing properly on the JButtons, and I have no idea why. The buttons show up as small, thin, blank rectangles without an image on it. If you want a picture, please tell me how to post one from my computer. Is this my computer being annoying and glitchy? Or is there something wrong with my coding?

The pertinent code is

JPanel ButtonBar = new JPanel(new FlowLayout());
JPanel FileActions = new JPanel(new GridLayout(1, 2));
ImageIcon OpenIcon = new ImageIcon("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\open.jpeg");
ImageIcon SaveIcon = new ImageIcon("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\save.jpeg");
JButton OpenButton = new JButton(OpenIcon);
JButton SaveButton = new JButton(SaveIcon);
JPanel TextActions = new JPanel(new GridLayout(1, 3));
ImageIcon CutIcon = new ImageIcon("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\cut.jpeg");
ImageIcon CopyIcon = new ImageIcon("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\copy.jpeg");
ImageIcon PasteIcon = new ImageIcon("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\paste.jpeg");
JButton CutButton = new JButton(CutIcon);
JButton CopyButton = new JButton(CopyIcon);
JButton PasteButton = new JButton(PasteIcon);

public basic_text_editor()
{

    super("Text Editor");
    setSize(WIDTH, HEIGHT);
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    getContentPane().setBackground(Color.WHITE);
    setLayout(new FlowLayout(FlowLayout.LEFT));

    FileActions.add(OpenButton);
    FileActions.add(SaveButton);

    TextActions.add(CutButton);
    TextActions.add(CopyButton);
    TextActions.add(PasteButton);

    ButtonBar.add(FileActions);
    ButtonBar.add(TextActions);

    add(ButtonBar);

}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
ws04
  • 45
  • 9
  • 1
    When debugging this sort of thing, it's often best to simplify the problem and try to isolate it. Consider creating a small program that just reads in one image file and displays the ImageIcon in a JOptionPane, that's it, and then try to debug that. If you're still stuck, then you can post a much simpler program for us to review. Having said that, often the problem is an incorrect path. Myself, I usually read in my images as BufferedImages using resource streams. – Hovercraft Full Of Eels Apr 13 '14 at 22:34
  • For an example of what I mean, please look at the small program that I posted in [this answer](http://stackoverflow.com/a/20623351/522444), only your code would not get the image from an online URL. – Hovercraft Full Of Eels Apr 13 '14 at 22:53
  • You need at least 10 rep to post an image. You can either provide a link to the image, and someone else will embed it for you, or wait for one more upvote (which I think this question definitely deserves). – Adi Inbar Apr 13 '14 at 22:54
  • What will happen if you try this program on different machine. Put the images in the project itself. – Braj Apr 13 '14 at 22:55
  • The only reasons you might be seeing this (based on your code) would be because the images can't be loaded (either aren't images or can't be found). Try using something like `System.out.println(new File("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\open.jpeg").exits());` to see if the image exists (should display `true` if the path/name is correct), then try using `ImageIO.read(File("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\open.jpeg"));` to see if the image can be loaded correctly. – MadProgrammer Apr 14 '14 at 00:08
  • This throws an `IOException` and is generally the preferred way of loading images – MadProgrammer Apr 14 '14 at 00:09

2 Answers2

1

Put the images in the project itself otherwise it will not work when this code is shipped on another machine. Avoid absolute path.

Looking an image 2.png from resources folder

ImageIcon folderIcon = new ImageIcon("resources/2.png");

OR

ImageIcon icon = new ImageIcon(ImageIO.read(new File("resources/2.png")));

OR

Try this one if the image is in the package where the class is present

ImageIcon icon = new ImageIcon(ImageIO.read(getClass().getResourceAsStream("2.png")));

OR

I never suggest you to use absolute path but you can try it.

ImageIcon icon = new ImageIcon(ImageIO.read("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\open.jpeg"));

OR

ImageIcon icon = new ImageIcon("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\open.jpeg");

If you want to re-size the image then use this method.

public BufferedImage resizeImage(BufferedImage originalImage, int width, int height)
        throws IOException {
    BufferedImage resizedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(originalImage, 0, 0, width, height, null);
    g.dispose();
    return resizedImage;
}

how to convert an Image(BufferedImage) into ImageIcon?

 ImageIcon icon = new ImageIcon(image);

Here is the project structure

enter image description here

Braj
  • 46,415
  • 5
  • 60
  • 76
0

Thanks all, but I've found the answer to my problem.
The file extension shouldn't have been .jpeg, which is the name for the type of file, but should've been .jpg. Thanks for your help, anyways. I'll make a copy of the folder in my project folder and use relative paths. :D

ws04
  • 45
  • 9