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. :sick:
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. :)
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){}
}
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.
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?
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!
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.
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());
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?
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?
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());
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(). :D
Just a thought.
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. ;)
Re: Index value of Component that fired an event?