How do I display the numbers in a for loop in a jLabel?

    int intStartingNumber = Integer.parseInt(this.txtInputStartingNumber.getText());
    int intEndingNumber = Integer.parseInt(this.txtInputEndingNumber.getText());

    for (int x = intStartingNumber; x <= intEndingNumber; x = x + 1) {
        System.out.print(x + " ");
        this.lblOutputNumbers.setText(x + " ");
    }

Hi this is my code how come it won't set my jLabel as all the numbers in the for loop but when i print it out, all the numbers show up. In the jLabel only whatever is inputted as the EndingNumber is printed out in the jLabel.

Upvotes: 1

Views: 1337

Answers (1)

ThisClark
ThisClark

Reputation: 14823

RE

In the jLabel only whatever is inputted as the EndingNumber is printed out in the jLabel.

for (int x = intStartingNumber; x <= intEndingNumber; x++) {
    System.out.print(x + " ");
    this.lblOutputNumbers.setText(lblOutputNumbers.getText() + " " + x);
}

The trick here is JLabel does not have an appendText function, but you can call the JLabel.getText() function inside of its setText function to append text to the label.

This answer stems from Append text in JLabel

Upvotes: 2

Related Questions