How can I get the "text" field from the actionEvent.getSource() ?

I have some sample code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;
public class JFrameTester{
     public static void main( String[] args ) {
          JFrame f = new JFrame("JFrame");
          f.setSize( 500, 500 );
          ArrayList < JButton > buttonsArr = new ArrayList < JButton > ();
          buttonsArr.add( new JButton( "first" ) );
          buttonsArr.add( new JButton( "second" ) );
          buttonsArr.add( new JButton( "third" ) );
          MyListener myListener = new MyListener();
          ( (JButton) buttonsArr.get( 0 ) ).addActionListener( myListener );
          ( (JButton) buttonsArr.get( 1 ) ).addActionListener( myListener );
          ( (JButton) buttonsArr.get( 2 ) ).addActionListener( myListener );
          JPanel panel = new JPanel();
          panel.add( buttonsArr.get( 0 ) );
          panel.add( buttonsArr.get( 1 ) );
          panel.add( buttonsArr.get( 2 ) );
          f.getContentPane().add( BorderLayout.CENTER, panel );
          f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
          f.setVisible( true );
     public static class MyListener  implements ActionListener{
          public MyListener() {}
          public void actionPerformed( ActionEvent e ) {
               System.out.println( "hi!! " + e.getSource() );
               // I need to know a title of the button (which was clicked)...
}The output of the code is something like this:
hi! javax.swing.JButton[,140,5,60x25,alignmentX=0.0,alignmentY=0.5,
border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1ebcda2d,
flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,
disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=2,left=14,bottom=2,
right=14],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,
rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=first,defaultCapable=true]
I need this: "first" (from this part: "text=first" of the output above).
Does anyone know how can I get the "text" field from the e.getSource() ?

System.out.println( "hi!! " + ( (JButton) e.getSource() ).getText() );I think the problem is solved..If your need is to know the text of the button, yes.
In a real-world application, no.
In a RW application, a typical need is merely to know the "logical role" of the button (i.e., the button that validates the form, regardless of whether its text is "OK" or "Save", "Go",...). Text tends to vary much more than the structure of the UI over time.
In this case you can get the source's name (+getName()+), which will be the name that you've set to the button at UI construction time. Or you can compare the source for equality with either button ( +if evt.getSource()==okButton) {...}+ ).
All in all, I think the best solution is: don't use the same ActionListener for more than one action (+i.e.+ don't add the same ActionListener to all your buttons, which leads to a big if-then-else series in your actionPerformed() ).
Eventually, if you're listening to a single button's actions, whose text change over time (e.g. "pause"/"resume" in a VCR bar), I still think it's a bad idea to rely on the text of the button - instead, this text corresponds to a logical state (resp. playing/paused), it is more maintainable to base your logic on the state - which is more resilient to the evolutions of the UI (e.g. if you happen to use 2 toggle buttons instead of one single play/pause button).

Similar Messages

  • How can i get HD video out from the lumia 900. ? ...

    How can i get HD video out from the lumia 900. ? Is there some kind of gable. ? N8 has HD cable port..so where is lumia`s port. ?

    Download Zune and start it, when started connect your Lumia 900 and you can now transfer mediafiles from the Lumia to your PC through a USB connection. For Mac use the Mac connector.
    Press the 'Accept As Solution' icon if I have solved your problem, click on the Star Icon below if my advice has helped you!

  • HT204411 How can I get a tv show from the Canadian iTunes store from the United States Store?

    I have been trying to find the tv show the Listener and a few others everywhere. I can only find them in the Canadian iTunes Store. How can I get it in the United States Store?

    Ask whomever makes the show to sell it in the U.S Store.

  • HT201269 All the photographs stored on my computer ended up getting put on my iPhone. How can I get rid of them from the iPhone?

    All the photographs stored on my computer ended up getting put on my iPhone. How can I remove them from the iPhone?

    Photos synced to your phone can only be removed from your phone the same way they got there, through the iTunes sync process. Connect your phone, iTunes running, & under the photos tab remove the checks for all albums/folders you do not want on your phone. Hit the apply/sync button & they will be removed from your phone.

  • How can i get a particular model from the iphone 5s

    I am from austria an for the lte i need the iphone 5s model A1457 or A1530. If i would buy one, would i get one of the model?
    Thanks for answering. ;)
    ...and sorry for my english

    If you live in Austria you cannot purchase from an online Store in another country
    The various models are only available in the countries shown on table I linked for you
    You could purchase in Germany and you will get the same warranty cover in Austria since both are member states of EU

  • If messages are deleted from an itouch that is synced the same Apple ID and phone number, how can you get them to delete from the phone as well?

    how do I delete messages from both my itouch and my iphone

    You have to delete the messages from the individual device. If you delete messages on one device, messages on the other are not automatically deleted.

  • I need to set up a new iTunes account. How can I get all my music from the old account on to the new one??? Thanks../

    I have just bought an ipad 2 and wish to set up a new account as now I won't have to share with my sister. I want to get all the music off my current iTunes account &amp; put it on to the ipad/new iTunes account. I'm not too sure how to do this though.... Please help lol :-) x

    If you mean iTunes libraries or computer user accounts, just create one. To make a new iTunes library on Windows, launch iTunes with the Shift key held down and choose Create Library.
    If you mean iTunes Store accounts, click here and request assistance.
    (79934)

  • How can I get back my bookmarks from the old sync?

    I reinstalled Firefox 29.0 but when I try to download the old sync bookmarks I can't find a way to enter my sync key.

    See this KB article - [[How to update to the new Firefox Sync]]

  • How can I get specific files back from my Time Machine backup?

    I wanted to make a fresh install on my MacBook Pro, in order to only have Java 8 instead of any older Java versions. So far so good. But now I want to get specific files/libraries from my external backup (USB):
    One Parallels Virtual Machine
    My old mail
    My iPhoto Library
    I don't need any settings for these, since I can easily configure them again.
    I already used the Migration Assistent to restore all apps.
    How can I get specific data back from the backup without restoring all stuff that I don't want anymore?
    I'm now working on a new account on the fresh system, called 'Admin'. My old account was called 'Hans' (or 'hans') and I'd like to create such an account again for all my tasks.
    Hans

    How can I get specific data back from the backup without restoring all stuff that I don't want anymore?
    I'm now working on a new account on the fresh system, called 'Admin'. My old account was called 'Hans' (or 'hans') and I'd like to create such an account again for all my tasks.
    You will want to add the same User account(s) which hold the data you want to restore. So you will want to add a User account of 'Hans or hans'. Check your Time Machine back up drive to see how the User account is labeled (Hans or hans). Navigate to the Users folder and see how it's labeled.
    You can then use your Time Machine back up drive and navigate to the User Home folder to copy its data over (e.g. contents from Documents, Downloads, Music, Pictures, etc.). Repeat for other Users.

  • Ever since I enabled iMessage on my Mac Airbook, I can no longer receive text messages from the iMessage people on my iPhone, only on my iMessage now!!  How do I get the messages to reach both iMessage and iPhone??

    Ever since I enabled iMessage on my Mac Airbook, I can no longer receive text messages from the iMessage people on my iPhone, only on my iMessage now!!  How do I get the messages to reach both iMessage and iPhone??

    We are suffering very similar issues when communicating from my iphone to a friend who has both iphone and iPad enabled.
    As long as I reply to an existing text chain, then it is visible on the target iphone.  If I SMS the phone number, it gets routed to the IPAD!!   And NO, I should not have to disable imessage when I want to send an SMS!!

  • How to remove the automatically generated text fields from the InfoSet

    I followed the procedure by SAP help.
    Automatically generated text fields are marked with a 'T' on the icon in the InfoSet. You can remove the automatically generated text fields from the InfoSet in the initial screen of the InfoSet maintenance under Further Functions -> Delete Text Fields. Cancel the DataSource creation on the next screen and delete the text fields in the InfoSet maintenance transaction.
    I know that there is an option "No automatic text recognition" when I create a new InfoSet, however, I couldn't find out how to change an existed InfoSet. I checked menu Goto->Global Properties, but that option is disabled. Is there a way to remove text fields from an existed InfoSet?

    Hi,
    In the initial screen (when you enter transaction code SQ02) type your infoset name and goto menu: Infoset -> More functions -> Delete text fields. this will delete all text fields.
    I hope this helps to resolve the issue.
    Ram

  • How can I watch a Disney movie from the Disney website I have purchased movie cd and I can go to the website on by gateway computer but can't get it to play on my iPad it wants me to download flash but apple doesn't support flash or I can down loadQuickTi

    How can I watch a Disney movie from the Disney website I have purchased movie cd and I can go to the website on by gateway computer but can't get it to play on my iPad it wants me to download flash but apple doesn't support flash or I can down loadQuickTime but don't know how. Says to email website did that but takes me back to same place to email help

    Contact iTunes Customer Support, but honestly, there are no returns for iTunes purchases.

  • Since I downloaded the most recent version of iOS, my iPad and iPhone do not have all the music (from CD's) anymore that I have on our home computer iTunes Library.  How can I get all our music onto the music libraries on the iPhone and iPad? Thanks!

    Hi
    Since we downloaded the latest version of iOS on our iPad and iPhone, and synchronized our music collection in the Cloud, we can no longer see on our iPad and iPhone the music from our CD's that we have on our home computer's iTunes library.  We can only see music in the iPhone and iPad library that have been purchased on iTunes.  How can we get all our music from our computer's iTunes librbary visible on the iPhone and iPad?  Thanks, Nick

    Resync from your computer.
    iOS: Syncing with iTunes - Support - Apple
    If your having trouble syncing, come back and let us (fellow users) know.

  • My computer crashed and I lost everything. It was the only computer with Sync on it and I cannot get my recovery key: how can I get all my bookmarks from that Sync account?

    My computer crashed and I lost everything. It was the only computer with Sync on it and I cannot get my recovery key: how can I get all my bookmarks from that Sync account? I have a ton of bookmarks/saved password and the only way to get on sync is create a new recovery key and lose all of my information.
    Is there any way to get my stuff back without creating a new recovery key and losing all my old stuff?

    It has always been very basic to always maintain a  backup copy of your computer for just such an occasion.
    Use your backup copy to put everything back.

  • Ok I can't seem to get my apps,music, or photos from my iCloud from when I first setup iCloud on a iPhone 4. How do I get my things back from the iCloud and get them to my new device?

    Ok I can't seem to get my apps,music, or photos from my iCloud from when I first setup iCloud on a iPhone 4. How do I get my things back from the iCloud and get them to my new device?

    If you no longer have the phone or have any Backup of it at all, you can re-download your apps and your content from the iTunes Store by logging into your Apple ID at settings> iTunes & App Store. Unfortunately you can no longer recover the data that was associated with those apps.
    So far as your photos go, you can recover any photos that were in photo stream and taken less than 30 days ago by logging in with your Apple ID to settings> iCloud> photos. Unfortunately you will be unable to recover any photos from your camera roll.

Maybe you are looking for

  • Licensing of a Test SQL Server

    Hi, My question is regarding the use of SQL in a Development Environment. We have a VL Agreement. We have a Historical server using SQL in a 2 server cluster with a SAN. We purchased a SQL 2012 Standard Server for each server plus CAL's for all devic

  • I (thought) downloaded the trial version of Photoshop Elements, Cannot find it!!

    and it even says 'You have Thirty Days left on your Trial' but I cannot seem to find the actual Photoshop Elements! I can only find the Adobe Download Assistant! It took like 8 hours to download and when I got up this morning it said Download complet

  • When i click on item in xsltlistviewwebpart.ribbon get disabled

    Hi, I have a scenerio where I need to enable a delete option [in listitem inside a ribbon], on click of an item in a listview webpart. I am administrator of the site but for few records I am unable to see "delete button." Image attached below. So, I

  • Solaris 8 4/01 Intel Instalation/Boot Problems

    I am TRYING to install the Solaris 8 for Intel and i am having several problems. I managed to solve some of them but now i am stuck. I am running a PII with a an Adapetc 2940 and a scsi disk. I am using interactive instalation one software cd 1. I am

  • Material needed for AFS and SCM integration

    Hello Experts,         Please provide with links and help documents which having good informations about doing demand planning for AFS data in SCM.  Any help is highly appreciated. Thanks and Regards, Babu