Hi Guys

Im having a problem.

Im very new to Java and am using blueJ.

I would like to displya a picture on a label when I click a button. At the moment all I can get it to do is to print a message when a button is clicked.

Ive tried numerous things and am getting frustrated. If anyone has any solutions/ pointers i would be very grateful. My code is below:

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


public class clsScreen implements ActionListener //actionlistener is needed to make buttons work 
{
    // instance variables - replace the example below with your own
 private static final String PATH = "I:/More Quiz Pictures/";  // Path to where pictures are placed

 private JLabel screenLabel; // creates a label so i can put images onto it
private JLabel screenLabel1;
public clsScreen()
    {

    }

    public void makescreen()
    {

       JFrame frame = new JFrame("Television"); //Creates a new frame called television
       Container contentPane = frame.getContentPane();
    contentPane.setLayout(new BorderLayout());


    JLabel screenLabel = new JLabel(new ImageIcon(PATH+"TVOFF.JPG")); // Sets the screen label to a blank screen which will
                                                              //simulate a blank screen

    contentPane.add(screenLabel, BorderLayout.CENTER);// CENTERS THE lABEL TO THE MIDDLE OF THE FRAME

    JPanel southPanel = new JPanel();
        JPanel buttonPanel = new JPanel();
     buttonPanel.setLayout(new FlowLayout());
 // ***********************************************


         southPanel.add(buttonPanel, BorderLayout.SOUTH);
       contentPane.add(southPanel, BorderLayout.SOUTH);

       //***Start of button creation*************
  JButton channelButton1 = new JButton("1"); //Create the button
    channelButton1.addActionListener(this);     // adds a actionlistener so the button responds to a click 
  buttonPanel.add(channelButton1); // add the button to the button panel

  //****Creating button 2******
       JButton channelButton2 = new JButton("2"); 
      channelButton2.addActionListener(this);
       buttonPanel.add(channelButton2); 

//****Creating button 3******
       JButton channelButton3 = new JButton("3"); 
         channelButton3.addActionListener(this);
       buttonPanel.add(channelButton3); 

//****Creating Button 4******
       JButton channelButton4 = new JButton("4"); 
       channelButton4.addActionListener(this);
       buttonPanel.add(channelButton4); 

//****Creating Guide Button******
       JButton channelButtonGuide = new JButton("T.V Guide");
         channelButtonGuide.addActionListener(this);
       buttonPanel.add(channelButtonGuide);
//*****Creating the standby Button******
       JButton channelButtonStandby = new JButton("Standby");
        channelButtonStandby.addActionListener(this);
       buttonPanel.add(channelButtonStandby);

//*****End of button creation***// 
     frame.pack(); //will pack the border down to fit the content 
       frame.setVisible(true); // shows the frame when method is called 



    }
 public void actionPerformed(ActionEvent event)
{ 
String command = event.getActionCommand(); //Command = button that is clicked

     if(command.equals("1")) //if 1 button is clicked then display 1. 
            {


           System.out.println("1");
        screenLabel1.setIcon(new ImageIcon(PATH + "Pic01.JPG"));


        }
    else if (command.equals("2")) 
        {


            System.out.println("2");

        }
   else if(command.equals("3"))
{
   System.out.println("3");
    }

   else if(command.equals("4"))
{
   System.out.println("4");
    }

    else if(command.equals("T.V Guide"))
{
   System.out.println("Guide");
}
   else if(command.equals("Standby"))
{
   System.out.println("Standby");
    }   

}

}//End

Recommended Answers

All 7 Replies

Are you certain that the Pic01.JPG file really is there, and its name is correctly capitalised? (just checking). May be worth just seeing if the new ImageIcon isn't null.

Are you certain that the Pic01.JPG file really is there, and its name is correctly capitalised? (just checking). May be worth just seeing if the new ImageIcon isn't null.

Hey
Thanks for your response. Im sure the file is there and that the caps are correct.

Thanks anyway.

Here's something (maybe). You declare screenLabel1, and you set its icon, but I can't see where you add it to the form.
Also, you declare screenLabel, but when you go to add that to the form you re-declare it within that method, so the one you declared first is not used either.

[QUOTE=JamesCherrill;874004]Here's something (maybe). You declare screenLabel1, and you set its icon, but I can't see where you add it to the form.
Also, you declare screenLabel, but when you go to add that to the form you re-declare it within that method, so the one you declared first is not used either.[/QUOTE]

Hey

Thanks for the reply and notifying me that I have declared screenLabel twice :-S

Ive just tried what you have suggested but no luck the image still does not load.
Would i need to create a new label or is it possible to overwrite the screenlabel image?
Apoligies for being thick just not sure why this is not working.

Anyway ive tried the below but still no luck :-(

Thanks anyway

public void actionPerformed(ActionEvent event)
{ 
String command = event.getActionCommand(); //Command = button that is clicked

JLabel screenLabel1 = new JLabel();      


if(command.equals("1")) //if 1 button is clicked then display 1. 
        {


       System.out.println("1");
    screenLabel1.setIcon(new ImageIcon(PATH + "Pic01.JPG"));


    }

No, that's exactly the wrong thing to do. Just declare it once, at the start of your class. Then you can add it to the form, and subsequently change its icon. Declaring it more than once will mean you are adding one label to the form, but changing the icon on a different (invisible) label.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.