1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

ImageIcon help.

Discussion in 'Software Development' started by ConstellationPF, Apr 12, 2008.

Thread Status:
Not open for further replies.
Advertisement
  1. ConstellationPF

    ConstellationPF Thread Starter

    Joined:
    Oct 12, 2007
    Messages:
    15
    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. :

    Code:
    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
     
  2. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    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 :
    Code:
    [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 :
     

    Attached Files:

  3. ConstellationPF

    ConstellationPF Thread Starter

    Joined:
    Oct 12, 2007
    Messages:
    15
    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.
     
  4. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    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.
     
  5. ConstellationPF

    ConstellationPF Thread Starter

    Joined:
    Oct 12, 2007
    Messages:
    15
    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.
     
  6. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    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 :
    Code:
    [SIZE=2]
        private void pictureButtonClicked() {
            pictureButton.setIcon(new ImageIcon("biggrin.gif"));
        }
    [/SIZE]
     

    Attached Files:

  7. ConstellationPF

    ConstellationPF Thread Starter

    Joined:
    Oct 12, 2007
    Messages:
    15
    It worked for changing the button icon. But it didn't work for changing the label's icon. This is what I did:
    Code:
    peopleLabel.setIcon(new ImageIcon("Negativity.jpg"));
    And it still shows me the same picture instead of the Negativity one.
     
  8. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    :confused:

    Make sure the name of the file is correct as Java is case sensitive.
    Always, from my coding, this works :
    Code:
    [SIZE=2]
        private void pictureButtonClicked() {
            pictureButton.setIcon(new ImageIcon("biggrin.gif"));
            peopleLabel.setIcon(new ImageIcon("cool.gif"));
        }
    [/SIZE]
     

    Attached Files:

  9. ConstellationPF

    ConstellationPF Thread Starter

    Joined:
    Oct 12, 2007
    Messages:
    15
    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!
     
  10. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    (y) 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.
     
  11. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Similar Threads - ImageIcon help
  1. charlzguitarz
    Replies:
    4
    Views:
    960
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/703161

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice