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();
}
}