I have a “MainWindow” with a table that shows all books in an overview. If I select an entry (or more than one) and click “Show selected”, new JFrame(s) are opened with the corresponding objects (“DetailView”). Since the “MainWindow” is still active, I’m able to open the same item twice (two “DetailView” for the same object).
I’d like to make this a singleton window depending on the object: if the same object is selected for the second time, I’d like to get the focus to the already opened JFrame.
I’m quite new to java, so this may be the wrong approach, and maybe already included in the Swing-Framework, though Google didn’t give me any hints.
You need some sort of “JFrame Registry”, a simple
Map<Object, JFrame>that allows looking up the correct JFrame for a given object.Whenever you create a JFrame for an object, you register this frame(value) with the object(key). Whenever you have an object, you call
map.get(object)and will receive the correct JFrame instance.