Skip to content
Advertisement

Cant add to arraylist inside actionevent Buttonclicked

Im kind of new to programming in java but here im trying to figure out how to add the value of a button clicked in ActionEvent. I even changed the actionlistener to MouseListener etc but still not working. It only displays the current button clicked but i would like to add every button clicked into an arraylist.

Problem: I cant add the value of buttons clicked into an arraylist.

Example: if i click on the button named E then i want E to be added to arraylist, after that if i click on the button S then it should add S o the same arraylist. but its not adding aything to the arraylist. it only displays the current button clicked

JavaScript

Advertisement

Answer

I think you are confused about the content of the JLabel displayed on the GUI and your ArrayList. The issue is actually in the former.

Your current implementation replaces the text of the JLabel instead of appending it for each pressed button.

So, in your actionPerformed() method, change this line

JavaScript

to

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement