Get the index of the clicked on JButton

Hi all

I have a table of 11 JButtons.

Notice of JButton [] = new JButton [11];

I added the JButton to the addActionListener.

Then in the ActionPerformed (ActionEvent ae) overrided method I use the

If (ae.getSource == btnC [0])
set of actions

How can I deduct that JButton has been clicked.
I have todo for all individual buttons 11
How can I get the index of the button clicked!

Please on your comment, an example would be better if its not not asking too much

OK, here's an example of the first form (""-If your 11 buttons have 11 different functions, they each have a listener code dedicated ""):

public class GodGamePanel extends JPanel() {

    public GodgamePanel() {
        Jbutton button1 = new JButton("Save the world");
        button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                world.save();
            }
        });

        Jbutton button2 = new JButton("Give up");
        button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                System.exit();
            }
        });

        this.add(button1);
        this.add(button2);
    }
}

Here is an example of the second form ("-if all the buttons have the same functionality, just on another entity (...)") Add another instance of the class of the listener even already using specific knowledge to an entity that has the code setting up the button'):

public class DivinePunitionsPanel extends JPanel() {

    public DivinePunitionsPanel() {
        Jbutton button1 = new JButton("Destroy City1");
        button1.addActionListener(new CityDestroyerListener("City1");

        Jbutton button2 = new JButton("Destroy City2");
        button1.addActionListener(new CityDestroyerListener("City2");

        this.add(button1);
        this.add(button2);
    }

    private class CityDestroyerListener implements ActionListener {
        private String cityName;

        CityDestroyerListener(String city) {
            this.cityName = city;
        }

        public void actionPerformed(ActionEvent evt) {
            RocketPlant.nukeCity(getGpsCoordinates(cityName));
        }
    }
}

Tags: Java

Similar Questions

  • Panel with click - image inside Panel event gets the click on-site

    I have a panel that uses its click event is locally.  It works very well for every click except when you click on a picture inside the Panel.  The image is reduced on the x-axis, so this worsens the problem (because the locally returned value is based on the value of is not put on the scale). Is it possible to ignore the click image event and just its parent panel get the click?

    Thanks for any help.

    Try to set mouseEnabled and mouseChildren to false on your Image.

  • How to get the clicked page number?

    I created a toolbutton.

    After I active this button I click on the page.

    Implement DoClickProc for toolbutton I get AVPageView and AVDevCoord.

    But how to know the page number, I clicked?

    Thank you.

    Hello

    Try to use the method AVPageViewGetPageNum (inPageView)

    Concerning

    Bartek

  • problem with the table of jbuttons actionListener

    Hello friends,

    I have a class of card whose 52 objects are stored in a vector.

    I have 2 players who have a vehicle for 11 objects of type map of each card.

    In my DispGame JFrame.
    I stored 11 JButtons in a vector that appear in the JPanel.

    When I click on a particular card (IE JButton) it is displayed on a JLabel in the middle of the game.
    to do this, following code is written to associate the actionlisteners
    private void reAssociateBtnListener() {
         for(byte b=0; b<(byte) (btnCardsPL.size()); b++) {
              btnCardsPL.elementAt(b).addActionListener(new BtnCardLocalPlayer(b));
         }
    }
    
    private class BtnCardLocalPlayer implements ActionListener {
         byte index;
         public BtnCardLocalPlayer(byte idx) {
              index = idx;
         }
         
         public void actionPerformed(ActionEvent evt) {
              
              // Remove the btnCardPL from the local player button array
              removeCardAndButton();
              
              playCard(index);
           }
    
         private void removeCardAndButton() {
              
              
              DispGame.pnlGame.remove(btnCardsPL.elementAt(index));    // ERROR aray idx out of bounds
              
              //Game.playerL.plyrCard.removeElementAt(index);
              btnCardsPL.removeElementAt(index);
              
              repaint();
         }
    }
    
    protected void playCard(byte b) {
    
    
         // Display the image on the board
         
         
         reAssociateBtnListener();         // Problem here
    }
    Now, here's the real problem. When the JButon is clicked, it is removed from the Vector and JPanel.
    Then I re associating the actionListeners.

    And also the associated object is removed from the 11 cards of players.

    When I re associate the actionlisteners then re action is performed. That is to say the event automatically fires again and a new card is played. How can I stop this!

    Below is the reference to my previous post about the event of the dashboard buttons.
    Get the index of the clicked on JButton
    as suggested by jduprez

    I hope I explained my point of view and clearly doubt!

    Thanking in advance!

    When you 'relink the action listeners', you add only more action listeners to the buttons. You never remove the original action listener. When you press the second button, it has two headphones. When you press the third button, there are three listeners.

  • get the index of an element of an array with the click of mouse

    Hi all

    I want to get the index of an item in an array when I click on that element. I have seen a few examples in the forum, but they do not work for me... Can someone please help me to do this. I enclose my vi here.

    Thanking you with impatience,

    Kind regards

    Nitz...


  • I get no answer to by clicking "Edit" in the dialog box indexing options. ;

    original title: indexation of dialog options change

    I recently followed instructions from this forum on the removal of invalid index locations; I have rebuilt the index. Unfortunately, an external drive that had been indexed throughout without asking me, is not be indexed now.  When I followed the instructions from Microsoft on the addition of the search location, I get no answer to by clicking "Edit" in the dialog box indexing options. ; nothing happens.

    Help!

    Hello

    Method 1: Let flow the fixit available in the link below and then check

    Fix Windows Search when it crashes or shows no results

    http://support.Microsoft.com/mats/windows_search/en-us

     

    Method 2: If a Protection of resources Windows (WRP) file is missing or is damaged, Windows may not behave as expected. Auditor of file system (CFS) scan to fix all of the corrupted system files. To do this, follow the steps mentioned in the link below:

    How to use the System File Checker tool to fix the system files missing or corrupted on Windows Vista or Windows 7

    http://support.Microsoft.com/kb/929833

     

    Method 3:  Select the boot and then check if the problem persists

    Follow step 1 in the link below,
    How to troubleshoot a problem by performing a clean boot in Windows Vista or in Windows 7

    If everything works well after a clean boot, you can deduce that some third-party services are at the origin of the problem.

    Continue with the remaining steps to pin-point on the third party service.

    After find you the program that is causing the problem, you will have to perhaps to update or install a newer version of the program, if you rarely use that you should consider uninstalling the software.

    Important: n ' forget not to put the computer to a normal startup follow step 7 in the link.

  • problem opening document pages. Get the message: the requested index.xml file is missing.

    I can't open the document 'Pages' and get "the index.xml file required is missing."

    You should post the related issues of Pages in the Pages for Mac community support.

    You try to open a document v5 Pages in Pages ' 08 or 09 ' which does not support the format of document Pages v5. Exit your application Pages ' 08 or 09 ' and then double-click the document even have it open in Pages v5.

    When you are in Pages ' 08, ' 09, or same v5, the red light on the title bar of the document just close the current document and fails to complete Pages.

  • I'm not able to open my documents pages. I get the message: the requested index.xml file is missing.  I wonder if it is because I have updates 10.11.5

    I have an iMac (20-inch, mid 2007) processor 2 GHz Intel Core 2 Duo 3 GB 667 MHz DDR2 SDRAM memory and recently updated to el capitan 10.11.5.  I have Pages ' 09, and I can't open my documents pages and get the message:

    The index.xml file required is missing.

    I now see that it says return to default pages 5.6.2.

    I can open them as 5.6.2 then save tehm in pages ' 09?

    Can someone help me with easy tips?

    Only the Pages ' 09 or old documents contain internal index.xml file. When you try to open a document in Pages ' 09 Pages v5, you get this dialog on the missing index.xml. The key is to stop Pages ' 09 and then double click on the document to open it in v5.6.2 Pages.

    Pages ' 09 will not leave by simply clicking on the red light on the title bar of the document. As long as the Pages ' 09 continues to run, it will intercept all attempts to open any document Pages, including incompatible versions of the v5 Pages.

    This has nothing to do with OS X 10.11.5. He has been a user focused on the issue since October 2013.

  • Determine which cluster and the array index element was clicked on table clusters

    Hello

    I'm overload of brain with dynamic events and references and nodes of property etc.

    I am trying to identify the cluster and the array index element that was clicked on table clusters

    This gives me the name of the element:

    Is this reasonable?  How can I get the index?

    Thank you

    Paul

    Hello

    Download the VI in this link (also attached).  It gives you the index of the array element that you clicked. Hope this will help you.

    Kind regards
    Nitz

    (Bravo to give good answers, mark it as a Solution If your problem is resolved)

  • Get the index of the column of table removed

    Is there a way to get the index of the column in a 2D table that has been deleted? In my application, the user has the possibility to use the right click option 'remove a column' on a 2D picture, but I would like to use this information to remove the same column in a different table programmatically. Is this possible?

    There is a file published in the documents of the community that refers to a location in a sheet of the user has clicked.  Use it like the image below.

    https://decibel.NI.com/content/docs/doc-22434

  • get the index of the item selected in the image grid

    Hello

    use the latest version of bbui.js and I am facing a problem in getting the selected item (but click or press event) to image grid.

    given that all images have the same class name and bb-type of data and the only difference between them is data-bb-img I need to shoot an event handler when you select a specific grid image and passing the argument index...

    can someone help me please?

    function jQuery Event.Target solved the problem! Thank you

  • "Adobe Acrobat Reader DC cannot open 'amy application' because it's a non supported file type or because the file is damaged."   I received this message during indexing on my computer.  I could not get the message, so I uninstalled Adob

    "Adobe Acrobat Reader DC cannot open 'amy application' because it's a non supported file type or because the file is damaged."

    I received this message during indexing on my computer.  I could not get the message, so I

    uninstalled Adobe.

    After you have uninstalled Abobe, I was able to get all my jobs and applications (with the exception of adobe files).

    Then I tried to re - install Adobe, but who has turned to a complicated procedure.

    There are so many choices, and there are a number of downloadable updates. What was I guess what to do?   But I have chosen one of the Adobes and tried to install it.

    After that I don't know what happened.  All my desktop icons are now Adobe icons in place.   that is The Firefox icon is now replaced by an icon of Adobe.  It is the same for several other desktop program icons. (QuickTime, chrome, Internet Explorer, etc.)

    When I try to open the program by clicking on it, I get the above message.

    I went to the site of help from Adobe and read a complex string of instructions, including

    words like '% pdf', "DWROD" and, for me, other nonsensical words and phrases.

    Needless to say that the instructions were no help.

    I said all that to say this.  I want that my computer back.  What should I do to do?

    Sincerely,

    Ralph Thompson

    [personal information... [Mod - https://forums.adobe.com/docs/DOC-3731]

    [This is an open forum, not the Adobe support, please do not post personal information]

    [If you send messages by e-mail, please turn off your "sig file" function for display]

    This may help: Application, file icons change in Acrobat/Reader icon

  • Get the row index in a dynamic Table

    Hello gurus,

    How can I get the index of a line when it is added dynamically.

    Capture.PNG

    When the user clicks on the "-" he should get the index of the row on which he clicked the button.

    I tried using the code

    var var1 = (data. Design_page1. Working_times_subform. Working_Table.Row1.instanceManager.Count - 1);

    But it gives me the total number of lines. I want that the index of the particular line.

    Thank you.

    Kind regards

    Nikhil

    On the "-" button use this.parent.parent.index to get the index of the line, the button is in. the mention of 'parent' is used to back up the hierarchy so if you had say a subform around the button in the cell, you have to add another parent to learn Row1.

    In order to remove the line try: _Row1.removeInstance (this.parent.parent.index);

    Or it should work too: _Row1.removeInstance (Row1.index);

    The underscore character is a shortcut for the management authority. You can not use it in the object names, I'm sure it caused me problems in the past, I only use case for object names now (to avoid too many dashes).

    You don't need to specify the full path to the object that you are trying to access. The '-' button be Row1 can see Row1.

  • How can I get the default for the new tab page when there is no improved/classic/empty choises when I click on the gear icon.

    When I open a new tab, I used to get a page which had tiles for some of the sites I visit often. Now I get the Bing search page. The help of instructions say I can change the default new tab page by clicking on the gear on a new improved, classic, or blank page icon in a drop-down list. When I click on the train, I don't get this list of options. Instead, a settings page appears, allowing me to make various changes, but improved, classic or white new tab page choices aren't there. What should I do?

    To modify the new page tab, please follow these steps:

    1. Go to about:config in Firefox
    2. Locate the browser.newtab.url string
    3. Set the value of this string on the URL of your page to the desired tab

    This should change the new tab page.

    Notes:

    • Set the about:newtab to get the page of the new classic tab
    • Set the about:home to get the default home page
    • Set the about:blank to get the blank page
  • When I click on address book I no longer get the slot to insert a name, instead a long list of names/addresses

    I sent this way new emails (not responses): click on address book. Place comes the space to insert a name. I do, find the name (among some close to it), select it and then click on "write". The result is an email address, which I proceed to write and send. But since there is one day, when I click on the address book, what appears is a long list of names and email addresses. It fills most of the screen of the iMac. And there is no space to insert a name.
    Without doubt, I did something stupid to get there. What have I done? And how I turn the tide?
    Thank you.
    Arthur

    This isn't answering your question on what is wrong.

    I think that the address book is a pretty lame place to start.

    Click on write or reply or forward and window of writing messages appears. pre-populated with the text of the previous message if it is a reply or forward.

    If you select the To: box and start typing, it will feature names in the address book corresponding to what you typed.

    If you really prefer to choose from a list, you can open a Contacts Sidebar which lists one of your address books. There are buttons to add Contacts selected to your message. You can drag and drop and use right click too. On a PC, F9 opens this sidebar, or you can use alt + v to get the view menu and choose the Contacts Sidebar in the submenu. I don't know how much it works on Mac.

    It is so easy to add multiple addresses. You can set them as Cc or BCC.

    Your method allows you to send a new message, but does not support the answer or transfer. It penetrates into all recipients regarding: and you have to change one by one, if that is not appropriate.

    You see why I think that the address book is a bad place to start from?

    If this alternative approach does not work then I suggest that corrupted address book. You have a recent backup to restore from?

Maybe you are looking for

  • HP 15-r014tx Notebook PC: computer hp laptop 15 LCD Back Cover Case Assembly

    I have HP 15-r014tx Notebook PC. My laptopbroken screen LCD NOT only his hood back. I want to buy Moose Back Cover Assembly from my laptop Case. I want to know where I can buy it online (pefferable) or offline. I live in India. I attach an Image also

  • HP Mini 110-1030NR: password power

    My brother password protected the boot CNU9214KPX

  • 5 D Mark IV too big video file size

    Why the 5 d Mark IV create greater than its predecessor at the same fps/res file size? all solutions beyond switching to ALL - I have to IPB?

  • Transfer of Win8 of HD to an SSD

    I want to improve my HP2000 2c10dx to a Mushkin enhanced chronos slow HD mknssdcr240GB-7 2.5 "sata III on sale at Newegg for $150. It would work to plug them into 2 sata ports to spare on my desktop computer to transfer the entire operating system to

  • 2 SmartBand does not connect to new phone

    I had a SmartBand 2 for the new year, that it was connected to my old Sony Xperia z3, but I had to have a new because I broke the screen and now I can't seem to connect to the new phone. I tried all the troubleshooting but to no avail. However, I hav