Array of Button


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Array of Button

  1. #1
    Join Date
    Aug 2002
    Posts
    2

    Array of Button

    Help,

    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[2][3] would be the 3rd row 3rd column.

    Can any one suggest how I would code this?


    Thanks


    Bill

  2. #2
    Join Date
    Aug 2002
    Posts
    94
    Hi Bill,

    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.
    Pankaj

  3. #3
    Join Date
    Aug 2002
    Posts
    94

    Unhappy

    Oops!

    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.
    bye

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center