Search code examples
javaswinginner-classesanonymous-classouter-classes

How to change variable value throughout outer class when it changed in an inner one?


I have a swing class that includes a String variable str3 declared as final and two

ActionListener interfaces that implemented by two JButtons b1

and b2 , when b1 JButton is pressed str3 String takes a value ,

My question here how to make str3 value to be changed throughout the class

rather in the second ActionListener interface (not in the first inner class only ) .

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

public class mySwing extends JFrame {

    JButton b1, b2;

    public mySwing() {
        final String str3;
        JPanel panel = new JPanel();
        b1 = new JButton("please click me first");
        b2 = new JButton("please click me second");
        final JTextField txt = new JTextField("                            ");
        panel.add(txt);
        Container pane = getContentPane();
        panel.add(b1);
        panel.add(b2);
        pane.add(panel);
        str3 = new String();
        b1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent acv) {
                String input = "HelloWorld";
                String str3 = new String(input.substring(0, 5));
                txt.setText(str3);
            }
        });
        b2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent acv) {
                txt.setText(str3);
            }
        });
        setVisible(true);
    }

    public static void main(String[] args) {
        new mySwing();
    }
}

Solution

  • Just make str3 a non-final instance variable of your outer class mySwing.

    By the way, do not do things like new String(input.substring(0, 5)) the result of input.substring(0, 5) is a String so you don`t need to create another String.

    Based on your code:

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.util.*;
    
    public class mySwing extends JFrame {
    
        JButton b1, b2;
        String str3="";
    
        public mySwing() {
            JPanel panel = new JPanel();
            b1 = new JButton("please click me first");
            b2 = new JButton("please click me second");
            final JTextField txt = new JTextField("                            ");
            panel.add(txt);
            Container pane = getContentPane();
            panel.add(b1);
            panel.add(b2);
            pane.add(panel);
            str3 = new String();
            b1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent acv) {
                    str3+=" (1)";
                    txt.setText(str3);
                }
            });
            b2.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent acv) {
                  str3+=" (2)";
                  txt.setText(str3);
                }
            });
            setVisible(true);
        }
    
        public static void main(String[] args) {
            new mySwing();
        }
    }