Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Calling the actionlistener class depending on variables

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Calling the actionlistener class depending on variables

    Hi there, I'm new to java and new to the forums. I find the best way to learn a programming language is to get coding a project you like and start from there so sorry if this question is a really basic concept (I despise reading books thought I'll look at documentation and tutorials for concepts I'm stuck on ).

    So now onto the question and not my life story I'm fairly bad at explaining so I'll do it with code;


        private void styleButtons(JButton... buttons){
            for ( int i=0; i < buttons.length; i++ )
            {
                buttons[i].setBounds(120,10 * (i * 5) + 15, 130, 30);
     
                buttons[i].addActionListener(new ButtonListener1());
            }
        }

    I have a button called BtnExit and BtnStart I would like it so during the loop it would assign the class BtnExitButtonListener to BtnExit and BtnStartButtonListener to Btn start and so on (hope you get the idea now).

    feel free to correct me over my use of technical words and any advice would be really great even if its "your doing it the wrong way".
    Last edited by zidsal; January 4th, 2011 at 06:23 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Calling the actionlistener class depending on variables

    How will you distinguish buttonStart and buttonEnd from the other buttons? Is the first button passed in buttonStart and the last one buttonEnd (or some variant of this), or is there some other method you're using?

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Calling the actionlistener class depending on variables

    Quote Originally Posted by helloworld922 View Post
    How will you distinguish buttonStart and buttonEnd from the other buttons? Is the first button passed in buttonStart and the last one buttonEnd (or some variant of this), or is there some other method you're using?
    I define my variables, and then I pass my buttons through the method styleButtons, its rather a lot of buttons so I wanted to make a loop to style all the buttons rather then me repeating the code over and over again.

    edit I see what you mean I think
    in the parameters style button I call it like this addButtons(ScreenManager.BtnStart, ScreenManager.BtnEnd, ScreenManager.BtnOptions);
    and thats how the code distinguishes what button is what during the loop i.e button[i] = BtnStart/BtnEnd/BtnOptions depending on the value in the loop.
    Last edited by zidsal; January 5th, 2011 at 06:59 AM.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,607
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Calling the actionlistener class depending on variables

    The easiest solution is to add your ActionListeners to the JButtons separately, instead of from your style method.

    The style method contains functions you want to perform on every JButton. You don't want to add the same ActionListener to every JButton. So don't do it in that function.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Calling method from .class file
    By alexx_88 in forum Java Theory & Questions
    Replies: 6
    Last Post: April 24th, 2012, 03:14 AM
  2. interface class ActionListener
    By NightFire91 in forum Java Theory & Questions
    Replies: 2
    Last Post: August 17th, 2010, 11:42 AM
  3. Construct a class that implement ActionListener with no constructor
    By striko_514 in forum Java Theory & Questions
    Replies: 1
    Last Post: July 5th, 2010, 04:15 PM
  4. Help Calling Method From Another Class
    By CheekySpoon in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 15th, 2010, 10:24 AM
  5. [SOLVED] How to call string in another class in java?
    By tazjaime in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 23rd, 2009, 10:31 AM