1

I am making a vehicle_management system in java swings. I have a MDI(muti-document interface) Window in which there are more then one panels can be open.But when one panel is open after another the previous one is hides under new one, so all the JPanel are stacks. what i want is that if a panel is open and user trying to open another panel the previous opened panel get closed.how to do that.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Yash
  • 141
  • 1
  • 12
  • 4
    Why is this an MDI if only one 'document' can be open at a time? – Andrew Thompson May 22 '14 at 09:07
  • 1
    Please provide the code which you tried so far. – Arijit May 22 '14 at 09:16
  • @AndrewThompson OP doesn't two documents pointing to the same "subject" to be opened. – Stephan May 22 '14 at 10:37
  • Multiple JInternal Frame can be open, but multiple JPanels should not be open. because it reduces the app's performance. – Yash May 22 '14 at 10:50
  • This is the code of my MDI Window. Through this my multiple windows opened. – Yash May 22 '14 at 10:53
  • else if(actCmd.equalsIgnoreCase("buses")){ Buses b = new Buses(); desktop.add(b); b.setVisible(true); }else if(actCmd.equalsIgnoreCase("employees")){ Employees e = new Employees(); desktop.add(e); e.setVisible(true); }else if(actCmd.equalsIgnoreCase("routes")){ Routes r = new Routes(); desktop.add(r); r.setVisible(true); } – Yash May 22 '14 at 10:58
  • The Rest of the code is too long to enter, i can only enter that much code, In Which i am calling buses() class. i extends buses() to JPanel, So it becomes a panel. Then next employee, routes and so on. So now i want is that when buses panel is opened and if user trying to open employee panel, then buses panel will closed. – Yash May 22 '14 at 11:01

2 Answers2

1

Your application should maintain a List<JInternalFame> of open frames. In your open Action, see if the target frame is already open. If so, invoke setSelected(true) to bring the frame to the front; if not, open the frame as usual. A related example is cited here.

Addendunm: I don't know how to make a list of open frame and check target frame is open or not.

This example illustrates how to compose and iterate a List<JInternalFrame>. Use the list's indexOf() method to search the list for an existing instance. A return value of -1 means that the list does not contain the element

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • The example link you give i already did the same.but i don't know how to make a list of open frame and check target frame is open or not.. – Yash May 22 '14 at 14:08
  • @Yash: I've elaborated above. – trashgod May 22 '14 at 15:17
  • The Answer you told me, that way it works correctly for JInternalFrame,But for JPanel we don't have setSelected(true).so for jpanels how we do that, if a jpanel is hide under another panel how to get back it to front or like setSelected(true). – Yash May 23 '14 at 19:04
  • Don's add panels directly to the `JDesktop`; add panel(s) to a `JInternalFrame`, each of which can have its own menu and layout. – trashgod May 23 '14 at 21:38
0

First off, unless yours is an extremely unusual application, you don't "open" a JPanel. Since we're talking about an MDI application I'm guessing that what you really mean is that you're opening a new JInternalFrame.

Second, you said that you want to "close" the other panels (again, I'm assuming here that you really meant internal frames) but as someone else already pointed out, if you close them then it's not really an MDI application. I'm guessing that what you really meant or want is to MINIMIZE (not close) the other internal frames. If that's the case, then you should create a collection (e.g., a List) of JInternalFrame instances in the class where you create and add new internal frames. When you're about to add a new frame then just loop through the items in that collection and call setIcon(true) for each internal frame instance. Once that loop has completed, add the new internal frame to the collection and then also add to the desktop pane. At that point it will be the only one that's not minimized / iconified and any frame after that will likewise be (at least initially) the only one that's not minimized / iconified.

Gimpy
  • 61
  • 5
  • I am using JTables to show the entered data to user, that jtables placed on jpanel and that jpanel placed on desktop pane of mdi window. Thats why i am saying jpanel. – Yash May 22 '14 at 14:41