I have a java program that has so many buttons with a text on it , whenever a user press the button the text inside a button should be added in an empty string ,
example,
String store = “”;
if a user press ‘A’ then then the letter ‘A’ stored in the String variable ‘store’ if then a user press ‘B’
then store must be equals to ‘AB’ , but this is not happening in my case , what happening is the previous String is simply replaced by the new one :S,
Here is the code,
String Text = "";
for ( int count = 0; count < names.length; count++ ){
if (event.getSource()==buttons[count]){
Text += buttons[count].getText();
JOptionPane.showMessageDialog(null, Text);
}
}
What am i doing wrong ? why the string is not adding new values ? why it replacing the previous ones ? :S
Is
Texta local variable inside your action listener? In that case it will always be re-initialized. Make it a member of your class.