4

I have about 3 frames in my java swing application. What it the correct way how to handle with these frames? I mean some pattern or something else. Now I have always one class which represent frame and one class for panel which is main in this frame. Now I have defined frames as static variable and when I wanna hide them I call classname.frameName.setVisible(false);

is this the correct solution?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
hudi
  • 15,555
  • 47
  • 142
  • 246

4 Answers4

9

Besides the (excellent) suggestions of a CardLayout or JFrame with multiple JDialog instances, here are some other strategies which might work singly or in combination, to collapse a variety of content panes into a single frame.

  1. JDesktopPane/JInternalFames (Tut.).
  2. JSplitPane (Tut.).
  3. JTabbedPane (Tut.).
  4. JLayeredPane, if you're feeling brave (Tut.).
  5. JToolBar - floatable if needed (Tut.).
  6. Different constraints of a JPanel in a nested layout.

There are probably more..


Of course, as Adamski pointed out, there are some further quirks to consider..

What if each frame has JMenuBars or JMenus?

Possibly combine them as sub-menus.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    +1: Re. toolbars I tend to solve this by defining a class: MyAppPanel that exposes a method like getToolbarActions(). When the panel is given focus (e.g. supposing it's part of a JTabbedPane) I pull out that panels actions and add buttons to the JToolBar on the fly. In addition I tend to have some permanent "global" toolbar actions. – Adamski Oct 25 '11 at 15:53
5

Take a look at a decent docking framework such as MyDoggy. This allows you to display all three components in a single JFrame, but is very flexible in that you can view the data side-by-side, resize and maximise components.

Adamski
  • 54,009
  • 15
  • 113
  • 152
4

This design seems flawed. Instead of having multiple containers, you should use an appropriate layout manager. In this case, I recommend using CardLayout. This way, you would have a single container with multiple exchangeable views.

mre
  • 43,520
  • 33
  • 120
  • 170
  • 1
    I disagree. There may be a completely valid reason to have multiple frames and there are still applications that use this approach. I mean, just take a look at GIMP; the user interface is a work of art (actually it's rubbish but you get my point). – Adamski Oct 25 '11 at 13:38
  • hm, cardLayout seems interesting. but as I said I have 3 frames because sometimes I need to see 1 and 2, or sometimes just 3... – hudi Oct 25 '11 at 13:40
  • @hudi, I know. My suggestion was to consolidate those 3 frames into a single frame with 3 separate views, which are all controlled by the frame's layout manager. – mre Oct 25 '11 at 13:41
  • 1
    @Adamski I dis_agree with idea how to manage (somehow to catch) 3 JFrames (another answer could be if is there one JFrame and two JDialogs/JWindows), in this case is better look for CardLayout (@mre +1) – mKorbel Oct 25 '11 at 13:43
  • @Adamski IMO GIMP is the classic example of why multiple frames is a bad idea. I would prefer to see all those frames enclosed in a single 'desktop pane'. Tastes differ, apparently. – Andrew Thompson Oct 25 '11 at 13:43
  • 1
    @Andrew: I was being sarcastic about GIMP; it has to be one of the worst user interfaces ever. I'm really just highlighting the assumptions made in the answer that CardLayout is appropriate. What if each frame has JMenuBars or JMenus? What if the user needs to be able to see all 3 sets of information at once? – Adamski Oct 25 '11 at 14:13
  • @Adamski *"I was being sarcastic about GIMP"* Is my face red! I blame the '32 hours up'. ;) – Andrew Thompson Oct 25 '11 at 14:17
  • @Adamski then you have to use JWindow (JDialog), sure that possible but there are lots of problems with move JFrame#toFront ..., etc.., simply why to wasting the time for that – mKorbel Oct 25 '11 at 14:17
  • @mKorbel - I'm not advocating multiple JFrames as a *good* solution; I'm just saying that there are other considerations to take into account (see my comment re. menu bars, etc.). – Adamski Oct 25 '11 at 14:20
  • 1
    @Andrew: You should go for the full 48; I hear you start hallucinating at that point. – Adamski Oct 25 '11 at 14:20
  • @Adamski *"What if the user needs to be able to see all 3 sets of information at once?"* I think this was covered in several of the points of my answer. e.g. nested layout, `JDesktopPane`, `JSplitPane`.. See also the edit for a possible approach on menus. – Andrew Thompson Oct 25 '11 at 14:25
  • But, you are all considering Windows. On a mac it's pretty normal having multiple Frames. (not necessarily Java) – 11684 Jun 13 '12 at 20:32
2

Controlling frames through static references seems to be a very fragile solution. What if the reference is null? What if the frame isn't in a completed state when setVisible() is called on it?

It would probably be a better idea to separate this logic out into a separate class and either have the frames register themselves to it , or construct everything up front.

Nate
  • 16,748
  • 5
  • 45
  • 59
  • yea I thought that this isnt very good solution so I looking for better. Maybe I should have some factory to frames some static class in which constructor I will create all frames what I need and then I call them but still it is static frames and I dont like static variable :) – hudi Oct 25 '11 at 13:35