views:

82

answers:

3

changed the project since its working now. kinda. the image still isnt changing.

package icnon;

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

public class FrameIconExample extends JFrame implements ActionListener {

    JLabel j;
    JPanel p, l, k;
    JButton picOne, picTwo;
    Container cPane;

    public FrameIconExample() {
        JButton picOne = new JButton("picOne");
        JButton picTwo = new JButton("picTwo");
        picOne.setName("picOne");
        picTwo.setName("picTwo");

        picOne.addActionListener(this);
        picTwo.addActionListener(this);

        p = new JPanel(new GridLayout(2, 1));
        l = new JPanel(new FlowLayout());
        k = new JPanel(new FlowLayout());

        cPane = getContentPane();

        j = new JLabel(new ImageIcon(
            "../meet/src/images/beautiful-closeup-portrait-photography.jpg"));

        l.add(j);
        k.add(picOne);
        k.add(picTwo);
        p.add(l);
        p.add(k);

        add(p);
    }

    public static void main(String[] args) {
        FrameIconExample frame = new FrameIconExample();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setSize(new Dimension(300, 800));
        frame.setTitle("Frame Icon Example");

        // Display the form
        frame.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton temp = (JButton) e.getSource();
        String src = "../meet/src/images/Majken Kruse portrait - john.jpg";

        //System.out.println(src + " " + temp.getName());

        if (temp.getName().equalsIgnoreCase("picOne")) {
            try {

                l.hide();

                try {

                    src = "../meet/src/images/beautiful-closeup-portrait-photography.jpg";
                    System.out.println(src + " " + temp.getName());
                    Icon img;
                    j = new JLabel(new ImageIcon(src));

                    l.add(j);

                    System.out.println("1");
                } catch (Exception q) {
                    q.printStackTrace();
                }

                if (temp.getName().equalsIgnoreCase("picTwo")) {
                    src = "../icontest/images/Majken Kruse portrait - john.jpg";
                    System.out.println(src + " " + temp.getName());
                    Icon img;
                    j = new JLabel(new ImageIcon(src));
                    l.add(j);
                    System.out.println("2");
                }
            } catch (Exception x) {
                x.printStackTrace();
            }
        }
    }
}

excuse the bad indentation. im pretty sure the method l.add(j); is the reason the image doesnt change.

any ideas what it should be?

+1  A: 

I assume you did not set the correct image location. Are you sure the images are in the exact location you specified? What IDE are you using? If you use Eclipse, a refresh of your project might help.

Ham
yeah. the images are fine. they're in the right location.whats a refresh?
OVERTONE
You can go to your project explorer, right click on your project and hit "refresh". If new files are stored in your workspace Eclipse needs a refresh to get them.
Ham
+6  A: 

Note: this answer was made for the revisions 1 and 2 of the question.

It's not an Awt error, it's a NullPointerException.

Your field l is null, because on the moment you thought you created it, you actually masked it with a local variable.

JPanel p = new JPanel(new GridLayout(2, 1));
JPanel l = new JPanel(new FlowLayout());
JPanel k = new JPanel(new FlowLayout());

Should be:

p = new JPanel(new GridLayout(2, 1));
l = new JPanel(new FlowLayout());
k = new JPanel(new FlowLayout());

Read again the error, with the stack trace. It tells you which line is the problem, and the type of the error tells you what happened, in this case.

Gnoupi
that did it! no errors. unfortunately image still isnt changing but thanks.
OVERTONE
@OVERTONE - JLabel has a "setIcon" method. If what you want is changing the image displayed, you should simply call setIcon() on the JLabel. Currently, you try to add a new JLabel to the panel. If what you want is to have a new image added (and not changed), you should probably call a revalidate on the panel.
Gnoupi
setIcon was it. i was writinrg setIon(src) should have been setIcon(img) thanks
OVERTONE
+2  A: 

The problem you are having is that you declare a global JPanel named l, but then when you instantiate JPanels in your constructor, you declare and assign a local scope JPanel named l. When you try to add a component in your actionPerformed, you are attempting to add it to the null global var.

akf