6

So I'm getting the error: "CANNOT REFER TO A NON-FINAL VARIABLE ROLE INSIDE AN INNERCLASS DEFINED IN A DIFFERENT METHOD". I want to be able to set the string roletype to whatever get's selected in that Dropdown. How can I do this if not in the way I'm trying below, or am I simply making some stupid error in the code I'm trying?

Thanks, Ravin

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

public class Funclass extends JFrame {

    FlowLayout layout = new FlowLayout();
    String[] skillz = {"Analytical", "Numerical", "Leadership",
        "Communication", "Organisation", "Interpersonal"};
    String[] rolez = {"Developer", "Sales", "Marketing"};
    String[] Industries = {"Consulting", "Tech"};
    String R1, R2, R3, R4, roletype;

    public Funclass() {
        super("Input Interface");
        setLayout(layout);
        JTextField Company = new JTextField("Company Name");
        JComboBox TYPE = new JComboBox(Industries);
        JList skills = new JList(skillz);
        JComboBox role = new JComboBox(rolez);
        skills.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        add(TYPE);
        add(skills);
        add(role);
        add(Company);

        ROLE.addItemListener(new ItemListener() {

            public void itemStateChanged(ItemEvent event) {
                if (event.getStateChange() == ItemEvent.SELECTED) {
                    roletype = rolez[role.getSelectedIndex()];
                }
            }
        });
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Ravin
  • 626
  • 2
  • 10
  • 25

3 Answers3

2

You need to declare the role variable as final so that the inner class (ItemListener) can have access to it, like so:

final JComboBox role = new JComboBox(rolez); 
João Silva
  • 89,303
  • 29
  • 152
  • 158
  • 1
    This won't work because `roletype` must also be declared `final`. – tskuzzy Aug 01 '11 at 19:16
  • That still won't work because he's modifying `roletype` in the inner class ;) – tskuzzy Aug 01 '11 at 19:21
  • 2
    tskuzzy -- the final restriction only applies when accessing variables scoped inside the method defining the local class. The inner class still has access to the enclosing class variables – Kal Aug 01 '11 at 19:26
  • @Kal: Ah sorry, I thought `roletype` was declared in the constructor as well. :) – tskuzzy Aug 01 '11 at 19:36
1
import java.awt.event.*;
import javax.swing.*;

public class Funclass extends JFrame {

    private static final long serialVersionUID = 1L;
    private String[] rolez = {"Developer", "Sales", "Marketing"};
    private String roletype;
    private JComboBox role;

    public Funclass() {
        role = new JComboBox(rolez);
        role.addItemListener(new ItemListener() {

            public void itemStateChanged(ItemEvent event) {
                if (event.getStateChange() == ItemEvent.SELECTED) {
                    roletype = role.getSelectedItem().toString();
                }
            }
        });
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
0

To access variables in the outer class from an inner class, they must be declared final. So in this case, role must be final.

EDIT: roletype does not need to be declared final even though it's accessed in the innerclass because it is a class variable, not a method variable.

tskuzzy
  • 35,812
  • 14
  • 73
  • 140
  • Sorry, I misread the code and thought `roletype` was declared in the constructor as well. I have since edited my answer and made that clear. – tskuzzy Aug 01 '11 at 19:35