Snay
Snay

Reputation: 15

Java update jLabel.setText via for loop

I basically checked out a book from the Library and started learning Java. I'm trying to code a little score calculator for my golf league and this site has been a lof of help! So thanks for even being here!

Now to the question:

I have a 9 labels, created with NetBeans GUI, with names like jLabel_Hole1, jLabel_Hole2, ...

If a user selects the radio option to play the front nine those labels have number 1 - 9 and if they change it to the "Back Nine" then they should display 10 - 18. I can manually set each label to the new value on a selection change but I wanted to know if there was a more elegant way and if so if one of you could be kind enough to explain how it works.

Here is the code that I want to try and truncate:

       TGL.jLbl_Hole1.setText("10");
       TGL.jLbl_Hole2.setText("11");
       TGL.jLbl_Hole3.setText("12");
       TGL.jLbl_Hole4.setText("13");
       TGL.jLbl_Hole5.setText("14");
       TGL.jLbl_Hole6.setText("15");
       TGL.jLbl_Hole7.setText("16");
       TGL.jLbl_Hole8.setText("17");
       TGL.jLbl_Hole9.setText("18");

I've read some things about String being immutable and maybe it's just a limitation but I would think there has to be way and I just can't imagine it.

Thanks.

Upvotes: 1

Views: 1262

Answers (2)

Eric Jablow
Eric Jablow

Reputation: 7899

I've never found a Java GUI-generator to provide code that's any good. I may be wrong--there may be a good one, but I always prefer to position and name them myself. So,

/**
 * The JLabels for the holes on the golf course.
 * <p>
 * holeLabels[0][i] are for the outward holes, 1-9.
 * holeLabels[1][i] are for the inward holes, 10-18.
 */
private JLabel[][] holeLabels;
/**
 * The starts of the outward and inward ranges of holes.
 */
private static final int[] holeStart = {1, 10};

// Later
holeLabels = new JLabel[2][9];
for(final int i = 0; i < holeLabels.length; i++) {
    for (final int j = 0; j < holeLabels[i].length; j++) {
        holeLabel[i][j] = new JLabel();
        holeLabel[i][j].setText(Integer.toString(holeStart[i] + j));
    }
}

Interestingly, holeLabels.length is 2. holeLabels is an array of 2 arrays of 9 ints. i goes from 0 to 1, and j goes from 0 to 8, so the text computation works. The reason I did things this way is so you can easily place the labels in an appropriate GridLayout later.

Upvotes: 1

MadProgrammer
MadProgrammer

Reputation: 347204

Basically, rather then creating a individual label for each hole, you should create an array of labels, where each element in the array represents a individual hole.

So instead of...

TGL.jLbl_Hole1.setText("10");
TGL.jLbl_Hole2.setText("11");
TGL.jLbl_Hole3.setText("12");
TGL.jLbl_Hole4.setText("13");
TGL.jLbl_Hole5.setText("14");
TGL.jLbl_Hole6.setText("15");
TGL.jLbl_Hole7.setText("16");
TGL.jLbl_Hole8.setText("17");
TGL.jLbl_Hole9.setText("18");

You would have...

for (JLabel label : TGL.holeLables) {
    lable.setText(...);
}

A better solution would be to hide the labels from the developer and simply provide a setter...

TGL.setHoleText(hole, text); // hole is int and text is String

Internally to your TGL class, you have two choices...

If you've used the form editor in Netbeans, you're going to have to place the components that Netbeans creates into your own array...

private JLabel[] holes;
//...//
// Some where after initComponents is called...
holes = new JLabel[9];
holes[0] = jLbl_Hole1;
// There other 8 holes...

Then you would simply provide a setter and getter methods that can update or return the value...

public void setHole(int hole, String text) {
    if (hole >= 0 && hole < holes.length) {
        holes[hole].setText(text);
    }
}

public String getHole() {
    String text = null;
    if (hole >= 0 && hole < holes.length) {
        text = holes[hole].getText();
    }
    return text;
}

Take a closer look at the Arrays tutorial for more details...

Upvotes: 2

Related Questions