Updating a JLabel with an integer array

In summary: Thanks for the help!In summary, you need to convert a random number to a string, peel of the digits one by one and put each one in the appropriate JLabel.
  • #1
ProPatto16
326
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
  • #2
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?
 
  • #3
Use the JLabel.setText method perhaps?
 
  • #4
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 (:
 
  • #5
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);
...
 

Related to Updating a JLabel with an integer array

1. How do I update a JLabel with an integer array?

To update a JLabel with an integer array, you can use the setText() method and pass in the array converted to a string using the Arrays.toString() method. For example:
int[] numbers = {1, 2, 3};
label.setText(Arrays.toString(numbers));

2. Can I update a JLabel with a specific element from the array?

Yes, you can update a JLabel with a specific element from the array by accessing it using its index and passing it as the argument for the setText() method. For example:
int[] numbers = {1, 2, 3};
label.setText(Integer.toString(numbers[0]));

3. Can I update a JLabel with an integer array in a loop?

Yes, you can update a JLabel with an integer array in a loop by using the same setText() method inside the loop and passing in the current element of the array each time. For example:
int[] numbers = {1, 2, 3};
for(int i=0; i    label.setText(Integer.toString(numbers[i]));
}

4. How can I format the JLabel to display the integer array in a specific way?

You can format the JLabel to display the integer array in a specific way by using the String.format() method and specifying the desired format using format specifiers. For example:
int[] numbers = {1, 2, 3};
String formattedString = String.format("The numbers are: %d, %d, %d", numbers[0], numbers[1], numbers[2]);
label.setText(formattedString);

5. Is it possible to update a JLabel with an integer array from a different class?

Yes, it is possible to update a JLabel with an integer array from a different class. You can pass the JLabel as a parameter to the other class's method and use it to update the label with the array. Alternatively, you can also use a getter method to access the label from the other class and update it directly.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
11
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
12K
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
Back
Top