|
-
Jul 19th, 2005, 07:19 PM
#1
Thread Starter
Dazed Member
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.
Last edited by Dilenger4; Jul 26th, 2005 at 04:52 PM.
Reason: Resolved
-
Jul 19th, 2005, 09:11 PM
#2
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.
-
Jul 19th, 2005, 09:46 PM
#3
Thread Starter
Dazed Member
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){}
}
Last edited by Dilenger4; Jul 19th, 2005 at 09:52 PM.
-
Jul 20th, 2005, 06:43 AM
#4
Frenzied Member
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.
-
Jul 20th, 2005, 04:44 PM
#5
Thread Starter
Dazed Member
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?
-
Jul 20th, 2005, 05:19 PM
#6
Frenzied Member
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!
-
Jul 20th, 2005, 05:29 PM
#7
Thread Starter
Dazed Member
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.
-
Jul 20th, 2005, 05:34 PM
#8
Thread Starter
Dazed Member
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());
-
Jul 20th, 2005, 05:47 PM
#9
Frenzied Member
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?
-
Jul 20th, 2005, 05:48 PM
#10
Frenzied Member
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?
-
Jul 20th, 2005, 06:08 PM
#11
Thread Starter
Dazed Member
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());
-
Jul 24th, 2005, 11:07 PM
#12
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.
-
Jul 26th, 2005, 08:17 AM
#13
Thread Starter
Dazed Member
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.
-
Jul 26th, 2005, 07:31 PM
#14
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|