2

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

CodeDump
  • 23
  • 4
  • What about this code doesn't do what you want? – Eric Hauenstein May 20 '15 at 15:03
  • 2
    1) See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) 2) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 3) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson May 20 '15 at 15:07
  • 1
    `JPanel bay1 = new JPanel();` This panel is declared with a scope local to the method it is in. It is unreachable outside that method. one way to access it in the method of interest is to declare it as an attribute of the class. But really, this is the sort of stuff ([scope of variables](https://docs.oracle.com/javase/tutorial/java/javaOO/variables.html)) you should work out in **command line apps.** – Andrew Thompson May 20 '15 at 15:16

1 Answers1

1

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.

Ezequiel
  • 3,477
  • 1
  • 20
  • 28
  • This works for getting me the access to bay1 but `bay1.addImage(icon)` is throwing out an error saying "cannot find symbol symbol:method addImage(ImageIcon) location: variable bay1 of type JPanel" – CodeDump May 20 '15 at 17:13
  • @CodeDump Does the `JPanel` class contain an `addImage(Icon icon)` method? – user1803551 May 20 '15 at 17:23
  • @user1803551 according to the API... no it doesnt not, thank you for pointing that out for me – CodeDump May 20 '15 at 17:58