CodeDump
CodeDump

Reputation: 23

Causing a JPanel Image to change from an actionPerformed() event

I am trying to make a GUI and as part of it I want to make a JPanel gain an image when I create a new instance of a class. The new class instance is created by pressing a button addBox.addActionListener(new addParcelListener(session,1)); during which it identifies a Imageicon to be used icon = new ImageIcon(this.getClass().getResource("images/box.png"));.

How would I go about making it so I can do bay1.addImage(icon) when the ImageIcon is inside the public void actionPerformed(ActionEvent event)?

Main Code

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

public class DispatchDepot
{
    public static void main(String[] args) 
    {
        DispatchDepot mainScreen = new DispatchDepot();
        mainScreen.createGUI();
    }
    private void createGUI() 
    {
        DepotPackages session = new DepotPackages();
        //Main Window
        JFrame theDepot = new JFrame("Dispatch Depot");
        theDepot.setResizable(false);
        theDepot.setLayout(new BorderLayout());
        //Button Panel
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridLayout(8, 1));
        buttonPanel.setPreferredSize(new Dimension(200, 800));
        buttonPanel.setMaximumSize(new Dimension(200, 800));
        //Buttons
        JButton addBox = new JButton("Add a Box");
        buttonPanel.add(addBox);
        theDepot.add(buttonPanel, BorderLayout.EAST);
        //Bay Panel
        JPanel bayPanel = new JPanel();
        bayPanel.setLayout(new GridLayout(2, 3));
        bayPanel.setPreferredSize(new Dimension(800, 800));
        bayPanel.setMaximumSize(new Dimension(800, 800));
        Border bayBorder = new LineBorder(Color.BLUE, 2);
        //Bay 1
        JPanel bay1 = new JPanel();
        JLabel label1 = new JLabel("Bay 1", JLabel.CENTER);
        bay1.setName("Bay 1");
        bay1.setBorder(bayBorder);
        bay1.add(label1);
       //GUI Construction
        bayPanel.add(bay1);
        theDepot.add(bayPanel, BorderLayout.WEST);
        //Display GUI
        theDepot.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        theDepot.pack();
        theDepot.setLocationRelativeTo(null);
        theDepot.setVisible(true); 
        //Event Listeners - Buttons
        addBox.addActionListener(new addParcelListener(session,1));
    }
    private class addParcelListener implements ActionListener
    {
        final private DepotPackages session;
        final private int type;
        public addParcelListener(DepotPackages session, int type)
        {
            this.session=session;
            this.type=type;
        }
        @Override
        public void actionPerformed(ActionEvent event)
        {
            if(session.checkBaySpace())
            {
                int zone = zoneEntryGUI();
                if(session.checkZoneSpace(zone))
                {
                    char zoneChar;
                    switch(zone)
                    {
                        case 1: zoneChar = 'a';break;
                        case 2: zoneChar = 'b';break;
                        default: zoneChar = 'c';break;
                    }
                    int id = idEntryGUI();
                    int bay;
                    ImageIcon icon;
                    switch(type)
                    {

                        case 1: {Parcel parcel = boxEntryGUI(id,zoneChar); icon=parcel.getImage(); session.newEntry(zone, parcel); break;}
                        case 2: {Parcel parcel = tubeEntryGUI(id,zoneChar); icon=parcel.getImage(); session.newEntry(zone, parcel); break;}
                        default: {Parcel parcel = envelopeEntryGUI(id,zoneChar); icon=parcel.getImage(); session.newEntry(zone, parcel); break;}
                }
            }
            else{System.out.println("No Zone Space");}
        }
        else{System.out.println("No Bay Space");}
    }
}

public class Box Code

public ImageIcon getImage()
    {
        ImageIcon icon;
        switch(this.zone)
        {
            case 'a': {icon = new ImageIcon(this.getClass().getResource("images/box.png")); break;}
            case 'b': {icon = new ImageIcon(this.getClass().getResource("images/box.png")); break;}
            case 'c': {icon = new ImageIcon(this.getClass().getResource("images/box-large.png")); break;}
            default: {icon=null; break;}
        }
        return icon;
    }

Sorry if you find my question difficult to understand. Thank you

Thank you Ezequiel and user1803551 your answers were very useful @Andrew Thompson Thank you I will keep your notes in mind next time I ask a question

Upvotes: 2

Views: 202

Answers (1)

Ezequiel
Ezequiel

Reputation: 3592

Since your code is not compilable is hard to post you a compilable answer.

The problem is that JPanel bay1 = new JPanel(); is a local variable, and the actionPerformed has no visibility of it.

Instead extract JPanel bay1 = new JPanel(); as a field:

public class DispatchDepot
{
    final private JPanel bay1 = new JPanel();

And you'll have visibility of bay1 inside your private inner class.

Upvotes: 1

Related Questions