File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes how to add JButtons to an array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "how to add JButtons to an array" Watch "how to add JButtons to an array" New topic

how to add JButtons to an array

mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

Hi ,

I have a GUI i have created a gui qith 16 JButtons

example of how i created them

they go from A1-A4 to D1-D4 ,four rows.
I would like to add the JButtons to an array, how should i go about it? i am guessing it is possible but maybe i am wrong?

i would like to make something like JButton[] rowA = JButton{A1,A2,A3,A4}; -----> i am really terrible whith Arrays!!

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46352
Suggest you get yourself a factory method.You would have to check the syntax, and that I have got the names of all the methods right. You can overload that method to your heart’s content. You can use Actions instead of Listeners.
mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

Hello Campbell,

Thanks for your response, that would be a good way to create the buttons, but i have already created them and want to have them in a list of rows.Maybe you missunderstood my question OR i don't understand your reply <--- (which is possible ;) ).

I am currently trying this with ArrayList

i will eventually need to test quite a few things in each row like if rowA doesn't equal an even number it will be false as well as if there are more than three of the same numbers beside eachother it will be false and so on,
i am thinking this will get really long with all the checks, maybe there is a better solution?

I agree. Here's the link:
subject: how to add JButtons to an array
It's not a secret anymore!