0

Ok so, whilst my code works. I am beginning to question the logic of my code, and how I can improve it, before adding yet more code.

In short, I am creating a JFrame using Borderlayout as the layout manager. I have added a JPanel, which then has buttons, textfields etc added. Once the user has done everything they need to do, I get the user to click "ok" and I basically call the dispose() method.

I then paint a new JFrame, and do exactly the same thing repeatedly.

Whilst this is easy to code, and I'm a self taught newb, I am concerned about my lack of code re-use and I am convinced I should only be using one JFrame.

My main question is this : Is the dispose() method destroying the window, or just hiding it?

Any advice on this topic, would be much appreciated :)

  • *I am convinced I should only be using one JFrame.* Why? Show us some code, unless the frame contains the same components I don't follow you... – Elliott Frisch Jan 20 '17 at 11:56
  • 1
    There are alternative solutions, such as using a `CardLayout` or simply switching the contents based on your needs. It reduces some of the "flashing" of windows, changed window position and size issues which, as a user, I'd find rather annoying. By not basing your core components on `JFrame` you make you UI more flexible and reusable, allowing you to make use of these components in different containers based on your needs. If you just need the user to choose an option, make a quick choice or provide some limited information, then a dialog would be more useful – MadProgrammer Jan 20 '17 at 12:00
  • 1
    No, there is no "hard and fast" rule which says you can't use multiple windows, but you'll find the cons greatly out weigh the pros in the long run, especially as the complexity of the program grows – MadProgrammer Jan 20 '17 at 12:01
  • 1
    See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Jan 20 '17 at 12:05
  • The additional JFrame's do contain similar components, JButtons, JOptionPane's to name a couple. I'm using netbeans and it takes about 30 seconds to build and compile. – Darren Estcourt Jan 20 '17 at 12:19
  • Yeah I've read Andrew's post and it really make me think. I'm taking into account the DRY (Don't Repeat Yourself) principle and can't help feeling I am ignoring that principal by having multiple JFrames and therefore multiple JPanels. But then solutions suggested are to make use of multiple JPanels on one JFrame. – Darren Estcourt Jan 20 '17 at 12:25
  • If all your `JFrame`s look the same then you could build your GUI based on a single `JPanel` and have multiple instances of it, then add those panels to a Card Layout as suggested above by @MadProgrammer. There's no shame on using multiple `JPanel`s and also using multiple Layout Managers – Frakcool Jan 20 '17 at 14:23
  • If you want to display a dialog that disappears when the user presses "OK", the standard way to do that is with `JOptionPane`. For your "main question", the `dispose()` method is not destroying the window or anything in it. You can display it again with `pack()` and `setVisible(true)`. – Enwired Jan 20 '17 at 18:34

0 Answers0