0

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();
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
PHPFan
  • 756
  • 3
  • 12
  • 46

3 Answers3

1

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();
    }
}
Holger
  • 285,553
  • 42
  • 434
  • 765
  • No it must be final to be accessed from an inner class – PHPFan Aug 29 '13 at 11:58
  • 1
    It has to be final only if it is a local variable. When creating an instance variable it can be accessed by inner classes without being final. Just try it out. Or look at your buttons `b1` and `b2`. They can be accessed by your inner classes without being final. – Holger Aug 29 '13 at 12:00
  • please more explanation I don't understand your idea – PHPFan Aug 29 '13 at 12:26
0

Your approach of declaring the variable str3 as final is correct. However, in Java Strings cannot change their contents, so you would have to do something different. Some ideas I can come across:

  1. Use a StringBuffer instead, and convert it to a String with the toString() method where needed.
  2. Use some other POJO with a getter and setter for your String, this way the POJO will be declared as final and the content can be changed via the getter and setter.

Hope this helps as a guideline, there sure are many other good approaches.

Martin
  • 3,018
  • 1
  • 26
  • 45
  • Thank you. the matter here is not Strings Members but rather changing variable values , So I will try the second option – PHPFan Aug 29 '13 at 12:04
0

your variable str3 is a final, then it can never be changed.

inside your code you've never tried to change it, you just declared a new str3 inside your ActionListener inner class.

Wagdi Kala
  • 63
  • 5
  • Look below b2.addActionListener(new ActionListener() , Why str3 value is null( I want to make it as the value in the previous inner class) – PHPFan Aug 29 '13 at 11:57
  • final String str3; is your "final" declaration that's mean str3 will never change during the run of the program, after that you've initialized your str3 (str3 = new String() <-- new String() return null), and str3 will be null for ever. now inside b1, you've declared another str3 (String str3 = new String(input.substring(0, 5));) and it's not the same str3 above, and if you try to do str3 = "someString"; the compiler will complain. Now str3 inside b2 is pointing/referring to the first str3 with the null value, because it can't see the str3 inside b1 and it's null.. – Wagdi Kala Aug 29 '13 at 15:47