1

I have had an issue with making multiple variables, such as buttons, and having to define, access or modify them each separately. I have searched many times on how to access multiple variables without wasting so much space. As you'd expect, I probably am just wording this wrong.

Example for what I am referring to:

button1.setBounds(25 + insets.left, 50 + insets.top,
                  size.width + 75, size.height + 75);
button2.setBounds(172 + insets.left, 50 + insets.top,
                  size.width + 75, size.height + 75);
button3.setBounds(319 + insets.left, 50 + insets.top,
                  size.width + 75, size.height + 75);
button4.setBounds(25 + insets.left, 172 + insets.top,
                  size.width + 75, size.height + 75);
button5.setBounds(172 + insets.left, 172 + insets.top,
                  size.width + 75, size.height + 75);
button6.setBounds(319 + insets.left, 172 + insets.top,
                  size.width + 75, size.height + 75);
button7.setBounds(25 + insets.left, 294 + insets.top,
                  size.width + 75, size.height + 75);
button8.setBounds(172 + insets.left, 294 + insets.top,
                  size.width + 75, size.height + 75);
button9.setBounds(319 + insets.left, 294 + insets.top,
                  size.width + 75, size.height + 75);

I was thinking of a for loop, leaving the int, x, to follow the variable, but it wouldn't be referenced properly.

* EDIT ** I'm not specifically referencing to the layout of the buttons... I am referring on how to modify multiple variables at once, i.e. Button1-9 without having to individually reference them.

            button1.setToolTipText("Click here to check for the missing Flight 307");
            button2.setToolTipText("Click here to check for the missing Flight 307");
            button3.setToolTipText("Click here to check for the missing Flight 307");
            button4.setToolTipText("Click here to check for the missing Flight 307");
            button5.setToolTipText("Click here to check for the missing Flight 307");
            button6.setToolTipText("Is it here?");
            button7.setToolTipText("Is it here?");
            button8.setToolTipText("Is it here?");
            button9.setToolTipText("Is it here?");

How would I modify button1-button5, and separately, button6-button9? I hope this helps

Munkeeface
  • 411
  • 3
  • 11
  • Well by the looks of it your numbers seem to jump the same amount of each time. YOu could easily turn them into a loop and just update them based on what they need. The first number 147 and the second by 122.It just depends on how you created the button1-9 variables. – Asthor Mar 25 '14 at 18:45
  • I'm quite not sure if I understand this correctly but isn't the layout containing the buttons not the better way to adjust the position? – LostKatana Mar 25 '14 at 18:54
  • @Asthor, As for loops go, how would I be able to express it as such? I thought if I just used (x as the variable), buttonx would reference as a separate, uninstantiated variable in itself? – Munkeeface Mar 26 '14 at 13:58
  • @LostKatana, I was considering using a basic gridLayout or gridBagLayout, but I am also referencing to if I want to define multiple variables with separations in their name by the single numbers (button1 and button3) and be able to define them, or modify them collectively without having to mirror the same line over and over, wasting space. What I'm asking isn't just positioning of the buttons, but anything. Maybe setting toolTipText, or other basic attributes. Thank you, and if I need to clarify more, I can provide other pressing examples – Munkeeface Mar 26 '14 at 14:01
  • The only idea I have for this is to hold your buttons in lists which correlates to the action you want to perform. Like you have two list where one is for updating button1 to button5 and one for updating button6 to button9. But that is just a workaround in my opinion not a good solution as your number of lists may explode. – LostKatana Mar 26 '14 at 14:15
  • @LostKatana, So establishing an ArrayList, and add which variables I want to edit within the array, and if I want to edit less within the array, remove those unedited from the list? (a temporary list) – Munkeeface Mar 26 '14 at 14:19
  • 1
    I mean that you add the buttons into lists according to the purpose the list has. Like you want to set borders to the first column of a 3x3 ordered button field you can have one list named e.g. `firstColumnButtons` and then perform the action you want to that list like setting the tool tip to "Column ones Buttons". Hope you get it. – LostKatana Mar 26 '14 at 14:25
  • @Munkeeface Well I assumed you were having issues setbounds and not the actual buttons but your edit cleared that up. As I said though it is just a question on how you create the buttons. Easiest way to iterate over them would be if they are stored in an array but you can also use more complex tools for storing them. You can then loop over that array easily to get to each button. – Asthor Mar 26 '14 at 15:43
  • @Asthor Even using the arrays to store the buttons, editing their positions, as they would be in a grid, would using a `gridBagLayout` be a formidable method? E.g. (given the array containing the buttons, named buttons) `for(int i = 0; i < buttons[].length; i++)`, `pane.add(buttons[i], gridBagLayout);`--- Would this work? – Munkeeface Mar 26 '14 at 16:48
  • @Munkeeface It should work yes. It would equal just doing pane.add(buttons1, gridBagLayout); pane.add(buttons2, gridBagLayout); and so on. – Asthor Mar 26 '14 at 16:59

2 Answers2

2

Hi I think you could put all this buttons in an array and then go through your array like you want considering the index of your array you can do what you want to the different objects ;)

Something like this :

    JButton buttons[] = {new JButton(), new JButton(), new JButton(),new JButton(), new JButton(), new JButton(),new JButton(), new JButton(), new JButton()};
    for(int i = 0; i < buttons.length; i++)
    {
        if( i < 5 )
            buttons[i].setToolTipText("Click here to check for the missing Flight 307");
        else
            buttons[i].setToolTipText("Is it here?");
    }

You just have to put your own JButton in the tab.

Clad Clad
  • 2,653
  • 1
  • 20
  • 32
  • That's a fantastic idea. So, if I would like to modify a specific button, I would access it through buttons[x]? And the layout of the buttons, each being changed through a separate x and y coordinate, would it be just another for loop? i.e. for int x, and another for int y and change each accordingly? – Munkeeface Mar 26 '14 at 15:17
  • yes you can access all your button by buttons[index] ;) For the layout I don't really know what you want to do, I let you try it ;) – Clad Clad Mar 26 '14 at 15:26
  • 1
    @Munkeeface Based on the setup you have in the question you don't need a for loop in the for loop. YOu just need to update your x and y as need them inside the loop that loops over the buttons. For example `for(int i = 0; i < buttons.length; i++, x++)` or `for(int i = 0; i < buttons.length; i++) { buttonstuff; x++}` – Asthor Mar 26 '14 at 15:48
  • @Asthor, as `x++` and `i++`, I assume the `x` would be a previously established variable (`int x = 0`), as the for loop establishes `i`. With that being said, if `x` and `i` are both solely increased by 1, how would that affect the coordinates? Such as in the first example `button1.setBounds...` – Munkeeface Mar 26 '14 at 16:28
  • @Munkeeface yeah it would. I just took a x++ as an example. Based on your in the question x could be 25 at the start and y 50 and then `x += 147` and `y += 122` when `i % 3 = 0`. When `i % 3 = 0` you also reset x to the initial value 25. (i being the counter you loop over the buttons with) – Asthor Mar 26 '14 at 16:36
  • @Asthor, Ok, that's what was slipping my mind. So in your explanation, every time i is divisble by 3 (within an if statement), set `x = 25` and `y = 50` again? Thank you so much for all your help! You have helped me immensely! – Munkeeface Mar 26 '14 at 16:54
  • @Munkeeface You would only reset the x but you update the y by doing y+=122. y should stay the same until every third iteration where it gets updated. – Asthor Mar 26 '14 at 16:58
  • @CladClad, thank you! You've helped bring a new view to the table for me. Thinking outside the box. I've decided to use `GridBagLayout`, or just remain with the current layout, but with a for loop from the array. – Munkeeface Mar 26 '14 at 16:59
  • @Asthor, Ok, now I see it. Kind of new to Java as a whole, so I'll probably be back posting again, soon. Again, Thank you! – Munkeeface Mar 26 '14 at 17:00
  • @Asthor I have a minor question to add. The buttons I am using are `JToggleButton`. How would I find out whether or not it is selected/toggled? I want the icon to changed based on whether or not the button is toggled (The overall program from this question is a "flip the card to find ___". But when you flip it, if it's not there, an X will appear than the original question mark – Munkeeface Mar 26 '14 at 18:31
  • @Munkeeface http://stackoverflow.com/questions/7524536/getting-the-state-of-jtogglebutton – Asthor Mar 27 '14 at 01:31
  • @Munkeeface Can't answer that as I don't know what your code does. This is also far outside the scope of the question. – Asthor Mar 28 '14 at 10:21
  • @Asthor, sorry about that, getting a bit ahead of myself – Munkeeface Mar 28 '14 at 12:24
0

As @LostKatana said, I think that a layout will be a better way to set the buttons position. here you will foun d how to use them (if you don't know yet).

Guto
  • 341
  • 15
  • 25
  • I'm not necessarily referring to the layout of the buttons, but how to modify each of them, or a lump sum of them without having to reference them individually... i.e. if I want to edit the toolTipText for buttons 1-5 but not the rest, a group wouldn't work, would it? How would I modify those specific sets but in surplus? The question in this matter is not the layout, per se, but more or less the modification of multiples. I'll show another example in the question to clarify – Munkeeface Mar 26 '14 at 14:04