The Java APIs - are we speaking the same language?

Hello again world.
I was reading in someone's response a suggestion to "write a different actionPerformed() method for each button."
So I thought, let me see if I can do that and came up with this:
import  java.awt.*;
import  java.awt.event.*;
import  javax.swing.*;
public  class  MyActionListeners
   private  JButton  myButtons[]  = new  JButton[3];
   public  MyActionListeners()
      myButtons[0] = new  JButton("Button 1");
      myButtons[1] = new  JButton("Button 2");
      myButtons[2] = new  JButton("Button 3");
      myButtons[0].addActionListener(new  ButtonListener());
      myButtons[1].addActionListener(new  ButtonListener());
      myButtons[2].addActionListener(new  ButtonListener());
      JPanel  panel = new  JPanel();
      panel.add(myButtons[0]);
      panel.add(myButtons[1]);
      panel.add(myButtons[2]);
      JFrame  frame = new  JFrame("Action Listeners");
      Container  contentPane = frame.getContentPane();
      contentPane.add(panel);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
   public  static  void  main(String[]  params)
      new  MyActionListeners();
class  ButtonListener  implements  ActionListener
   public  void  actionPerformed(ActionEvent  ae)
      JOptionPane.showMessageDialog(null , "You pressed " + ae.getActionCommand());
}which works fine but, of course, there's only one actionPerformed() method.
My first program, which I got to work as suggested, had three but when I noticed they all did the same thing (just announce which button was pressed), I decided to further test my skill to see if I could reduce it to the program above.
My question has to do with my difficulty in locating that getActionCommand() method.
It's not that it was actually difficult to find, it's more that I saw it and figured it was not what I was looking for.
The API describes the getActionCommand() method as "Returns the command string associated with this action."
I thought, "command string . . . I don't need no stinkin' command string"
I naturally tried the paramString() and the inherited toString() and getSource() methods first.
Indeed, it was the API description for the return value of paramString() - "a string identifying the event and its associated command" - that clued me in to looking at the getActionCommand() method.
So, why is my button text considered a "command string"?
I'm not playing semantics, I'm just trying to understand how to read the APIs.
Thank you one and all.
Ciao for now.

Anyway, for buttons, typically, what I do is either:
1) 1 listener for all buttons, usually the same class:
public class MyClass implements ActionListener {
   public MyClass() {
      button1.setActionCommand("new");
      button1.addActionListener(this);
      ... etc. for each button...
   public void actionPerformed(ActionEvent ae) {
      if("new"equals(ae.getActionCommand()) {
         doNewThing();
      } else if(...) ...
   public void doNewThing() {
      // do new stuff...
}The advantage here is that often you have buttons and menu items (which are buttons) and maybe even text fields which all fire action events for the same command, and can use the same listener.
The disadvantage here is that you typically end up with long sets of if/else statements to check for each command.
A modification on this is not using action commands, but if the buttons are all variables defined at the class level, you can use the event source to check if it's this or that component.
2) Use anonymous listeners to call methods.
public class MyClass implements ActionListener {
   public MyClass() {
      button1.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent ae) {
            doNewThing();
      ... etc. for each button...
   public void doNewThing() {
      // do new stuff...
}The disadvantage (sorta) is you end up with a lot of MyClass$#.class files. Of course, using Ant to compile and package everything in a Jar makes this more or less a non-issue. And it does often look cleaner. But doing that means you have separate listeners for the button and the menu item and whatever, which may call the same class. You can, of course, create 1 listener and reuse it for multiple components:
public class MyClass implements ActionListener {
   ActionListener newListener = new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
         doNewThing();
   public MyClass() {
      button1.addActionListener(newListener);
      ... etc. for each button...
   public void doNewThing() {
      // do new stuff...
}3) You can also use Action classes, which incorparate the action listener and several other things (text, icon, etc) for a button or menu. But honestly, I almost never use this.
4) Option 4 this code I wrote here:
http://forum.java.sun.com/thread.jspa?forumID=31&threadID=548168
An example based on what I've already shown using that would be:
public class MyClass implements ActionListener {
   public MyClass() {
      button1.addActionListener(new MethodInvokeAction(this, "doNewThing"));
      ... etc. for each button...
   public void doNewThing() {
      // do new stuff...
}I've used all these ways in real apps. Generally, I use something like 2 (a or b) if there's lots of buttons and menus, and 1 if there's only a few buttons. The one app that I wrote the MethodInvokeAction class for, I actually used several ways.

Similar Messages

  • Why, when you have auto update on, isn't the same language installed as before, but English instead.

    When auto update the new version installed is English and different from the original language installed. Probably the same thing happens with thunderbird. When I have a language installed I want an update in the same language without having to reinstall the wanted language.

    I already thought of that but it can't be the way it is suppossed to be. Firefox, and Thunderbird, offer an auto update function so the update automatically sould be in the same language and not in English as it does now. Downloading and installing your own language you can hardly call auto update.
    For now I have stopped auto update till a solution is presented.

  • What are JAVA API are threaded in default list plz

    hi every body
    Can u give me List of JAVA API are threaded default

    Can u give me List of JAVA API are threaded default1. Java is not an abbreviation.
    2. What do you mean?

  • Capabilities.language for variants of the same language (i.e: Portuguese from Portugal and Brazil)

    According to the documentation, Capabilities.language has a fixed list of possible values, as shown here:
    http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/system/Capabiliti es.html#language
    For Portuguese there's only one entry: "pt"
    So will Capabilites.language return the same "pt" value for people from Portugal and from Brazil?
    How can I tell the difference? How can i get the "pt-br" or "pt-pt" strings?
    Thanks!

    Check out the difference between Capabilities.language and Capabilities.languages
    http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/system/Capabiliti es.html#language
    vs.
    http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/system/Capabiliti es.html#languages
    roymeo

  • Why to use the Java API in MDM

    Hi Experts,
    I am new to the MDM.
    We are having the requirement of inetration between Portal and MDM.Can u please tell me when to use the following coponents like the scenorio's.
    Why to install the MDM Business packages in Portal?
    why to use the JAVA API.
    Please give me the complete information.
    Regards

    Hi Vijay,
    Standard Business Package is use to provide interaction between MDM Server and the Portal. It consists of MDM iViews like Item Detail iView (allows to create,edit and delete the records) , Resultset iView (Displays the records, allows to add the records in workflows etc), Search iViews (for searching)i.e. Data Manager functionality.
    Java API are used when you need some functionality that standard MDM iViews dont have.MDM Java API consists of set of classes and interfaces with the help of which customization can be done.
    Regards,
    Jitesh Talreja

  • Reg:special characters are not priniting while selecting the CS language

    Hi All,
    When I am trying to print in CS language some special characters are not printing.
    Even when I was trying to create the entries in TTDGT table ,these special characters are not created
    ( příkazu) here r & i are the special characters. instead of special characters simply r and i were created .
    Could you please advice me on this how to resolve this .
    Regards,
    S.chaitanya.

    Hello Chaitu,
    I think you are working on scripts and trying to create the Script in different language?
    However, try to copy the text in CS and paste it in STandard Text. If it is not displaying properly then Try to log in to the same language and try to paste that Text in the STanard text So10. If it appears properly then your issue is fixed.
    Please let me know if the issue is while printing the Script form, i mean are you able to view it properly in the Print preview...?
    If this is the issue then it is not an issue at your end user needs to check whether his printer is supporting their langauge or not.
    Also check with the related language package . for ex I had worked on the Script form and tried to convert it into chinese i faced the same issue so when i checked with the System Admin he installed Asian Langauge Package in my system. After that I was able to convert the script into Chinese properly.
    Hope this will be helpful..
    Regards,
    Kittu

  • I bought 3 films and none of them had subtitles or alternative languages: why? I paid full price (like a dvd) without the same options. Why should I pay the same price? What I want is to change languages or to have subtitles (Kung Fu Panda2 and Rise..apes

    I bought 3 films and none of them had subtitles or alternative languages: why? I paid full price (like a dvd) without the same options. Why should I pay the same price? What I want is to change languages or to have subtitles (Kung Fu Panda2 and Rise of the Planet of the Apes and SpongeBob).
    Pease tell me.
    Best Regards

    You can't change languages in any of the movies offered in the store. Some movies have subtitles but it is in the same language as the movie.
    Depending on what country you are in, you may get movies in that countries native language.

  • Is there any java API throgh which My program can get harddisk,cpu ID adres

    hello
    Is there any java api throuh which i can get hard disk and processor serial numbers.
    i have already use
    process p=Runtime.getTime().exec("cmd command");
    please other than this send any java api, progamming technique that remain same on all plate forms
    advance thanks
    best regards

    >
    Is there any java api throuh which i can get hard disk and processor serial numbers.
    i have already use ..>1) What business is that information to your app.? Or to put that another way. Why do you want to know?
    2) Those words are 'Java', 'API' and 'I' (which is always upper case).
    3) One upper case letter should be used at the start of each sentence, not just once at the start of a post.

  • Java API support in MHP

    What java APIs are supported exactly on the MHP platform? The specs descrive mainly the MHP APIs (havi, dvb, etc), but how about the core java APIs? Are we really restricted to java 1.1? Cause I tried using an ArrayList, which was introduced with java 1.2, and it's running fine in my Alticast STB. Should I ignore this and use a Vector instead?

    Ideally, you should use only Java 1.1 with the restrictions described in the MHP specification. Most people do use pJava as you say, but there are a few companies using other implementations.
    While using elements from Java 1.2 or later may work, it's not guaranteed that it will be interoperable, and you'll probably run in to problems on some platforms.
    Steve.

  • RFC_ERROR_COMMUNICATION:     Select one of the installed languages

    Hi Experts,
    When I try to test the JCO Connection created for ESS.
    name : SAP_R3_SelfServiceGenerics.
    I am getting the error message like below:
    com.sap.mw.jco.JCO$Exception: (102) RFC_ERROR_COMMUNICATION:      Select one of the installed languages
    If I try to edit the same,I am getting Null Pointer Exception.
    Can anyone help me out to resolve this error?
    Regards,
    Karthick

    Are you using an LDAP as your UME, or are the user records from from R/3 ?
    Apparently  (Ive never done it) you used to be able to force a default language by editing this file (back in the EP5 days):
    <J2EE root>\WEB-INF\portal\system\properties\prtDefault.properties
    Look at the following lines :
    This is the default language to be used when none is specified
    request.defaultlanguage=en
    request.defaultcountry=us
    If you prefer to have the same language for all users no matter their user locale, change the lines to this :
    This is the default language to be used when none is specified
    request.mandatorylanguage=en
    request.mandatorycountry=us
    I hope this works out for you

  • Java threads. Are they running at the same time?

    Hello everybody. I am implementing a multiagent system in java and at some point in the program i need a specific function (from a specific class) to run at the same time in several "copies". Those functions are communicating with each other with some messages, so as you can understand not running at the same time is a big problem because of this messages.
    Below i am giving you an example of the code i am using. I would like your opinion whether those functions are running parallel or serial. If they are not running parallel what should i do? Any opinion or something that can help me would be perfect. Thanks in advance.
    //my class
    public class Agent extends Thread {
                           public void MinConflicts(Agent array[],int max_moves){//code in here}
    public static void main(String[] args) throws IOException {
                  Agent[] fbi = new Agent[num] //num is inserted by the user
                 //code here
                 for(i=0;i<num;i++){
                         fbi.start(); //starting the treads
    for(i=0;i<num;i++){
    fbi[i].MinConflicts(fbi,max_moves); //running the function that i want to run parallel
    //code here also :)

    What you appear to be doing here is creating an array of Thread references (which I hope at some point gets populated with actual Thread object references!)......because your first loop is wanting to cycle through each array slot starting the referenced Thread. Then the next loop iterates through the array again, this time calling the method for each Thread object.
    Assuming that you do populate the array, what this code will do is start each Thread which, I believe, will start and immediately terminate because run() has not been over-ridden. The second loop winds up calling the method for each Thread in the array. You are actually calling it from the main thread though. So, as previously advised, I think that you will run each method sequentially.
    The direction that I would head with this is to override run() in your agent Thread and have that run() method call the MiniConflicts() method. Then, in your main routine you can do away with the second for loop. The looping start will start all of the individual threads and they will execute the run() method......which in essence will be executing your MiniConflicts method.
    This reply is NOT a total design... I am sure that there are holes to be filled... but I think that will get you headed in the right direction.

  • Several of us have a iPhone 6s and are having trouble with hearing and speaking at the same time when using the phone. ? The speakers seem to be too far apart? Are others having this issue? Solutions?

    Several of us have a iPhone 6plus and are having trouble with hearing and speaking at the same time when using the phone. ? The speakers seem to be too far apart? Are others having this issue? Solutions?

    Not having the problem, don't personally know anyone who is.

  • Where are the Java API classes?

    Hello All,
    I understand that Oracle9i uses a Java API different than those older classes found in sdoapi.zip here on the OTN.
    Problem is, I don't know where to find them in my newly-installed 9iR2.
    Can anyone help me with this simple question or point me to appropriate documentation?
    Thanks!
    -- john

    I believe the new Java API classes are slated for 10i.

  • For the past month, I've had an echo of my voice while I'm talking on my phone.  It's very annoying and sounds like multiple people are speaking at the same time and it's only me.  Anyone know what's wrong?

    For the past month, I've had an echo of my voice while I'm talking on my phone.  It's very annoying and sounds like multiple people are speaking at the same time and it's only me.  Anyone know what's wrong?

    For the past month, I've had an echo of my voice while I'm talking on my phone.  It's very annoying and sounds like multiple people are speaking at the same time and it's only me.  Anyone know what's wrong?

  • Hello !! Because if I talk daily to the dame Person for FaceTime and two are always in the same place, once day face time no prolem speak for another day and I have to use skype for error appearing face time ? Thanks for your answers.

    Hello !! Because if I talk daily to the same Person for FaceTime and two are always in the same place, once day face time no prolem speak for another day and I have to use skype for error appearing face time ? Thanks for your answers.

    Using FaceTime http://support.apple.com/kb/ht4319
    Troubleshooting FaceTime http://support.apple.com/kb/TS3367
    The Complete Guide to FaceTime: Set-up, Use, and Troubleshooting Problems
    http://tinyurl.com/32drz3d
     Cheers, Tom

Maybe you are looking for

  • ITunes creates corrupted backup after updating to iOS 8.3

    So this happened right after I updated my iPhone 5s to iOS 8.3. I got an "iTunes could not backup the iPhone because the backup was corrupt or not compatible with the iPhone" error. I've followed instructions to update iTunes, restart the program, co

  • How to embed image in html when using JTextPane, HTMLEditorKit, JavaMail

    I am developing a specialized e-mail client that has to be able to send e-mail messages (in html format with images and attachments) that can be read by standard e-mail clients, and receive similar messages from standard e-mail clients. I have everyt

  • Best practice for ConcurrentHashMap use?

    Hi All, would the following be considered "best practice", or is there a better way of doing the same thing? The requirement is to have a single unique "Handler" object for each Key: public class HandlerManager {     private Object lock = new Object(

  • Itunes won't open i tried everything

    help i have tried every thing even taking down the fire wall like the troubleshooting guides told me to please help

  • Marketing Part Number

    where i find Marketing Part Number ?