javauser-interfacestaticactionlistenerlistselectionlistener

Passing Variables From ListSelectionListeners and ActionListeners


I'm having issues properly passing values/variables as result of ListSelectionListener and ActionListener methods from my GUI. Basically, I am receiving (6) six errors stemming from the same problem.

non-static variable cannot be referenced from a static context

I've had problems before with fully understanding this error and thought that I had it. But once we started to learn GUI's and how to implement them, I have lost my understanding again. Ideally, I wish to take an end-users decision(s) and pass that along to the main GUI function. Below is a portion of public class EnemyPanel extends JPanel that feeds off of my main JFrame of my program. Everything was fine until I attempted to do the following:

public class PlayerPanel extends JPanel
{

private JPanel compPlayerPanel;
private JPanel playerPanel;
private JList playerList;
private JPanel playerListPanel;
private JLabel playerImageLabel;
private JPanel playerImagePanel;
private JPanel buttonPanel;
private JButton selctButton;
private ImageIcon playerImage;
private String playerSlctd;
public int playerChoice;
public int playerWeaponChoice;


private void buildWeaponSelectionPanel()
{
    weaponPanel = new JPanel();

    weaponListPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
    weaponListPanel.setPreferredSize(new Dimension(180, 85));
    weaponListPanel.setBackground(Color.WHITE);
    weaponPanel.setLayout(new BoxLayout(weaponPanel, BoxLayout.Y_AXIS));

    weaponList = new JList(weapons);

    weaponList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    weaponList.addListSelectionListener(new WeaponListListener());
    weaponListPanel.add(weaponList);
    weaponPanel.add(weaponListPanel);
}
private class WeaponListListener implements ListSelectionListener
{
    public void valueChanged(ListSelectionEvent e)
    {
        weaponSlctd = (String) weaponList.getSelectedValue();

        if (weaponSlctd == "Mace")
        {
            weaponImage = new ImageIcon("Mace.png");
            playerWeaponChoice = 1;
        }
        else if(weaponSlctd == "Short Sword")
        {
            weaponImage = new ImageIcon("ShortSword.png");
            playerWeaponChoice = 2;
        }
        else if(weaponSlctd == "Long Sword")
        {
            weaponImage = new ImageIcon("LongSword.png");
            playerWeaponChoice = 3;
        }
        else if(weaponSlctd == "Axe")
        {
            weaponImage = new ImageIcon("Axe.png");
            playerWeaponChoice = 4;
        }

        weaponImageLabel.setIcon(weaponImage);

        weaponImageLabel.setText(null);
    }
}

public int getPlayerWeaponSelected()
{
    return playerWeaponChoice;
}

Basically, the end-user selects from a JList and depending on their selection, a .png displays and their selection is recorded as weaponSlctd in the method above. My issue is that I would like to take that value and use it in the following method of my JFrame:

private class RunButtonListener implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
                    playerWeaponType = PlayerPanel.getPlayerWeaponSelected();
    }
}

I have a lot more code but I don't feel it is necessary to post it all due to several reasons. A) the player follows a template that other of my JLabels are designed after. B) The error during compilation arises from the method getPlayerSelected() which is dependent on the WeaponListListener class. I am positive that it is the way I'm attempting to pass the integer weaponSlctd from one method to the next, and from one extended JPanel to the parent JFrame that is calling it all.

I'd appreciate some help and insight into what I'm doing wrong and/or how I can change my code that will permit me to pass JList, JButton, and other end-user selection variables into a main method. I'm having a disconnect on the appropriate way of doing so. I've read some of the other threads concerning static and non-static variables and they have helped me in the past. But this is the first time I'm building and manipulating the swing and awt extensions to help with a GUI. I believe that for some reason incorporating the ActionListener and ListSelectionListener functionality into the mix has tossed me for a loop.


Solution

  • non-static variable cannot be referenced from a static context
    

    I used to get this error so much...Okay, so you have two options as of what to do:

    1. You make your fields static
    2. Make your methods non-static by creating an instance of the class (object)

    When using that method, I suggest you make an object of the class, and call it by doing:

    Object.method();
    

    Or, you can just make the fields themselves static, and access it through static variables.

    Static can only access more static. Static cannot access instance.

    Static methods and variables do not belong to an instance of a class, which is why you cannot use them with an object. So, again, either make everything static, or access the method and variable using an object, which is an instance of the class.