Anas Altarazi
Anas Altarazi

Reputation: 117

how can i change variable value in initialize from jbutton event?

private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    scrollPane = new JScrollPane();
    frame.getContentPane().add(scrollPane, BorderLayout.WEST);
    JButton btnNewButton = new JButton("New button");
    frame.getContentPane().add(tes, BorderLayout.NORTH);
    int n = 6;
    btnNewButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent arg0) {
    //here iwant to change n value
          }

    }  
        });
}

i want to change a variable value in initialize from more than one button (if i click any button this variable will change)

Upvotes: 0

Views: 103

Answers (1)

user2543740
user2543740

Reputation: 179

Put n as global, so it will be never deallocated. Then inside actionPerformed you can process n as you wish.

In your example n is local to initialize and so it will be lost after the function terminate.

Upvotes: 1

Related Questions