JLabel problem

Hi,
How can I display strings that are stored in an array onto a JLabel?
For example, String[] tempfeatures has the following contents: "Digital","Stopwatch","Timer","Alarm","Lap Timer","Waterproof","Date"

Here is my code(it's not right);
[code]
tempfeatures=w.getFeatures();
feat=tempfeatures;
for (int x=0; x<feat.getLength();x++) {
feature = new JLabel(feat[x]);
}
[/code]

Comments

  • : Hi,
    : How can I display strings that are stored in an array onto a JLabel?
    : For example, String[] tempfeatures has the following contents: "Digital","Stopwatch","Timer","Alarm","Lap Timer","Waterproof","Date"
    :
    : Here is my code(it's not right);
    : [code]
    : tempfeatures=w.getFeatures();
    : feat=tempfeatures;
    : for (int x=0; x<feat.getLength();x++) {
    : feature = new JLabel(feat[x]);
    : }
    : [/code]
    :
    Sure, all this code needs is the add() statement to add the labels to the GUI. Beware though it might produce an infinitely large number of labels, if the user keeps doing something which runs this code. It would be better to create an array of labels and setting their text variables instead of creating the each time anew:
    [code]
    // Initialization
    for (int i = 0; i < feat.getLength(); i++ {
    labels[i] = new JLabel("-");
    this.add(labels[i]);
    }

    // Your code:

    tempfeatures=w.getFeatures();
    feat=tempfeatures;
    for (int x=0; x<feat.getLength();x++) {
    labels[x].setText(feat[x]);
    }
    [/code]
  • Hi zibadian!

    Thanx again for your help! U solved the JLabel problem :)
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories