0

I'm fairly new at GUIs, and I'd like to understand how to properly structure my program.

I'm looking to create a GUI for a MVC model(all control methods are already implemented). Say I need to have 3 separate windows, with certain buttons closing one window and opening another.

I was thinking of having 3 classes, one for each window. Each window would be a JFrame. Is that reasonable? Then, we need to put in the action listeners. Where would those go? I'm thinking there should be one other class, with the main method. That class could also have all the action listeners, since that's the class that also creates the control and is capable of calling control's methods. Is this a good way to go about it?

Whatever your recommendation is, could you provide an idea of how this would look in terms of code? I know how to put together components in one frame, and add a listener within that frame so that a button performs some action. But I'm not sure how this would look with multiple windows. Any small example would do. Thanks.

Salah Eddine Taouririt
  • 24,925
  • 20
  • 60
  • 96
silentwinter
  • 107
  • 2
  • 12

2 Answers2

1

I suggest that you don't use swapping JFrames since that can be very annoying to the user -- how many applications do you currently use that throw a bunch of windows sequentially at you? Instead consider using dialog windows judiciously where called for, for instance where the program must stop until the user interacts with it in a certain way such as logging in, but mostly that you swap views (often JPanels) by using a CardLayout. Your Swing GUI classes should be geared towards creating JPanels which will better allow this flexibility.

And yes, major separate portions of your GUI code should be in separate classes.

Also, I strongly advise against use of GUI-builder utilities for creating Swing applications until the programmer has a pretty good handle on Swing basics.

For a simple example of Swing MVC, please have a look here: using-a-jfilechooser-with-swing-gui-classes-and-listeners

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
0

Note that each JFrame gets its own taskbar entry (I think). Consider JDialog if you want to avoid this and/or have need for modal windows.

One way to organize actions that are used in multiple places is to create a singleton hash (as its own class, or a context class) to store them in. Use the Action class instead of ActionListener for this - pass it to JButton's constructor.

Your program may go like this:

  1. create ShowViewFooAction and add to ActionHash
  2. create ShowViewBarAction and add to ActionHash
  3. create ShowViewBazAction and add to ActionHash
  4. create ViewFoo
    1. create ShowViewBarButton with ShowViewBarAction from ActionHash
    2. create ShowViewBazAction with ShowViewBazAction from ActionHash
  5. create ViewBar ...
  6. create ViewBaz ...

As for structuring Swing code in general, check out JGoodies Binding. They have tutorial/sample code in the 1.5.0 version (it's old but still a good example).

Peter Tseng
  • 13,613
  • 4
  • 67
  • 57