-1

For example

JLabel label0 = new JLabel("");
JLabel label1 = new JLabel("");
JLabel label2 = new JLabel("");
JLabel label3 = new JLabel("");
JLabel label4 = new JLabel("");

for (int i = 0;i<5;i++)
{
    labeli.setText(i);
    
}

In this code, the for automatically change the label texts, first the label0 and after then the label1 etc.. Obviusly, this isn't working. My best solution was I do an if, and if i == 0 set label0 etc.. but maybe there is an easier solution which I don't know. (And if I make 100 elements, it is very long time to write down)

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
copy14
  • 3
  • 3

2 Answers2

0

At the simplest of levels, you have a number of objects you want to maintain in an easy/accessible container. Assuming for a second that the list is of a predefined or, after it's initialisation, will never change size without needing to be be re-initialised, an array would be a good choice.

See Arrays for more details.

For example...

import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JLabel[] labels = new JLabel[6];

        public TestPane() {
            setLayout(new GridLayout(0, 1));
            for (int index = 0; index < labels.length; index++) {
                JLabel label = new JLabel(Integer.toString(index));
                // Customise the label, as it' easy to type `label`
                // then `labels[index]` ;)
                add(label);
                labels[index] = label;
            }
        }

    }

}

If, on the other hand, you need something which is more dynamic (can grow and shrink at runtime), then a List of some kind would be more practice, but since you don't seem to understand arrays, maybe you should stick with those first.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
-2
  1. Create your own class and extend the JLabel class

  2. After the initiation of the constructor, add the class into a holder (that holds your custom class)

    class MyJLabel extends JLabel
    {
        public MyJLabel(String name)
        {
            super(name);
            holder.add(this);
        }
    }
    

Your holder should look like

List<MyJLabel> holder = new ArrayList<>();

and then, you can change the text of all your jlabels by using lambda expression:

holder.forEach(jlabel -> jlabel.setText(""));

Note: your variables shouldn't be JLabel but MyJLabel.

Melron
  • 569
  • 4
  • 10
  • (1-) no need to create a custom class. The JLabel class should NOT reference an external variable. – camickr Sep 22 '21 at 19:44
  • @camickr why it shouldn't? – Melron Sep 22 '21 at 19:49
  • I gave you the reason. A class should be self contained. If should NOT reference an external variable. You should be able to use the custom `MyJLabel` class in any application that current uses a `JLabel`. However you can't because it won't compile because you are referencing an external variable. – camickr Sep 22 '21 at 19:57
  • @camickr Your words does not make any sense here, in this example. There isn't any specification that saying that thing. The question was clearly about how X jlabels can change their texts with 'another' way. In the other hand, in order to follow your words, i have to take care about the type of the holder too. Should i use CopyOnWriteArrayList too for thread safety reasons? – Melron Sep 22 '21 at 20:13
  • You've generated a side effect, by having the label itself to some `ArrayList` which, by this workflow should need to be `static` in order to be used. Not only is it an anti-pattern/smell, it's just generally a bad design and wholly unnecessary. Why not just add the `JLabel` instances you've created an `ArrayList`- `holder.add(new JLabel())`, in fact, you could combine the creation and update within the loop. The whole suggestion of a "custom class" in this context just smells give all the other possibilities – MadProgrammer Sep 22 '21 at 22:09
  • The "holder" has nothing to do with the functionality of the JLabel. It is simply used to hold multiple JLabel components so you can invoke the setText() method on each of these labels in some method. If you don't understand that basic concept then there is not much more I can say. – camickr Sep 22 '21 at 22:47