2

I am creating a Java desktop application. In this application I have one table. If the user double clicks the row of table, the corresponding data was viewed in another window [frame -> view frame]. The user minimizes the view frame and selects another row in a table. It was again open in a new window [frame].

How to avoid the multiple frame? I want only one view frame at a time. Suppose the user want to view new data then the old view frame will exit and new view frame will open.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Siddhu
  • 107
  • 1
  • 8
  • don't have two JFrames in the first place! But I would say: make the view frame modal. – 11684 Aug 18 '12 at 07:35
  • Why don't you maintain a single reference to the JFrame ? Singleton pattern? – Shashank Kadne Aug 18 '12 at 07:40
  • 1
    See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) for links to answers with multiple alternatives - including `CardLayout` and a nested layout (e.g. table in `CENTER` with details in PAGE_END` of a `BorderLayout`). – Andrew Thompson Aug 18 '12 at 07:40

2 Answers2

3

Make the frame an instance variable. Whenever you have to display a new frame, check if it is already displayed and if it is, first close the existing one and then display the new one.

Dan D.
  • 32,246
  • 5
  • 63
  • 79
2

Generally, it's best if you have a single JFrame in your application while all other windows are JDialogs.

  • Have you considered making the second window modal ?

  • Another option would be to have a flag which tells you if the second window is open. If it is, then when the user double clicks another row in the table you just replace the content (instead of creating a new window).

You can set that flag inside the windowClosed method of a WindowListener. This WindowListener you will then add it to your second window.

  • You can see if a Window is opened by calling Window#isShowing. This is applicable to both JFrames and JDialogs.
Radu Murzea
  • 10,724
  • 10
  • 47
  • 69