Updating a JLabel with an integer array

  • Thread starter Thread starter ProPatto16
  • Start date Start date
  • Tags Tags
    Array Integer
Click For Summary

Discussion Overview

The discussion revolves around updating a JLabel array in Java with digits from a randomly generated number. Participants explore methods for efficiently setting the text and properties of multiple JLabels based on an integer array of digits.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant describes their setup with a JLabel array and a JPanel, indicating that they need to update the JLabels with digits from an integer array.
  • Another participant suggests converting the random number to a string and extracting digits to update the JLabels.
  • A third participant proposes using the JLabel.setText method to update the labels.
  • A later reply mentions the challenge of applying the setText method individually to each JLabel, leading to repetitive code for setting properties like color and font size.
  • Another participant recommends encapsulating the property-setting code in a loop or method to reduce repetition and improve code organization.

Areas of Agreement / Disagreement

Participants generally agree on the need to update JLabels with digits from an integer array, but there is no consensus on the most efficient method to do so, with various approaches being discussed.

Contextual Notes

Participants express concerns about repetitive code when setting properties for each JLabel and explore potential solutions without resolving the best approach.

ProPatto16
Messages
323
Reaction score
0
halfway through my code so its a bit of mess but this is simple what i need to do.

I have a JLabel array of 6 labels.

a random number is generated, its then split into its digits into a 6 element array of integers. now i need to update the Jlabels with the integer array of digits.

my JLabel declaration is

odometer = new JLabel[ 6 ]; // create array for odometer digits
odometerJPanel = new JPanel();
for ( int count = 0; count < odometer.length; count ++)
{
odometer[ count ] = new JLabel( " 0 " );
odometerJPanel.add( odometer[ count ] ); // add odometer to JPanel
}

so initially the labels are all 0.
i have another array filled with the digits i want to put onto the JLabels.
How?

Thanks in advance
 
Physics news on Phys.org
ProPatto16 said:
halfway through my code so its a bit of mess but this is simple what i need to do.

I have a JLabel array of 6 labels.

a random number is generated, its then split into its digits into a 6 element array of integers. now i need to update the Jlabels with the integer array of digits.

my JLabel declaration is

odometer = new JLabel[ 6 ]; // create array for odometer digits
odometerJPanel = new JPanel();
for ( int count = 0; count < odometer.length; count ++)
{
odometer[ count ] = new JLabel( " 0 " );
odometerJPanel.add( odometer[ count ] ); // add odometer to JPanel
}

so initially the labels are all 0.
i have another array filled with the digits i want to put onto the JLabels.
How?

Thanks in advance

Convert the random number to a string, then peel of the digits one by one and put each one in the appropriate JLabel. Am I correct in assuming you are writing Java code?
 
Use the JLabel.setText method perhaps?
 
That's what I ended up doing. But to use the set text method it needs to be applied individually to each index of the array. So I have like 30 lines of code to set the number, colour and font size. Just means its a lot of repetitive code. I was looking for a way to do it using the array directly but seems most methods can't be applied to arrays, only Ints and strings. And yeah writing java. Thanks guys (:
 
Sound like you could benefit from moving the code that sets the properties of each JLabel into a loop (if the "source" data is also stored in arrays) or at least method, like the setMyLabel example method below.

Code:
public void setMyLabel(JLabel label, String text, Color color, float pt) {
  label.setText(text);
  label.setForeground(color);
  label.setFont(label.getFont().deriveFont(pt));
}


JLabel[] labels = ...
...
setMyLabel(labels[0], "first", Color.RED, 12);
setMyLabel(labels[1], "second", Color.GREEN, 10);
...
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 8 ·
Replies
8
Views
7K
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
13K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K