Search code examples
javaswinguser-interfacejtableobservers

Handling events in multiple windows


so basically I need to have 2 windows and in the first one I have (an initially empty) JList and in the 2nd one I have a JTable. Once I double click on an item from the JTable it needs to add a corresponding item to the JList (I am not yet sure what this will be, perhaps the String value of the first cell in the row in which I double-clicked). Furthermore, if I ever have any items in my JList then their corresponding values in the table must be highlighted in a different colour, so the row in which I double-clicked should be set to red. Additionally, if I remove an item from the JList at some point, it should change the colour of the corresponding row in the table back to black.

I am just wondering if somebody could give me advice on the best approach to design an application like this and in particular (if nobody can suggest anything else) would it be more effiecient to use observers, rather than some getter methods? I have not yet tried working with observers but if they are a better choice in this case then I'm happy to try and learn.

Thanks


Solution

  • You are going to have to use observers when you create listeners to listen for these click events. Try to write code that listens for the relevant events from one Swing component, and setting the corresponding changes to the other Swing component. You can probably also when you remove an item from the JList make the changes to the Table without having to listen for the add event from the JList (not sure if there even is one).