Search code examples
javaswingloopsjlabelsettext

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.


Solution

  • 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...