4

display array values ​​in a jlabel

 2 years ago
source link: https://www.codesd.com/item/display-array-values-in-a-jlabel.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

display array values ​​in a jlabel

advertisements

I would like display the values of my String array in one Jlabel.

I've tried with a loop (for) but, the result is the new letter overwrites the previous. I do not understand how I could displaying the letters following.

labelWord is the variableName of my Jlabel.

String myArray[] = new String[4];
myArray[0] = "h";
myArray[1] = "e";
myArray[2] = "l";
myArray[3] = "l";
myArray[4] = "o";

and my loop :

for (int j = 0; j <= myArray.length; j++) {
    labelWord.setText(myArray[j]);
}


Your code does not work because you set the label value with the 1st String, then the second one, then the 3rd one... You need to concatene all your String into one then set the label text with this value:

String value ="";
for (int j = 0; j <= myArray.length; j++) {
                value += myArray[j];}
labelWord.setText( value );

Note: you can also use a StringBuilder instead of concatenating directly to the String.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK