Reputation: 139
I'm back again with a simpler question! I'd like the content of this JLabel (triedLettersLA) to update periodically throughout the application (I have that part handled).
But I'd like to ADD text to the label. NOT rewrite it entirely. For example.. If the text said "Letters Tried: ", I'd want to add "N", and "X", and then "H" on three separate occasions. So at the end, it'd look like this "Letters Tried: N X H". Here's what I have, and it's totally not working..
This is way up top,
JLabel triedLettersLA = new JLabel("Tried letters:");
public boolean used[] = new boolean[26];
And this is lower down in my code..
StringBuffer missedLetter = new StringBuffer();
for (int le = 0; le <= 25; le++) {
if (used[le]) missedLetter.append((char)(le + 'a'));
String triedLettersLA.getText(t);
triedLettersLA.setText(t + " " + missedLetter.toString());
}
Upvotes: 2
Views: 133
Reputation: 725
Nonsence code:
String triedLettersLA.getText(t);
Change it to:
String t = triedLettersLA.getText();
Upvotes: 0
Reputation: 76898
The code you posted makes no sense (nor could it ever compile). Well, it would compile now, possibly.
That being said, a String
in Java is immutable; you can't change it. To change the text of a JLabel
you need to create a new String
and call the JLabel
's setText()
method.
String old = triedLettersLA.getText();
String newString = old + " N"; // this creates a new String object
triedLettersLA.setText(newString);
Upvotes: 4