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.

3
  • don't have two JFrames in the first place! But I would say: make the view frame modal. Commented Aug 18, 2012 at 7:35
  • Why don't you maintain a single reference to the JFrame ? Singleton pattern? Commented Aug 18, 2012 at 7:40
  • 1
    See The Use of Multiple JFrames, Good/Bad Practice? 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). Commented Aug 18, 2012 at 7:40

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.