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 6 of 6

Thread: difference between action event from 2 buttons with same name?

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    9
    My Mood
    Depressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default difference between action event from 2 buttons with same name?

    first of all, I am sorry if I post it in the wrong sub-forum...but I guess this is a GUI problem..?I am not sure :<

    the code below will probably explain the problem I got, but my goal is to have 2(or more) exactly same button in a Panel and have different effect when I clicked it. The only difference between those buttons will be location on the screen, is there anyway I can extract that information AFTER I get an unknown action event from 1 of them?

    edit : The only thing I need to keep constant is the text on the button, the color and they need to use the same action listener, all other property is changable, if this info helps.

    for your information,I know I could 'solve' the problem by assigning unique action listener to each button to solve the problem, but that method will not fit into all my other code, unless I am willing to re-do 5000 lines of code...

    begging for a solution

    b = new JButton("Button");
    b.addActionListener(this);
    add(b);
    b = new JButton("Button");
    b.addActionListener(this);
    add(b);
    public void actionPerformed(ActionEvent e) {
        	JButton s = (JButton)e.getSource(); 
    //This line here will tell me I got a event from "Button", which 
    //I have no way to tell which button it is actually refering to
    }
    Last edited by nggdowt; November 7th, 2011 at 09:29 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,180
    My Mood
    Hungover
    Thanks
    141
    Thanked 602 Times in 517 Posts

    Default Re: difference between action event from 2 buttons with same name?

    Why are you doing it this way? Why are you not keeping track of each JButton? Why are you only using one ActionListener? Doing either of those things is fine, but doing both doesn't make sense. Either add a different ActionListener to each JButton, or keep track of the JButton instances so you can compare them to the source. There are probably other ways around this problem, but they'll probably be kludges.
    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!

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    9
    My Mood
    Depressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: difference between action event from 2 buttons with same name?

    right, if anyone wants to know why, I am generating buttons according to use input. I am scan an array for inputs, if the array is {a,a,a,b,a} my code will generate 5 buttons in the order of a,a,a,b,a. Since I can not predict the unknown input, thus all I can think of is something like scaning the array element by element and pass the input to a method which will generate different button according to input(as below)

    I simply cant think of anyway to assign unique action listener to unknow number of buttons and will function correctly, is there a way to create action listener with name that changes dynamicly?

    case Farm:
    				bFarm = new JButton("Farm");
     				bFarm.addActionListener(this);
     				add(bFarm);
     				if(!isCapture && !initDone){
     					bFarm.setBackground(new Color(0,119,0));
        			}else if(!isCapture && initDone){
     					bFarm.setBackground(new Color(0,119,0));
     					rp.FoodGrowth -= 5;
     				}else if(isCapture){
     					bHouse.setBackground(new Color(220,220,220));
     					rp.FoodGrowth += 5;
     				}
    			break;
    			case House:
    				bHouse = new JButton("House");
     				bHouse.addActionListener(this);
     				add(bHouse);
     				if(!isCapture && !initDone){
     					bHouse.setBackground(new Color(0,119,0));
        			}else if(!isCapture && initDone){
     					bHouse.setBackground(new Color(0,119,0));
     					rp.PopCap -= 5;
     				}else if(isCapture){
     					bHouse.setBackground(new Color(220,220,220));
     					rp.PopCap += 5;
     				}
    			break;
    Last edited by nggdowt; November 7th, 2011 at 08:31 AM.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,180
    My Mood
    Hungover
    Thanks
    141
    Thanked 602 Times in 517 Posts

    Default Re: difference between action event from 2 buttons with same name?

    That's fine, but then why are you only using one ActionListener? Give each button its own, that way you don't need to worry about checking the source. Anonymous inner classes might be useful here.
    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!

  5. The Following User Says Thank You to KevinWorkman For This Useful Post:

    nggdowt (November 7th, 2011)

  6. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    9
    My Mood
    Depressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: difference between action event from 2 buttons with same name?

    oh that's a great suggestion, I think I am getting there. that way I can create the exact amount of action listener according to user input you save my day *kiss*

    P.S. I am a dude, don't expect anything from that kiss lol

  7. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,180
    My Mood
    Hungover
    Thanks
    141
    Thanked 602 Times in 517 Posts

    Default Re: difference between action event from 2 buttons with same name?

    Quote Originally Posted by nggdowt View Post
    P.S. I am a dude, don't expect anything from that kiss lol
    That's okay, I take payment in the form of postcards from wherever you live. Eventually somebody from here will send me one!
    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. Replies: 9
    Last Post: June 29th, 2011, 12:27 PM
  2. Difference between jboss 4.x,5.x and 6.x
    By vairam in forum Member Introductions
    Replies: 1
    Last Post: May 3rd, 2011, 09:49 AM
  3. [SOLVED] Difference between == and equals.
    By goldest in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 18th, 2010, 02:15 PM
  4. jbutton and action event
    By mt888 in forum AWT / Java Swing
    Replies: 3
    Last Post: March 26th, 2010, 06:24 AM
  5. Need to know this difference
    By arvind in forum Java Theory & Questions
    Replies: 2
    Last Post: January 19th, 2010, 05:24 AM