Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Actionlistener and actionperformed question

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Actionlistener and actionperformed question

    i have created a matrix with button and i want to listen event of this matrix. i need to take coordinates of matrix (X Y) when a button clicked and i ll use this coordinate for my other operations in code but i couldnt find a solution get coordinates

    this is snippet of my code


    private void BlockButtonSec() {

    secim=1;
    for (int i = 0; i < 50; i++) {
    for (int j = 0; j < 50; j++) {

    butonlar[i][j].addActionListener(this);
    }

    }

    for (int i = 0; i < 50; i++) {
    for (int j = 0; j < 50; j++) {

    gecici=butonlar[i][j];

    gecici.addActionListener
    (new ActionListener() {
    public void actionPerformed(ActionEvent event) {


    coordinate_x=i;
    coordinate_x=j;
    sent(coordinate_x,coordinate_y);
    setbackgroundofthisbutton();
    }

    });
    }

    }



    and other my idea is taking out actionperformed side like that:



    public void actionPerformed(ActionEvent e) {
    // JButton tiklandi=new JButton();
    if(secim==1){
    butonlar[i][j].se
    // tiklandi.setBackground(Color.RED);
    }

    }


    this code is not completed --> butonlar[i][j].se because i,j is not available but i couldnt sent i,j


    just i need an idea that:
    when i clicked a button how can i change any button color in buttons of matrix and how can i get coordinates from buttons of matrix


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Actionlistener and actionperformed question

    how can i get coordinates from buttons of matrix
    One way would be to use the ClientProperties of the JComponent class. The ClientProperties is a hashtable.
    You could create an object with the x,y for the button and save/put it in the ClientProperties for that button. When the listener method is called you could then get the object with the values from the ClientProperties for the button that was pressed.

Similar Threads

  1. actionPerformed Question.
    By TP-Oreilly in forum Java Theory & Questions
    Replies: 1
    Last Post: December 9th, 2011, 11:18 AM
  2. actionPerformed not returning a value.
    By gher in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 22nd, 2011, 10:15 AM
  3. actionPerformed
    By Kumarrrr in forum Java Theory & Questions
    Replies: 5
    Last Post: November 23rd, 2010, 09:08 AM
  4. New Java user ActionListener question
    By VBGuy in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 20th, 2010, 09:46 PM
  5. Question about ActionListener
    By TimW in forum AWT / Java Swing
    Replies: 6
    Last Post: November 4th, 2009, 11:00 AM