• 0

[java] 2d Array Getsource


Question

i have a 2d array of JButtons. now i want to know the exact index where the button is pressed down.

if i were to use getSource method it would still not give me the index location.

any ideas :cry:

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

public class ..... imlements ActionListener ...

ArrayList array = new ArrayList();

.

.

.

for ( int i = 0; i < 100; i++ ) {

JButton jButton = new JButton("Hello");

jButton.addActionListener ( this );

button.add( jButton );

}

.

.

.

public void onActionPerformed( ActionEvent e ) {

object src = e.getSource();

int index = this.array.indexOf( src );

if( index != -1 ) {

}

}

Link to comment
Share on other sites

  • 0

Or if you dont want to use the ArrayList but instead want to use JButton [] then just search through the array

for( int = 0; i < array.length; i++ ) {

if( src == array) {

index = i;

break;

}

}

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.