2

I am working on a little game, a Java project for IT-classes.

To add/declare(???) Java swing elements I've used this type of writing:

JLabel A = new JLabel();
JLabel B = new JLabel();
//More JLabels...


JButton A = new JButton();
JButton B = new JButton();
//More JButtons...

That the code does not become longer and (more) confusing, I continued with this type of writing:

JLabel A = new JLabel(), B = new JLabel()/*More JLabels...*/;

JButton A = new JButton(), B = new JButton()/*More JButton...*/;

/*Generaly more comments everywhere over the code for my teacher (and me)
 *and more for a better overview.
 */

My question is:

Is there a shorter way to add/declare(???) multiple Java swing elements at once?

//like this
JLabel A, B, C, D, E, F = new JLabel();
//or
new JLabel[A, B, C, D, E, F];//PLS don't ask what I'm doing in this line xD

or may is there already a semilar question in Stackoverflow that I've not found?

Edit

This question may already have an answer here: Initializing multiple variables to the same value in Java 6 answers

Here the Link to the question

Your question has been identified as a possible duplicate of another question. If the answers there do not address your problem, please edit to explain in detail the parts of your question that are unique.

Not worked with Jbuttons and JLabels.

Community
  • 1
  • 1
Jayrel
  • 31
  • 6
  • Be careful of the downvoters, They love this kind of questions ! – Yahya May 15 '17 at 20:34
  • 1
    However, if you don't care about the **Name** of the JLabel object, you can create an `ArrayList` and loop through it, in every loop you add and initialize anonymous label `new JLabel()` – Yahya May 15 '17 at 20:36
  • http://stackoverflow.com/questions/6202818/initializing-multiple-variables-to-the-same-value-in-java This question should help you; and at the same time explain why you shouldn't ;) It can be done shorter, but all your fields would point to exactly the same object. – Malte Hartwig May 15 '17 at 20:37
  • 1
    You are asking for people to weigh in with opinions on style. That is _two_ reasons why your question might attract some negative attention. The charter for this forum mostly restricts it to questions along the lines of, "Why doesn't this trick work...?, or what does this error message mean...?" You might have better luck on https://softwareengineering.stackexchange.com/ where they are more open to talk about design patterns, and "best practices" and, etc. – Solomon Slow May 15 '17 at 20:40

4 Answers4

5

If you are using Java 8 you can use :

List<String> labels = ....;
Stream<Button> stream = labels.stream().map(Button::new);
List<Button> buttons = stream.collect(Collectors.toList());

From the book Java se 8 for the really impatient


Then you can use :

JPanel p = new JPanel();
buttons.forEach((t) -> p.add(t));//add your buttons to your panel
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
2

It depends, if all your objects are JLabel or the same object type, you could try:

  • An array of JLabel like:

    JLabel[] labels = new JLabel[size of your array];
    

    Then access it after inside a for loop:

    for (int i = 0; i < labels.length; i++) {
        labels[i] = new JLabel("I'm label: " + i);
    }
    
  • A List of labels:

    ArrayList <JLabel> labelsList = new ArrayList <JLabel>();
    

    Then you could:

    for (int i = 0; i < 10; i++) { //I take 10 as an arbitrary number just to do it in a loop, it could be inside a loop or not
        labelsList.add(new JLabel("I'm label-list: " + i));
    }
    

And later you could add them like:

pane.add(labels[i]); //Array
pane.add(labelsList.get(i)); //List

The above code should be inside a loop or change i for the explicit index of the element to be added.

Frakcool
  • 10,915
  • 9
  • 50
  • 89
1

If you want to do the same thing (or a similar thing) many times in a program, the answer is to use a loop of some kind. Here, you could declare an array (or List) of JButton elements, and loop over it to initialize its elements:

final int NUM_BUTTONS = 6;
JButton[] buttons = new JButton[NUM_BUTTONS];
for (int i = 0; i < NUM_BUTTONS; i++) {
  buttons[i] = new JButton();
}

// refer to A as buttons[0], C as buttons[2], etc
amalloy
  • 89,153
  • 8
  • 140
  • 205
-3

You can make variables equal to each other after declaring them.

String one, two, three;
one = two = three = "";

So, I think you could do

JLabel A,B,C,D,E,F;
A = B = C = D = E = F = new JLabel();
Raheel138
  • 147
  • 10
  • 1
    Beware that all those variables point at the exact same JLabel, which is surely not what is intended. – Malte Hartwig May 15 '17 at 20:38
  • @Yahya You can, but it isn't very useful for normal objects. For primitive types it is completely valid, though – Malte Hartwig May 15 '17 at 20:39
  • that doesn't make sense, what is the purpose of the one, two, three and the JLabel initialization points to the same object. – Eddie Martinez May 15 '17 at 20:40
  • @Yahya It would help if you told us what exactly did not work. I took those two lines, put them into the right places, and it worked completely fine, only problem being that all six point at the same JLabel(). – Malte Hartwig May 15 '17 at 20:46
  • @MalteHartwig I think that is the problem with my answer. Since they all point to one another, any changes to their properties would affect all of them. – Raheel138 May 15 '17 at 20:48
  • Yes, it becomes very clear when you put parantheses around each assignment: `(A = (B = (C = (D = (E = (F = new JLabel()))))));` Assign label to F, assignment returns label, assign to E, returns label, assign... Still kind of valuable as an explanation why it wouldn't work. – Malte Hartwig May 15 '17 at 20:51