Results 1 to 10 of 10
- 03-15-2012, 01:12 AM #1
Member
- Join Date
- Jan 2012
- Posts
- 45
- Rep Power
- 0
Logic fart in my brain, identify each cycle of while
As I said I'm having a major brain fart in the logic department. I'm adding event listeners to table models with a while loop, but when the listeners are attached, I find no way to identify which listener has been activated :D
Here's my code:
Java Code:DefaultTableModel bodyModel =(DefaultTableModel)bm1.getModel(); DefaultTableModel neckModel =(DefaultTableModel)bm2.getModel(); DefaultTableModel fretboardModel =(DefaultTableModel)bm3.getModel(); DefaultTableModel[]modelsArray = new DefaultTableModel[3]; modelsArray[0] = bodyModel; modelsArray[1] = neckModel; modelsArray[2] = fretboardModel; listenerCounter = 0; while(listenerCounter < modelsArray.length){ modelsArray[listenerCounter].addTableModelListener(new TableModelListener() { public void tableChanged(TableModelEvent e){ tableModelTableChanged(e, ------------------ something to identify which listener has been activated-------------------------); } }); listenerCounter++; } } public void tableModelTableChanged(TableModelEvent evt, int tableIdentifier){ //updateTableCell(evt.getFirstRow(), evt.getColumn(), tableIdentifier); System.out.println("this is Table number " + tableIdentifier + " reporting in!"); }
- 03-15-2012, 01:14 AM #2
Re: Logic fart in my brain, identify each cycle of while
What does it mean to "activate" a listener?identify which listener has been activated
- 03-15-2012, 01:21 AM #3
Member
- Join Date
- Jan 2012
- Posts
- 45
- Rep Power
- 0
Re: Logic fart in my brain, identify each cycle of while
I mean which table had the event tableChanged happen to it. I have 3 tables, I need to know which table contains the changed value.
In other words, if the table at neckModel had a value changed, the eventListener should pass a parameter to tableModelTableChanged to tell which table it was.Last edited by MonkeyMan; 03-15-2012 at 01:23 AM.
- 03-15-2012, 01:24 AM #4
Re: Logic fart in my brain, identify each cycle of while
Store a reference in the listener class when you create an instance of it.
- 03-15-2012, 01:49 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Re: Logic fart in my brain, identify each cycle of while
An event object (like TableModelEvent) contains a method to find out which object the event occurred on which might be helpful if you want to know which table is associated with the event. (In general, of course, there may be multiple tables that "contain" the value in question.)I need to know which table contains the changed value.
- 03-15-2012, 04:54 AM #6
Re: Logic fart in my brain, identify each cycle of while
Also, and not limited to Listener interfaces, you can't change the method signature by adding a parameter of your choosing when implementing an interface.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 03-15-2012, 03:27 PM #7
Member
- Join Date
- Jan 2012
- Posts
- 45
- Rep Power
- 0
Re: Logic fart in my brain, identify each cycle of while
Solved it, thank you pbrockway2 , with yours and Toll's tip in the other thread, I was able to do getSource() on the event object that handled the change event, and then I took the hash code off the source and compared it to all tables, identifying the correct table.
I'm sure there's a lot simpler and better way to do it, but it's the best I could come up with the tips given. Never used hash before either, so it's good :)
-
Re: Logic fart in my brain, identify each cycle of while
Were you trying to find which Table was associated with the activated listener or the table model that was associated with activated listener? And what were you going to to with this information once you got it?
- 03-15-2012, 03:41 PM #9
Member
- Join Date
- Jan 2012
- Posts
- 45
- Rep Power
- 0
Re: Logic fart in my brain, identify each cycle of while
Either one really. You see, I needed the information of which table has had data changed, so I can insert the correct mySQL table name in to the update statement. This way I managed to identify the table model, which is essentially the same as identifying the table itself, since all my tables use a different table model so it works like an id for the tables themselves. If you got a tip how to identify the table directly instead of the table model, I'm sure it would be useful in the future if I ever run in to a case where I can't use the table model as an identifier.
- 03-15-2012, 09:12 PM #10
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Re: Logic fart in my brain, identify each cycle of while
I don't have time to look it up, but I'm pretty sure that isn't possible. The event is a TableModelEvent and the library writers are pretty careful about how they name things. It will be an event that occurred in the model. But there can, in general, be multiple tables (views) of this data (model).If you got a tip how to identify the table directly instead of the table model...
One thing you could do is store the id in the table model. Then the logic would be getSource()-from-event->getId()-from-model->doStuff()-with-id.
The fact that you have a single (swing) table associated with the (database) table is something of an accident. The Swing table is a view and, who knows, you may later want to have multiple views of the same database table. (A filtered view, or a summary view etc).
Similar Threads
-
Racking my brain
By beauti477 in forum New To JavaReplies: 5Last Post: 07-22-2011, 08:17 AM -
How do I create an array with every cycle of a loop?
By blackhole8746 in forum New To JavaReplies: 3Last Post: 05-07-2008, 07:49 AM -
Servlet Life Cycle
By Java Tip in forum Java TipReplies: 0Last Post: 11-28-2007, 09:57 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks