Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8672557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:14:07+00:00 2026-06-12T19:14:07+00:00

I just have a question on the following. I’ve got a 2D array of

  • 0

I just have a question on the following. I’ve got a 2D array of buttons that require me to run another method when I click on them. My current method relies on the following input, a String (which I can get from the user easily) and two integer values. These are dictated by the buttons position in the array.

I have a button listener attached to these buttons but I am not too sure how I can work out what the button’s position actually is. I’ve made my own button class (because I wanted some specific settings and I thought it would be easier) and when making it I implemented a method called getXPos and getYPos which basically hold the values for the actual button in the array when it was created. Thing is I don’t know how to retrieve these values now as the listener doesn’t actually know what button is being pressed does it?

I can use the getSource() method but I then don’t know how to invoke that source’s methods. For example I tried to do the following.

int x = event.getSource().getXPos(); but I am unable to do this. Is there any way of telling what button I have pressed so I can access it’s internal methods or something similar? Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T19:14:08+00:00Added an answer on June 12, 2026 at 7:14 pm

    To call a method on the source, you have to cast it first. If you’re never adding your ActionListener to anything but an instance of your special MyButton with its x and y variables, you can do this:

    MyButton button = (MyButton) event.getSource();
    int x = button.getXPos();
    int y = button.getYPos();
    

    Then MyButton contains the x and y:

    public class MyButton extends JButton {
    
        private int xPos;
        private int yPos;
    
        // ...
    
        public int getXPos() {
            return xPos;
        }
    
        public int getYPos() {
            return yPos;
        }
    }
    

    And make sure you’re always adding your listener to instances of MyButton:

    MyButton myButton = new MyButton();
    // ...
    myButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            MyButton button = (MyButton) e.getSource();
            int xPos = button.getXPos();
            int yPos = button.getYPos();
            // Do something with x and y
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just a (hopefully) quick question, I have the following HTML code: <tr> <td><img src=img/icons/file_pdf.png></td>
I just have a question regarding SimpleJSON's documentation. Is it implicit understood that functions
I have a question that I just don't feel like I've found a satisfactory
I have a following question. Suppose I have a bunch of repositories hosted that
I have just started learning C# and would have one question that I cannot
I just have a question thats been pozzling my head all morning. I want
I just have a quick question about how to get the length of a
So I just have posted a question about this code (which was answered): $(document).ready(Main);
I have just a question... I'm seeing this coding style in various php scripts
This is probably more a design or style question: I have just been considering

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.