Question:

Why wont my gif display in my java pane?

by  |  earlier

0 LIKES UnLike

I have knocked up a test program to fiddle with images in swing but cant get the image to show in a button on the pane although it shows in the top bar hence the path is right .. any suggestions as to what I am doing wrong.

The code sample is as follows:

package swingTest2;

import java.net.URL;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class SwingTest2 extends JFrame{

public SwingTest2() {

// TODO Auto-generated constructor stub

super("Test Window");

setSize(300,300);

setDefaultCloseOperation(JFrame.EXIT...

JPanel pane = new JPanel();

URL resourceLocation = getClass().getResource("/images/load.gif...

ImageIcon icon = new ImageIcon(resourceLocation);

setIconImage(icon.getImage());

JButton load = new JButton("Load");

JButton save = new JButton("Save");

pane.add(load,icon);

add(pane);

setVisible(true);

}

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

SwingTest2 s = new SwingTest2();

}

}

 Tags:

   Report

1 ANSWERS


  1. Is there a way to add the image to the button BEFORE you add the button to the panel?

    That could be the problem.

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.