# ImageIcon help.



## ConstellationPF (Oct 13, 2007)

Hi everybody. I'm almost 6 months into GUIS and Java and am running into a little problem with my image. What I basically want to do is add an image and I've done all that I can to make the picture appear and try to figure out what is wrong and I can't figure it out. My whole program compiles perfectly but the image doesn't appear. But anyway, any help is appreciated very much. Here's the code I have just for the main frame(where I want to put the picture) Thanks for taking the time to read this. Have a great day. :


```
import java.io.*;
import javax.swing.*;//JApplet,JButton,JLabel,JTextField
import java.awt.*;//Container,FlowLayout
import java.awt.event.*;//Action event/ActionListener
import java.util.*;
 
	 class ComputerAssignmentTwo implements ActionListener
	 {
	  
	  public static void main(String[] args)
	  {
	    ComputerAssignmentTwo start = new ComputerAssignmentTwo();
	   
	  }
	       	    	
	 //declaring all the buttons  
	 
	    JButton startButton = new JButton("Start checking!");
	    JButton instructionsButton = new JButton("Instructions");
	    JButton exitButton = new JButton("Exit");
	    ImageIcon testing = new ImageIcon("image\trying.gif");
	    JButton pictureButton = new JButton(testing);
	    	    
	    //Frames
	  
	    JFrame mainFrame = new JFrame("Main");
	    JFrame instructionsFrame = new JFrame("Instructions");
	    JFrame gameFrame = new JFrame();
	  
	    //Labels
	    
	    JLabel mainFrameTitle = new JLabel();//label for the main frame
	   
	     
	 ComputerAssignmentTwo()
	 
	 //welcome page
	 {
	    //thank you opening message
	    
	    JOptionPane.showMessageDialog(null, "Thank you for using my programme");//opening message saying thank you for using my Tester!
	    
	    //create panel
	    
	    JPanel mainFramePanel= new JPanel(new FlowLayout());
	    ImageIcon picture= new ImageIcon("5.jpg");
	   JLabel peopleLabel= new JLabel(picture);
	  
	  //add action listener to buttons
	   
	    startButton.addActionListener(this);
	    instructionsButton.addActionListener(this);
	    exitButton.addActionListener(this);
	    pictureButton.addActionListener(this);
	 
	    //changing the colour and the font of the various buttons on the main page
	    
	    startButton.setFont(new Font("Times New Roman", 1, 12));//changes the font to Times New Roman
	    startButton.setForeground(Color.red);//changes the colour of the font to red
	    instructionsButton.setFont(new Font("Times New Roman", 1, 12));
	    instructionsButton.setForeground(Color.red);
	    exitButton.setFont(new Font("Times New Roman", 1, 12));
	    exitButton.setForeground(Color.red);
	    pictureButton.setIcon(testing);
	
	    //Adding names to JPanel
	     mainFramePanel.add(pilarLabel);
	    mainFramePanel.add(startButton);
	    mainFramePanel.add(instructionsButton);
	    mainFramePanel.add(exitButton);
                    mainFramePanel.add(pictureButton);
                    mainFramePanel.add(peopleLabel);
	   
                       //Setting size to JPanel
	   mainFrame.setContentPane(mainFramePanel);
    	    //mainFramePanel.setBackground(Color.black);
	    mainFrame.setSize(300,300);
	    mainFrame.setVisible(true);
	   	 
	 }//end of mainFrame creation
```


----------



## Chicon (Jul 29, 2004)

Hi constellationPF,

Glad to see you back ! 

I've had a try with your coding and I'm getting compilation errors with the implementation of _ActionListener_ and _pilarLabel_ is not declared.
Also, it is not a good idea to set a listener at the highest level of a Swing application as it may be difficult to locate the component involved in the action fired by the user.
In your example, it is better to assign a different listener to each button.
I've rebuild your coding this way and it works :

```
[SIZE=2]
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class ComputerAssignmentTwo_correction {
    // AWT and Swing components
    private JFrame mainFrame;
    private JFrame instructionsFrame;
    private JFrame gameFrame;
    
    private JPanel mainFramePanel;
    
    private JLabel mainFrameTitle;
    private JLabel peopleLabel;

    private JButton startButton;
    private JButton instructionsButton;
    private JButton exitButton;
    private JButton pictureButton;
    
    private ImageIcon picture;
    private ImageIcon testing;
    
    // Contructor
    public ComputerAssignmentTwo_correction() {
        initComponents();
        mainFrame.setVisible(true);
    }
    
	// Starter
    public static void main(String[] args) {
        new ComputerAssignmentTwo_correction();
    }
    
    private void initComponents() {
        // init & build main frame
        mainFrame = new JFrame("Main");
        mainFrame.setSize(300, 300);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFramePanel = new JPanel(new FlowLayout());
        mainFrameTitle = new JLabel("Some title");
        picture = new ImageIcon("smile.gif");
        peopleLabel = new JLabel(picture);
        startButton = new JButton("Start checking!");
        startButton.setFont(new Font("Times New Roman", 1, 12));
        startButton.setForeground(Color.red);
        startButton.addMouseListener(new MAdapter(0));
        instructionsButton = new JButton("Instructions");
        instructionsButton.setFont(new Font("Times New Roman", 1, 12));
        instructionsButton.setForeground(Color.red);
        instructionsButton.addMouseListener(new MAdapter(1));
        testing = new ImageIcon("thumbsup.gif");
        pictureButton = new JButton(testing);
        pictureButton.addMouseListener(new MAdapter(2));
        exitButton = new JButton("Exit");
        exitButton.setFont(new Font("Times New Roman", 1, 12));
        exitButton.setForeground(Color.red);
        exitButton.addMouseListener(new MAdapter(3));
        mainFramePanel.add(mainFrameTitle);
        mainFramePanel.add(startButton);
        mainFramePanel.add(instructionsButton);
        mainFramePanel.add(exitButton);
        mainFramePanel.add(pictureButton);
        mainFramePanel.add(peopleLabel);
        mainFrame.getContentPane().add(mainFramePanel);
        
        // init & build instructions frame
        instructionsFrame = new JFrame("Instructions");
                // TODO
        
        // init & build game frame
        gameFrame = new JFrame();
                // TODO
    }
    
    // Actions fired 
    
    private void startButtonClicked() {
        // TODO
    }
    
    private void instructionsButtonClicked() {
        // TODO
    }
    
    private void exitButtonClicked() {
        System.exit(0);
    }
    
    private void pictureButtonClicked() {
        // TODO
    }
    
    private class MAdapter extends MouseAdapter {
        int status;
        
        public MAdapter(int status) {
            this.status = status;
        }
        
        @Override
        public void mouseClicked(MouseEvent evt) {
            switch(status) {
                case 0:
                    startButtonClicked();
                    break;
                case 1:
                    instructionsButtonClicked();
                    break;
                case 2:
                    pictureButtonClicked();
                    break;
                default:
                    exitButtonClicked();
            }
        }        
    }
}[/SIZE]
```
Here's the proof :


----------



## ConstellationPF (Oct 13, 2007)

Hi! Thank you so much. I have figured it out. The only problem now is I don't know how to change the image when the user presses a button. I have tried setting the label to .setVisible(false) and then setting that same label to a different icon with setIcon(),but it doesn't do anything it just stays there and no new picture appears. Are there any ideas on how I could change the picture, that is relatively simple. Any help is appreciated. Thanks.


----------



## Chicon (Jul 29, 2004)

Just have a try writing the full path of your pictures files and also, use */* instead of *\* as *\* may be considered like the begin of a escape character.


----------



## ConstellationPF (Oct 13, 2007)

Hi there, thanks for the quick reply, but unfortunately I don't know what try's are for, and was wondering if you could give me an example on how one works and explain how it would work to change my picture. Thanks a lot. Much appreciated.


----------



## Chicon (Jul 29, 2004)

ConstellationPF said:


> ... I have figured it out. The only problem now is I don't know how to change the image when the user presses a button...


Sorry ! I've been interrupted.

While treating the mouse clicked event, you've just to use the _setIcon_ method of the _JButton_ instance. Here's an example from the coding I posted :

```
[SIZE=2]
    private void pictureButtonClicked() {
        pictureButton.setIcon(new ImageIcon("biggrin.gif"));
    }
[/SIZE]
```


----------



## ConstellationPF (Oct 13, 2007)

It worked for changing the button icon. But it didn't work for changing the label's icon. This is what I did:

```
peopleLabel.setIcon(new ImageIcon("Negativity.jpg"));
```
And it still shows me the same picture instead of the Negativity one.


----------



## Chicon (Jul 29, 2004)

Make sure the name of the file is correct as Java is case sensitive.
Always, from my coding, this works :

```
[SIZE=2]
    private void pictureButtonClicked() {
        pictureButton.setIcon(new ImageIcon("biggrin.gif"));
        peopleLabel.setIcon(new ImageIcon("cool.gif"));
    }
[/SIZE]
```


----------



## ConstellationPF (Oct 13, 2007)

Thanks it works for other frames, but strangely not for my main frame. I appreciate all your help, time and patience. Have a great day!


----------



## Chicon (Jul 29, 2004)

:up: Thanks ! 

Just one more precision about event listeners.

There's no specific rule to use event listeners when you build a Swing form. 
Generally, it is recommanded to assign the appropriate listener to a specific component as it makes the coding more _malleable_ and more easy to debug. Sometimes, it is also recommanded to assign a listener by group of components.

In the example I posted in Geek4Life's Tic Tac Toe thread, I use 1 listener by cell, therefore 9 listeners. In this case, the loss of performance is insignificant.
If, instead of Tic Tac Toe, the poster had to build a Go board game, make sure I would have never used 361 listeners ! Instead I would have enclosed my board into a pane and assign a listener to the pane.

An event listener is a separate thread which is running in background. It's permanently checking the status of the component(s) for which it was registered.
Therefore, the more listeners you set on a form, the more performance your application will lose.


----------

