Array of Button
I am a new Java programmer and I need some help.
I need to create a 4x4 array of Buttonsinside a panel. I would like to be able to address each button by the index of the array. For instance buttonArray would be the 3rd row 3rd column.
Can any one suggest how I would code this?
You can create array of buttons like the following :
1. Button but=new Button[your_array_size ]
you can also leave array size blank for starting.
2. Add suitable (& of required size) layout manager to your panel.
3. Add the buttons to layout manager.
4. For acessing - but[0,0] is the first button and so on.
Tip: You can identify buttons based on their labels or object names for event handling . Try to assign descritive names to buttons such as "start", "exit" , "move" etc instead of passive array to enhance your coding.
Hope this helps.
Between step 1 & 2 you also need one more step.
You have initialized the array of Buttons but not yet created its individual objects i.e Buttons so you will have to create each one of these Button namely
but[0,0]=new Button("Button 1");
but[0,1]=new Button("Button 2");
but[0,2]=new Button("Button 3");
and so on.
You have to do this step before you add these buttons to panel.
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center