Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JButton array with NetBeans 6.1

800471May 19 2008 — edited May 19 2008
Hi,


I am relatively new to Java and NetBeans.


I have the following problem; wish someone could help me.


I need to create an array of 30 buttons programatically, on a Panel, namely pnlButtonPack. I have created this panel using NetBeans 6.1 GUI builder, along with several other components which are on other panels, frames and so on.


All the 30 buttons are with same behaviour and I need only the index of the button that a user presses to trigger different actions. Thus, one ActionListner
for the entire array.


I know how to create the button array if I were to create it purely on the code editor, the relevent portion of the code being;
 JButton[] btnNumbers = new JButton[30];
    JTextField txtDisplay = new JTextField();

     for (int i = 0; i < btnNumbers.length; i++){
             String text = String.valueOf(i);
             JButton btn = new JButton( text );
             btn.addActionListener( this );
             btn.setMnemonic( text.charAt(0) );
             btnNumbers[i] = btn;
             pnlButtonPack.add( btn );
      }



      public void actionPerformed(ActionEvent e){
            JButton source = (JButton)e.getSource();
            txtDisplay.replaceSelection( source.getActionCommand());
       }
How can I do this on NetBeans ?


The guarded code does not allow me to insert my code at the appropriate places. (The "code" pane in the component Properties window is ****, it inserts my code at wrong places).


One option is to drag and drop 30 buttons on my panel, but that idea and the resulting code would be rediculous.
Another option would be to design the whole GUI from scratch on the code editor but that defeats the purpose of using NetBeans in the first place.


Your kind advice on this is highly appreciated please.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 16 2008
Added on May 19 2008
5 comments
1,032 views