4 Replies Latest reply on Oct 23, 2012 6:37 AM by 945083

    Question: JFrame or JDialog for new application window(s) and frame(s)?

      Hi All - I realise this may be a really dumb/stupid question but I haven't really found an answer anywhere yet. I'm new to Java but have developed apps in .NET for a few years now and am looking for some advice and direction on basic Swing application layout/design planning

      In my .NET application I had a main form for the main application, when the user selected menu options or clicked a button (or right click etc) I would simply call a new form item. In .NET a form is a form, it just holds buttons/controls etc and has its own methods and subs and does not differentiate between form objects. I design these separately and code them individually as each form would perform different tasks, then from my main form I call them whenever they are required - all controls are accessible from any part of the code

      After doing some reading and searching I (think) that Java Swing basically encourages the use of only 1 main JFrame and then if you need another form to enter data on or store separate related controls then I believe that you need (or are encouraged to use) a JDialog to do this? Is there an issue if I have say 5 or 6 JFrames called from the main window or is it better to use the JDialog?

      I have added both another JFrame and also a JDialog to my work in progress and I can call both by using the standard .setVisible call from the main app, so my question relates to which is the best practice in Swing? What are the advantages/disadvantages of using one or the other approach?

      Different forums seem to have conflicting opinions, a few articles mention you should only use 1 main JFrame and then use panes etc inside that and make them visible when needed; however my app really needs separate windows so the user can work on several tasks at one time. For instance my app manages telecomms equipment and related services, so there is a need to manage the equipment in one window and then possibly view KPI data in a graph in another window, and make a new XML data file in another (and countless other tasks blah blah)

      From what I've read I believe a JDialog will do pretty much what I want as it extends JFrame in a lot of ways, but I would like some advice from people that work with Swing in the real world to give me their opinions on the best approach

      Apologies for the newbie stupid question but I would rather start with a good approach than just hack a load of bits together that just about do the job!......which I have to confess has been my main approach to programming so far :)
        • 1. Re: Question: JFrame or JDialog for new application window(s) and frame(s)?
          The recommended apporach with one JFrame and multiple JDialogs is suitable one for your case (IMHO). When you have multiple frames you see multiple app instances on taskbar. Additionally only JDialogs can be modal. So if you need some user's input you open JDialog and user can't click through the JDialog and change something on main JFrame. Of course the modal JDialog should have the JFrame instance as parent. If you need one more dialog to be opened from the first one just set the first dialog as parent of the second one and make it modal.
          1 person found this helpful
          • 2. Re: Question: JFrame or JDialog for new application window(s) and frame(s)?
            I would suggest another approach, better for maintenance IMHO: design your forms as JPanel, and add them in JFrame/JDialog when you see fit.
            If there are not too much nesting of JFrame level1 brings up JFrame/JDialog L2 brings up JFrame/JDIalog L3, you can even design your code such as the decision points (where you put the panels in JFrame or JDialog) can all be located in a single class.

            Keep in mind that bringing up a modal dialog that contains a JPanel insatnce is very handy with static methods JOptionPane.showXxxDialog(..., yourPanel,...).

            Best regards,

            1 person found this helpful
            • 3. Re: Question: JFrame or JDialog for new application window(s) and frame(s)?
              Interesting question.
              Presently I'm working on a text editor, and the user can open an arbitrary number of windows (JFrames), and they can also open up an arbitrary number of panes within a single window (accomplished with nested JSplitPanes).
              This approach (arbitrary number of JFrames) has been working well for me thus far. I'm a newbie to Swing, but I suppose using JFrames instead of JDialogs is OK in this case, since the JFrames are essentially independent of each other. My understanding is that a JDialog has a parent and is displayed in the context of its parent.
              • 4. Re: Question: JFrame or JDialog for new application window(s) and frame(s)?
                Thanks for all the replies guys! Much appreciated....