Results 1 to 14 of 14

Thread: Index value of Component that fired an event?

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Resolved Index value of Component that fired an event?

    What i need is a way to find out which JButton from a JButton[] fired off an event and depending on which JButton it was associate that button with an int.
    I need to associate the JButtons with integers so i can use the ints as positions to add and remove Components from a Containter using remove(Component comp).

    It would be easy if the Container class had a method that returned an index of which Component within it fired off an event.

  2. #2
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Index value of Component that fired an event?

    you could extend that component and add some kind of index to it through a function.

  3. #3

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Index value of Component that fired an event?

    Im trying to keep it
    Code:
    class A{
      JButton[] jb = {new JButton("Hold")};
      jb.[0].addActionListener(new B()); 
    }
    
    class B{
     // ref to A
    }
    
    class C implements ActionListener{
     public void actionPerformed(ActionEvent evt){}
    }

  4. #4
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Index value of Component that fired an event?

    Maybe a loop?

    Code:
    int index = 0;
    
    for (int i=0; i<buttons.length; i++)
    {
    	if (ae.getSource() == buttons[i])
    	{
    		index = i;
    	}
    }
    Or maybe I don't understand the question.

  5. #5

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Index value of Component that fired an event?

    Ok say I have five JButtons all in say a JPanel. Now I know you can get which button fired off an event using getSource() but what I need is a way to find out which button fired off an event out of a group but in int form. The only possible way I can see to do this is at the Container level. But the Container class dosen't offer a way to find out which Component fired off the event. Dosent Visual Basic have something similar like Control Arrays?

  6. #6
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Index value of Component that fired an event?

    I see what you're saying now, but I think your just complicating things too much. I don't think you would need to do all of that junk when it's in an array. The buttons(since in an array) will already have an index value, and a simple variable can be used to hold the index of the button selected in a loop(using getSource()).

    Now, maybe you want to assign a value to these buttons when an event is fired? If that's the case, couldn't you have an array of ints the same size of the button array and change values at index's parallel to the button and int array?

    I really think you're trying to complicate this more than it has to be, or I just don't understand. We need CB here to help us out! Calling CB!

  7. #7

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Index value of Component that fired an event?

    Actually i am trying to simplify the process by looking for existing functionality instead of trying to come up with a work around.

  8. #8

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Index value of Component that fired an event?

    I could do the following. But then i would have to name the JButtons "1" to "5" instead of "Hold".
    Code:
    int i = Integer.parseInt(evt.getSource());

  9. #9
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Index value of Component that fired an event?

    Ok, sorry. Now I'm understanding. What about an ArrayList? You could use the indexOf() method to return the index.

    Maybe this?

    Code:
    int index = ArrayList.indexOf((Object)evt.getSource());
    That might not work, can't test it right now. That's assuming you want the int to be associated with the button to be it's index in the array. Is that what your looking for?

  10. #10
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Index value of Component that fired an event?

    Wait, that would just mess things up worse storing buttons in an arrayList I guess. Maybe the JButton inherits the indexOf method?

  11. #11

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Index value of Component that fired an event?

    I could do that but since indexOf(Obj element) is not a static method i would have to pass an instance of the ArrayList that holds the JButtons to the class that implements the ActionListener interface. Not much work but a little more then int i = Integer.parseInt(evt.getSource());

  12. #12
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Index value of Component that fired an event?

    You could perhaps set the objects ActionCommand into the index you want.

    so then use.. getActionCommand().

    Just a thought.

  13. #13

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Index value of Component that fired an event?

    Correction: I could do that but since indexOf(Obj element) is not a static method i would have to pass an instance of the ArrayList that holds the JButtons to the class that implements the ActionListener interface. Not much work but a little more then int i = Integer.parseInt(evt.getActionCommand());

    oceanebelle seems we were on the same page.

  14. #14
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Index value of Component that fired an event?


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width