0

Is there a standard way of notifying parent windows of occurrences in child windows? I have a jFrame with a lot of controls in it, including a custom control I've created myself which is a jList with checkboxes for each list item. The jList has listeners for mouse and keyboard so that it can change the checked status of list items when the user mouse clicks or hits space while they are selected.

I want the parent window to know when the checked status of a list item changes, so that it can refresh the contents of another window. But I can't work out how to send the notification of the change up to the parent window.

Bud
  • 709
  • 2
  • 11
  • 28

1 Answers1

1

Have a look at Observer Pattern and How to Write a List Selection Listener.

You might also be better off using a modal dialog, see How to Make Dialogs for more details, which will block the code execution at the point that the dialog is made visible until the dialog is closed, at which point you could request information about what has changed (from the dialog) and apply those changes as need

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thanks. The problem is that since this is a custom control, the listener for it is outside of the main file, in its own java file. The code in that file is correctly listening for all the events on the jList. The question is how, once it receives a notification of such an event, can it notify its parent of that event. – Bud Sep 11 '15 at 02:40
  • So are most listeners. You need to devise your own listener pattern, based on the events you want the component to generate – MadProgrammer Sep 11 '15 at 02:46
  • I don't even understand what that means. What does "own listener pattern" mean? I have the listener, and it's capturing the right events. But how do I notify the parent? – Bud Sep 11 '15 at 04:41
  • No, you need to make you're own listener interface which exposes those events you want other people to be notified about – MadProgrammer Sep 11 '15 at 04:46
  • Thanks, but that doesn't really help. Maybe I'm dumb, but what you are saying doesn't tell me how to do whatever it is I need to do. I have the existing listeners - telling me to make my own doesn't help me. I don't know what it should look like or what it should do. I don't even know what to search for on Google for this. – Bud Sep 11 '15 at 15:18
  • *"I want the parent window to know when the checked status of a list item changes"* - So write a listener interface, may which extends from `EventListener`, which describes what events your component will generate. Any interested parties would implement this interface and register them selves to your component. When your component's state changes, you simply call each registered listener and notify them of the events which have occurred. This is a very common pattern in Swing and the web resounds with a multitude of examples. – MadProgrammer Sep 13 '15 at 04:02
  • Using one of the pre-existing listeners isn't beyond you should it meet your needs. But maybe [Creating Custom Listeners In Java](http://stackoverflow.com/questions/10375178/creating-custom-listeners-in-java) or [Create a custom event in Java](http://stackoverflow.com/questions/6270132/create-a-custom-event-in-java) or [Events and listeners](http://www.javaworld.com/article/2077351/java-se/events-and-listeners.html) can help. You question doesn't have enough detail for me to provide you with more then an abstract idea of what you need to do. – MadProgrammer Sep 13 '15 at 04:05