I'm developing an application which displays images, and plays sounds from a database. I'm trying to decide whether or not to use a separate JFrame to add images to the database from the GUI.
I'm just wondering whether it is good practice to use multiple JFrame windows?
JFrame is a top-level container that provides a window on the screen. A frame is actually a base window on which other components rely, namely the menu bar, panels, labels, text fields, buttons, etc. Almost every other Swing application starts with the JFrame window.
java write the actual logic code, in this class write the code which creates JFrame, add JButton for open New Frame. In this class write the code which creates JFrame, add JLabel into the New Frame. Simillarly, you can create multiple frames and navigate from one frame to another.
JInternalFrame is a part of Java Swing . JInternalFrame is a container that provides many features of a frame which includes displaying title, opening, closing, resizing, support for menu bar, etc.
I'm just wondering whether it is good practice to use multiple JFrames?
Bad (bad, bad) practice.
There are any number of ways of displaying many elements in one GUI, e.g.:
CardLayout
(short demo.). Good for: JInternalFrame
/JDesktopPane
typically used for an MDI.JTabbedPane
for groups of components.JSplitPane
A way to display two components of which the importance between one or the other (the size) varies according to what the user is doing.JLayeredPane
far many well ..layered components.JToolBar
typically contains groups of actions or controls. Can be dragged around the GUI, or off it entirely according to user need. As mentioned above, will minimize/restore according to the parent doing so.JList
(simple example below).JTree
.But if those strategies do not work for a particular use-case, try the following. Establish a single main JFrame
, then have JDialog
or JOptionPane
instances appear for the rest of the free-floating elements, using the frame as the parent for the dialogs.
In this case where the multiple elements are images, it would be better to use either of the following instead:
JLabel
(centered in a scroll pane) to display whichever image the user is interested in at that moment. As seen in ImageViewer
.JList
. As seen in this answer. The 'single row' part of that only works if they are all the same dimensions. Alternately, if you are prepared to scale the images on the fly, and they are all the same aspect ratio (e.g. 4:3 or 16:9).The multiple JFrame
approach has been something I've implemented since I began programming Swing apps. For the most part, I did it in the beginning because I didn't know any better. However, as I matured in my experience and knowledge as a developer and as began to read and absorb the opinions of so many more experienced Java devs online, I made an attempt to shift away from the multiple JFrame
approach (both in current projects and future projects) only to be met with... get this... resistance from my clients! As I began implementing modal dialogs to control "child" windows and JInternalFrame
s for separate components, my clients began to complain! I was quite surprised, as I was doing what I thought was best-practice! But, as they say, "A happy wife is a happy life." Same goes for your clients. Of course, I am a contractor so my end-users have direct access to me, the developer, which is obviously not a common scenario.
So, I'm going to explain the benefits of the multiple JFrame
approach, as well as myth-bust some of the cons that others have presented.
JFrame
s, you give your end-user the ability to spread out and control what's on his/her screen. The concept feels "open" and non-constricting. You lose this when you go towards one big JFrame
and a bunch of JInternalFrame
s.JFrame
s. However, I wanted the data entry screen to be a JDialog
whose parent was the data viewer. I made the change, and immediately I received a call from an end-user who relied heavily on the fact that he could minimize or close the viewer and keep the editor open while he referenced another part of the program (or a website, I don't remember). He's not on a multi-monitor, so he needed the entry dialog to be first and something else to be second, with the data viewer completely hidden. This was impossible with a JDialog
and certainly would've been impossible with a JInternalFrame
as well. I begrudgingly changed it back to being separate JFrames
for his sanity, but it taught me an important lesson.JInternalFrame
than a JFrame
. In fact, in my experience, JInternalFrames
offer much less flexibility. I have developed a systematic way of handling the opening & closing of JFrame
s in my apps that really works well. I control the frame almost completely from within the frame's code itself; the creation of the new frame, SwingWorker
s that control the retrieval of data on background threads and the GUI code on EDT, restoring/bringing to front the frame if the user tries to open it twice, etc. All you need to open my JFrame
s is call a public static method open()
and the open method, combined with a windowClosing()
event handles the rest (is the frame already open? is it not open, but loading? etc.) I made this approach a template so it's not difficult to implement for each frame.JFrame
needs more space than a JInternalFrame
, even if you open up 100 JFrame
s, how many more resources would you really be consuming? If your concern is memory leaks because of resources: calling dispose()
frees all resources used by the frame for garbage collection (and, again I say, a JInternalFrame
should invoke exactly the same concern).I've written a lot and I feel like I could write more. Anyways, I hope I don't get down-voted simply because it's an unpopular opinion. The question is clearly a valuable one and I hope I've provided a valuable answer, even if it isn't the common opinion.
A great example of multiple frames/single document per frame (SDI) vs single frame/multiple documents per frame (MDI) is Microsoft Excel. Some of MDI benefits:
SDI (Single-Document Interface, i.e., every window can only have a single document):
MDI (Multiple-Document Interface, i.e., every window can have multiple documents):
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With