Reactivate frame instead of open a new one
Hi I've got a MainWindow with a开发者_如何学编程 JTable, which is a list of Stuff. If you doubleclick on one of them, there is a detailView which open a new Frame (in a new window). Now if one detailView is already open, how can I reactivate it instead of open a new one? Can one somehow get an Array of visible frames in Java?
Thank you!
I think if you use frame.setVisible(true);
the frame gets focus
To indicate number of opened frames, either you:
1) If you have an array of your frames in your MainWindow, then you can do this
getOpenedFrames() {
int openedFrames = 0;
for(int i=0; i<arraOfFrames.length; i++) {
JFrame frame = arrayOfFrames[i];
if(frame.isVisible) openedFrames++;
}
return openedFrames;
}
OR
2) If you don't have an array of the frames, then use a flag int to keep track of number of opened frames:
int openedFrames = 0;
every time a new frame is opened, increment openedFrames by 1, everytime a frame is closed decrement openedFrames by 1
One way is to use CardLayout
in detailView
, put a child panel for each Stuff
. And then switch between children panels. The advantage is that the size of detailView
won't change when switching panels for Stuff
. But you will create unnecessary panels for all the Stuff.
Another way is to extend JFrame
with some method showStuff(Stuff s)
(or make a class which contains an instance of JFrame
inside and provides some basic methods: show
, close
, showStuff
) which clears the content of the frame and shows the new stuff.
First of all the "detail" window should not be a JFrame, it should be a (modal?) dialog.
I can't tell from your question if you want one detail dialog for each row in the table or one dialog for the entire table.
If you want one dialog for the table then the solution is easy, just use a modal dialog, then the user will be forced to close the detail dialog before opening another one.
If you want multiple dialogs, then use a HashMap to keep track of the open dialogs. Maybe use the row number as the key and then add the dialog as the value. You then add a WindowListener to the dialog. Every time the dialog is opened/closed you update the HashMap.
I did a Controller class, which has all the detailViews in a Hashmap with key s. The controller has an method show(Stuff s) and if s is already in the list I do an detailView.toFront() and otherwise I create a new detailView an insert it into the Hashmap.
精彩评论