Home > database >  How can I use my object insiden a button?
How can I use my object insiden a button?

Time:11-23

I have an issue. I want to use my object which is outside a button, and I get an error message: Local variable es defined in an enclosing scope must be final or effectively final

    File inputfile = new File("./input.txt");
    testobject to = null;
        try 
        {
            Scanner inputscan = new Scanner(inputfile);
            String text1 = inputscan.nextLine();
            String text2 = inputscan.nextLine();
            to = new testobject(text1,text2);
            
            
            
            inputscan.close();
        }
        catch (Exception e) 
        {
            System.err.println(e);
        }



        JButton btnSave = new JButton("Save");
        btnSave.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                
                
                to.setSomething("test");
                
                
                
            
                
            }
        });

Obviusly I have a class with a constructor etc... but I have problem that I cant reach my object inside the button.

CodePudding user response:

Variables that you want to use inside anonymous classes, like your ActionListener need to be final. You are initializing the variable with null and change it to the object created from the text input later.

You could instead create the object using a no args constructor and use setters to set the value in your testobject.

// Class names should start with a uppercase
testobject to = new testobject();
// Use try-with-resources to close them automatically.
try(Scanner inputscan = new Scanner(inputfile)){
    to.setText1(inputscan.nextLine());
    to.setText2(inputs can.neytLine());
 } catch (Exception e) {
    System.err.println(e)
 }
JButton btnSave = new JButton("Save");
btnSave.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        to.setSomething("test");
    }
});

Or initialize the variable inside your actionPerfromed method. The downside is that you can't access the testobject outside of the method.

btnSave.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent e) {
         try(Scanner inputscan = new Scanner(inputfile)){
             testobject to = new testobject(inputscan.nextLine(), inputscan.nextLine());
             to.setSomething("test");
         } catch (Exception e) {
            System.err.println(e)
         }
     }
});

CodePudding user response:

I tried the first method, maybe I did something worng but it didn't work. The second method is working, but this create a new object in the button, so if I want to use outside a button, I need to set everything again. And in my program it is very complicated etc...

So the solution I created an Arraylist and I can use this inside and outside the button too.

ArrayList<testobject> list = new ArrayList<testobject>();

And after this I set the first (0) element to my data

list.add(new eselyek(text1,text2));

And after this I can easily use easily the methods of the class

lista.get(0).setSomething("test");
  • Related