Results 1 to 10 of 10
  1. #1
    MonkeyMan is offline Member
    Join Date
    Jan 2012
    Posts
    45
    Rep Power
    0

    Default 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!");
       }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Logic fart in my brain, identify each cycle of while

    identify which listener has been activated
    What does it mean to "activate" a listener?

  3. #3
    MonkeyMan is offline Member
    Join Date
    Jan 2012
    Posts
    45
    Rep Power
    0

    Default 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.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default 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.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Logic fart in my brain, identify each cycle of while

    I need to know which table contains the changed value.
    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.)

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default 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.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    MonkeyMan is offline Member
    Join Date
    Jan 2012
    Posts
    45
    Rep Power
    0

    Default 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 :)

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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?

  9. #9
    MonkeyMan is offline Member
    Join Date
    Jan 2012
    Posts
    45
    Rep Power
    0

    Default 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.

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Logic fart in my brain, identify each cycle of while

    If you got a tip how to identify the table directly instead of the table model...
    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).

    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

  1. Racking my brain
    By beauti477 in forum New To Java
    Replies: 5
    Last Post: 07-22-2011, 08:17 AM
  2. How do I create an array with every cycle of a loop?
    By blackhole8746 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 07:49 AM
  3. Servlet Life Cycle
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-28-2007, 09:57 AM

Posting Permissions

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