Actionlistener - for multiple buttons with similar functionality

Hi, all:
I'm working on a calculator program and want to know how to go about making a generic ActionListener for all the functions (i.e. +,-,*,/) so that I don't have to write separate inner classes for each function (i.e. + button, - button, etc.).
I think this generic class should be created within a method that takes parameters. The parameters are then used within the inner class.
What I need to know specifically is whether it's possible to pass a string operator symbol (e.g. "+" or "-") as a parameter in the following context:
class buttonListener implements ActionListener
public void actionPerformed(ActionEvent e)
intField1Val + intFieldVal2;
in this case, the "+" would be the parameter that would change depending on the values provided to the method.
Can this be done??
I'd also appreciate any other feedback you can give m,e on this problem (i.e. how to set up the method...basic structure??)
Thanks!!

Hi there,
One way to do this is as follows:
Button plus = new Button("+");
Button minus = new Button("-");
public void actionPerformed(ActionEvent e) {
String componentHit = e.getActionCommand();
if (componentHit == "+") {
doAddMeth(); // implemented elsewhere
else
if (componentHit == "-") {
doSubMeth(); // implemented elsewhere
~Bill

Similar Messages

  • Simplification Needed: Multiple buttons with similar execution

    Hello,
    A vi I am currently working on needs many buttons on the front panel. However, the execution for each is quite simple and similar all throughout.The following figure shows the buttons on the left and right side of the Case Structure. Inside the Case Structure, the same commands remain essentially the same except with the change of a number as the input of the Zaber Write vi (in this case it is a 2). Does anyone have a better idea for me to execute these functions in a more compact way? 
    Thank you.
    Alfredo
    Solved!
    Go to Solution.

    It depends if your buttons are switch action (any combination of buttons can be true: 2^N possible cases)
    or latch action (only zero or one can be true: N+1 possible outcomes).
    Here is code for both cases as shown.
    (If you want some weird, nonconsecutive case numbers, you can index into a lookup table before going to the case structure.)
    Overall, your code shows some very odd programming style that makes little sense. Can you attach the actual program?
    Message Edited by altenbach on 10-17-2009 02:29 AM
    LabVIEW Champion . Do more with less code and in less time .
    Attachments:
    multiBooleans.png ‏12 KB

  • I don't use Photoshop - looking for software with similar functionality to Photoshop's Pixelbender / Oil Painting.  Any suggestions?

    I don't use Photoshop, I'm an Aperture guy.
    I'm looking for software with similar functionality to Photoshop's Pixelbender / Oil Painting. 
    I'm especially interested in the bending of the pixels, along the lines of impressionist paintings - think van Gogh.
    Any suggestions?

    If you are interested in computer simulated drawing and painting effects, have a look at the "ToonIt!" plug-in for Aperture. This will do some of the basic work for you, but you will have to do quite a lot of editing afterwards, to turn the image into a piece of art.
    It might be easier, to do most of the drwing and painting yourself in the first place - ArtRage Deluxe is very versatile, if you have a large trackpad and pen.
    For example: Applying a light ToonIt! effect "Impressionist2": The diagonal
    cross-pattern is due to "Demo" mode - I have not yet bought the plug-in - it is rather expensive:
    The original:

  • Two button with single function

    I want to create two buttons for single function and one button for one function. For example, I click button1, the screen will show "clicked1"
    If I clicked button2, the screen will show "clicked2". If I click button1 and then click button2, it will show "two button are clicked". However, it does not work in two button with single function. What is the problem? Here is the code.
    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    var button1:MovieClip=new Button1();
    button1.x=200;
    button1.y=200;
    addChild(button1);
    var button2:MovieClip=new Button2();
    button2.x=300;
    button2.y=250;
    addChild(button2);
    button1.addEventListener(MouseEvent.CLICK, onClick);
    button2.addEventListener(MouseEvent.CLICK, onClick2);
    var isClicked:Boolean;
    var isClicked1:Boolean;
    function onClick(evt:MouseEvent):void{
              isClicked=true;
              trace("clicked1");
    function onClick2(evt:MouseEvent):void{
              isClicked1=true;
              trace("clicked2");
    if (isClicked&&isClicked1){
              trace("two buttons are clicked");

    It works but I want the screen only shows the "isClicked" only when I click button1. But now when I click button1, "isClicked" and "two buttons are clicked" show at the same time.
    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    var button1:MovieClip=new Button1();
    button1.x=200;
    button1.y=200;
    addChild(button1);
    var button2:MovieClip=new Button2();
    button2.x=300;
    button2.y=250;
    addChild(button2);
    var isClicked:Boolean;
    var isClicked1:Boolean;
    button1.addEventListener(MouseEvent.CLICK, onClick);
    button2.addEventListener(MouseEvent.CLICK, onClick2);
    button1.addEventListener(MouseEvent.CLICK, onClick3);
    button2.addEventListener(MouseEvent.CLICK, onClick3);
    function onClick(evt:MouseEvent):void{
              isClicked=true;
              trace("clicked1");
    function onClick2(evt:MouseEvent):void{
              isClicked1=true;
              trace("clicked2");
    function onClick3(evt:MouseEvent):void{
              trace("two buttons are clicked");

  • Single invoice for multiple orders with different divisions

    Dear All,
    while I am trying to do single invoice for muliple orders with different divisons invoice is gettting splitted..In copy control vbrk routine I  tried with 003 and 007 etc..but result is same..what configuration need to be done for getting the single invoice
    Thanks In advance
    Regards
    CNRNAIR

    Hello
    single invoice for multiple orders with "different divisions" invoice is getting split
    FYI - Different Division means different Sales Area.
    So, sales doc (whether sales order, Delivery & Billing) is applicable to EXACTLY one sales area.
    Thus, combining docs with different sales area is practically impossible.
    Provided, you are using common division.
    Hope this assist you,
    Thanks & REgards
    JP

  • Is it possible to search for multiple folders with the same name and...

    Is it possible to search for multiple folders with the same name and then select them all and change the permissions on just those folders .i.e. Search for the budget folders in all client folders and lock them down to just the project managers. Without having to go to each folder and apply the permissions.

    user11919409 wrote:
    Is it possible to create a Clone database with the same name of source db using RMAN ...
    yes
    >
    DB version is 11.2.0.2
    Is it possible to clone a 11.2.0.2 database to 11.2.0.3 home location directly on a new server . If it starts in a upgrade mode , it is ok ....yes
    Handle:     user11919409
    Status Level:     Newbie (10)
    Registered:     Dec 7, 2009
    Total Posts:     102
    Total Questions:     28 (22 unresolved)
    why do you waste time here when you rarely get any answers to your questions?

  • Create a CR for multiple processing with me as the current processor

    Hi
    How to create a change request for multiple processing with myself as the current processor.
    I have created a change request for multiple processing and the ystem throws me the error that "There are no suitable change requests"
    Display help shows the below
    For the upload you need a change request that has the status Considered
    and Approved or To Be Revised and ideally has a type that does
    not have the property Object List Is Binding. If the
    change request does have this property, all the entities that are uploaded must
    already be assigned to this object list.
    Please can someoen guide me on how to fix this.
    I found few threads related to this but cudint understand on how to assign a status to CR
    Thanks,
    Sharma

    Hi Prateek,
    Please can anyone give any pointers on how to set the status of change request (for multiple processing):-
    For assigning the statuses to the change request:-
    Go to t code:- MDGIMG->General settings>Process Modeling>Change Request>Edit status of change request.
    the path you mentioned is to create and edit the existing Change request statuses, what I am looking for is when I create a CR for Multi record processing and try to upload a file I get the error "There are no suitable change requests". and when I check the Status of the change request I created I see that the status is Changes to be executed (Set automatically).
    how can i set myself as the processor.
    tcode: MDGIMG->General settings>Process Modeling>Change Request>Create Change request type. Ucheck the single object checkbox and assign your userid for that CR step.
    Single object is already Unchecked  and I am using BRF+ so when I am the processor First approver is say X, I am not sure if we have any place where we can set ourselves as the user. Please let me know if I missed sumting
    Thanks
    Hi Shephalika,
    Yes I am trying to upload a file and Single object is Unchecked in the Change request type.
    Thanks,
    Sharma

  • How to config a shared cache for multiple environments with C API

    How to config a shared cache for multiple environments with C API?  Just like Java edition. Chapter 2. Database Environments
    I want to open large number of databases, at least 10,000. But as the counts of databases opened increase, the db->open operation become very slow. It almost cost 2 hours to 10,000 databases.
    So I try to distribute these databases to multiple environments ( for example, 5 envs ). And in order to improve the efficient of memory use, I want to share cache between envs.

    Hi,
    It is not clear what you meaning about multiple environments. Do you mean these environments are in different directories or  in the same directory ? If you mean environments in different dirs share the same cache, it is interesting why you need that.
    If you do not use DB_PRIVATE to open the environment, the created cache will be on disk, in the environment directory, so it can be shared by multiple processes and multiple threads. Currently, the cache file is in the environment directory, and we do not support specifying a separate directory for cache only.
    Regards,
    Oracle Berkeley DB.

  • One function for multiple buttons?

    I have a county map on my timeline with a list of each county as buttons ( there is 88 counties). I want to highlight the county on the map when I click the county's name from a list.
    when I test this out, only the last one gets highlighted. I know I am missing something really simple. it does work if I write code for each button but I can't to seem to get it to work with only writing the function once for all buttons
    var countyHigh:MovieClip = adamsHighlight;
    var countyCheck:MovieClip = kickout.adamsCM;
    kickout.adamsCheck_btn.addEventListener(MouseEvent.CLICK, highlightCounty);
    kickout.allenCheck_btn.addEventListener(MouseEvent.CLICK, highlightCounty);
      countyHigh = allenHighlight;
      countyCheck = kickout.allenCM;
    kickout.ashlandCheck_btn.addEventListener(MouseEvent.CLICK, highlightCounty);
      countyHigh = ashlandHighlight;
      countyCheck = kickout.ashlandCM;
    function highlightCounty(event:Event):void{
      if   (countyHigh.alpha == 0)
           countyHigh.alpha = .5;
      else  countyHigh.alpha = 0;
       if(countyCheck.alpha == 0)
       countyCheck.alpha = .5;
      else
      countyCheck.alpha = 0;
    when I click all there buttons, the all highlight ashland county instead of their own county. I am using Flash Professional CS5 and only been playing with Flash for about a year.

    var previouslyHighlightedCounty:Movieclip;
    function highlightCounty(e:MouseEvent):void{
    if(previouslyHighlightedCounty){
    //reset previouslyHighlightedCounty. ie, un-highlight
    // highlight e.currentTarget
    e.currentTarget=previouslyHighlightedCounty

  • I am writting a test  using a sequencer to test for multiple button click

    I'm using flex sdk 4.1 and have a situation where a  button click is sometimes called twice. I am writting a test for this using a sequencer and it will run for at least one button click, what I am not sure is how to test for multiple click events(which shouldn't happen, but occours randomly in my project)
    package tests.view
        import flash.events.MouseEvent;
        import flexunit.framework.Assert;
        import mx.events.FlexEvent;
        import org.flexunit.async.Async;
        import org.fluint.sequence.SequenceEventDispatcher;
        import org.fluint.sequence.SequenceRunner;
        import org.fluint.sequence.SequenceWaiter;
        import org.fluint.uiImpersonation.UIImpersonator;
        import views.AddComments;
        public class TestAddComments
            private var view:AddComments;
            [Before(async,ui)]
            public function setUp() : void
                view = new AddComments;
                Async.proceedOnEvent( this, view, FlexEvent.CREATION_COMPLETE, 600 );
                UIImpersonator.addChild( view );
            [Test]
            public function testButtonClick():void
                view.addCommentBtn
                var sequence:SequenceRunner = new SequenceRunner( this );
                sequence.addStep( new SequenceEventDispatcher( view.addCommentBtn, new MouseEvent( 'click', true, false ) ) );
                sequence.addStep( new SequenceWaiter( view.addCommentBtn, MouseEvent.CLICK, 100 ) );
                sequence.run();

    The only way you could test for that type of circumstance... one where something may happen... is to do so with a timeout. In other words, you cannot let the test pass simply because the first event occurred.
    Two ways to handle this... with sequences:
    Instead, you write a test that expects 2 clicks. In the assertion handler (which would only be reached if both clicks occur) you would fail the test:
    fail('i shouldnt be here');
    In the Waiter for the 2nd click, you would specify a timeout handler. That timeout handler will get called in the 2nd click does not occur in the specified time... (finding that time is the hard part). The default timeout handler fails the test, however, in your case, your timeout handler would just happily do any assertions needed and move on. A timeout does not necessarily mean failure... in your case, it is correct
    Second choice:
    You could create a timer. Create an event handler for the button click which increments a counter. Start the timer, do your test. However, have your test pending on the timer's completion rather than something like the click.. In that case, you would then be able to just check the count:
    private var clickCount:int = 0;
    [Test]
    public function testButtonClick():void {
         var timer:Timer = new Timer( 100, 1 );
        Async.handleEvent( this, timer, TimerEvent.COMPLETE, checkClickCount );
         view.addCommentBtn.addEventListener( 'click', function( e:Event ):void { clickCount++ } );
         view.addCommentBtn.dispatchEvent( new MouseEvent( 'click;, true, false ) );
    private function checkClickCount( e:Event ):void {
    assertEquals( 1, clickCount );
    However, I can't imagine how the test you are describing could ever yield more than one click event... I am guessing the problem is elsewhere, but this will help you verify
    Mike

  • How to use the same script for multiple buttons

    Hi,
    I've only just started using flash so any help would be great!
    I'm creating a blockbusters type game, I have a grid of 20 buttons and I need  them (individually) to turn blue on click and red on double click. I've managed to do it with the first one using this code;
    var clicked:Boolean = false;
    bn1.addEventListener(MouseEvent.CLICK, bn1click);
    function bn1click(event:MouseEvent):void {
        clicked = true;
        var newColorTransform:ColorTransform = bn1.transform.colorTransform;
        if(clicked){
        newColorTransform.color = 0x064258;
        bn1.transform.colorTransform = newColorTransform;
    bn1.doubleClickEnabled = true;
    var doubleclicked:Boolean = false;
    bn1.addEventListener(MouseEvent.DOUBLE_CLICK, bn1dclick);
    function bn1dclick(event:MouseEvent):void {
        doubleclicked = true;
        var newColorTransform:ColorTransform = bn1.transform.colorTransform;
        if(clicked){
        newColorTransform.color = 0xac1e23;
        bn1.transform.colorTransform = newColorTransform;
    Now I'm having trouble getting the same to work for the rest of the buttons, they are each named bn2, bn3 etc. They need to work individually and remain blue/red once clicked. I've tried listing them as addEventListener commands but not having a great deal of success!
    Any help would be greatly appreciated, thanks!
    Tomo

    One way to do this is to use arrays to keep track of the buttons and their properties.
    var buttonList:Array = new Array(bn1,bn2,bn3);
    var clickedList:Array = new Array();
    var doubleClickedList:Array = new Array();
    //then use a for loop to assign the functions and properties for each button:
    var thisMany:int = buttonList.length; // this will give you the number of items in the buttonList array
    for(var i:int = 0; i<thisMany; i++) {
         buttonList[i].addEventListener(MouseEvent.CLICK,btnClick); // assign the click function to each button
         buttonList[i].addEventListener(MouseEvent.DOUBLE_CLICK,btnDClick); // assign the double click function
         clickedList.push(false);  // add a false value for each button to this array
         doubleClickedList.push(false);
               buttonList[i].doubleClickEnabled = true; // set the double click property for each button
    function btnClick(event:MouseEvent):void {
              var thisButton:int = buttonList.indexOf(event.target);  // figure out which button was clicked as an item in the array
              clickedList[thisButton] = true;  // change the value in the array
               var newColorTransform:ColorTransform = buttonList[thisButton].transform.colorTransform;
        if(clickedList[thisButton]){
        newColorTransform.color = 0x064258;
        buttonList[thisButton].transform.colorTransform = newColorTransform;
    function btnDClick(event:MouseEvent):void {
              var thisButton:int = buttonList.indexOf(event.target);
              doubleClickedList[thisButton] = true;
        var newColorTransform:ColorTransform = buttonList[thisButton].transform.colorTransform;
        if(doubleClickedList[thisButton]){
        newColorTransform.color = 0xac1e23;
        buttonList[thisButton].transform.colorTransform = newColorTransform;
    Now you can have any number of buttons, just add their instance names to the array at the top.

  • Single ERS Invoice for multiple POs with different GS and same IP

    Dear All,
    I have a Requirement as per the below scenario:
    We need to have single invoice created using ERS (MRRL) functionality in the below scenario:
    Two POs created with different Partner function GS (goods supplier) and same IP (Invoice party partner function).
    Ex:
    GS IP PO GR ERS
    vendor1 10001 4500001 500001 Yes ( GR-IV-Yes, GR done- Yes, same payment terms - 001)
    vendor2 10001 4500002 500002 Yes ( GR-IV-Yes, GR done- Yes, same payment terms - 001)
    When I run ERS for last 1 week (GR) and select the option (Doc selection – per IP or other criteria like GRs posted in last one week) , system should create 1 invoice document. Standard SAP posts 2 documents for each GS.
    I have tried using 2 user exits for MRRL, but none of them work for this functionality.
    Thanks,

    Hi,
    Go for  MIRO without MRRL.
    Instead ERS, you can post single  Invoice(MIRO) for multiple POs  for one vendor.
    OR
    Check with Technical consultant and go for development to have single ERS output for multiple ERS invoice documents for single vendor.
    Regards,
    Biju K

  • How to search for multiple values with Bex Prompts

    Hi,
    I would like to know  if it is possible to search for multiple values at the same time with Bex Prompts instead of searching one value at a time (please see the screen shot below). I searched the forum couldn't find any relevant answers. please let me know  how  to achieve?
    Thanks,
    Charvi.

    Hi Charvi,
    You can use wild card search to ensure you get multiple similar values listed as search output.use * for multiple characters and ? for single character.
    For Example, Ravi * would result you all employees with first name as Ravi
    You can use various search formats such as * Ravi * and * Ravi and Ra?? etc.
    Thanks
    Mallik

  • Adding a button with zoom function

    Hello,
    I am working on a product catalog and for this purpose we have used a professional photographer to shoot product photos. The photos are really nice and they can be zoomed in to see every detail clearly. The photos have been inserted into the pdf catalog but because of the limited space they are not very detailed.
    Is it possible to add a button or link with zoom function? For example, an icon such as a magnifier or just the word zoom so when a user clicks it, it will zoom in the pre set area to a pre set level and center it.
    Sounds quite fancy right? Maybe i'm thinking the impossible... but it's worth a try here :)
    Regards,
    Rudi

    ok. i got it! :))
    i have made some changes myself. When the mouse enters button1, button2 becomes visible and button1 becomes hidden (otherwise it is overlapping button2). I have set this in the actions of button1. I would also add the following function: when visible button2 is clicked it hides button2 (this you have already shown and works) and makes button1 visible again. I guess making button1 visible again has to be done with a Javascript in the actions of button2 under mouse up.
    I have no knowledge of programming. Can you tell me how to do this?
    After i openen the pdf file and used this function and close the pdf file it asks to save the pdf file. How can i prevent this to show up? I dont want this to happen to my customers.
    Is it possible to see the exact dimensions of the button? Then i could create both buttons so that they fit in my catalog exactly as i want them, and then check the dimensions and scale my photos to it. Although it seems that Acrobat does something with the photo size already as the photo in button2 is 34mb while the pdf file is only 200kb.
    thanks.
    rudi
    BTW this is really a well thought out solution and i think not many people are aware of this because i have never seen it before

  • T codes with similar functions in AP and AR process

    Hi,
    I need  list of T codes which has similar functions in entire AP and AR process.
    Thanks
    SM

    Hi,
      From the menu  Extras > Settings >   Tick "Show Technical Names".
    The look through the standard menu for AP & AR . Items with similar descriptions = Similar T-Code.
    regards

Maybe you are looking for