my dog learned polymorphism
The moose likes Swing / AWT / SWT and the fly likes A main JFrame with a button to open a new JDialog - MVC Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply Bookmark "A main JFrame with a button to open a new JDialog - MVC" Watch "A main JFrame with a button to open a new JDialog - MVC" New topic
Author

A main JFrame with a button to open a new JDialog - MVC

Besjamain Greenaway
Ranch Hand

Joined: Dec 21, 2007
Posts: 45
Somebody suggested to look how the JOptionPane is implemented, but I couldn't find anything. What I want is seemingly very simple: I have a main JFrame with a button on it. When that button is clicked, I want a new screen to come up. This main JFrame is having its behaviour separated out into another class (sort of a controller). The screen that comes up when the button on the main JFrame is clicked uses MVC. My question is this: The new screen and the controller for that screen needs to be created somewhere. I know it doesn't go in the view of the main JFrame, but having it in a method in the controller doesn't seem right as well.

What is a right solution?
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Besjamain Greenaway wrote:but having it in a method in the controller doesn't seem right as well.

Why not? It seems fine to me.
Besjamain Greenaway
Ranch Hand

Joined: Dec 21, 2007
Posts: 45
As far as I think I know, the controller is the class that delegates actions to the model and enables/disables buttons in the view when necessary. It doesn't seem the place for object creation/creating new views. With emphasis on seem, because I have no clue as to how it really is
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
You can still have the JDialog creation code all in a separate GUI/view class and just instantiate that via the controller. While the controller shouldn't hold GUI code, it certainly is going to have to interact with the GUI classes.
Besjamain Greenaway
Ranch Hand

Joined: Dec 21, 2007
Posts: 45
Well, today I started churning out some code for this:

Code for the first window you see, the Main window:

the controller for the Main window:

Code for the second window:

Code for the controller of the second window:

So, is this code any good? Is it how it's supposed to? There are still some bugs that need to be fixed - In the constructor it only runs till the second line:
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
It's hard to judge this out of context. Could you create a Jar file of your current program so we can inspect and run it? If it can't be posted here, then there are other places it can be posted, and then post a link.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Somebody suggested to look how the JOptionPane is implemented,

how it is used

let's say your frame has a button, when clicked it does something via the controller.
if something is not right (invalid data?) a warning message might pop up

JOptionPane.showMessageDialog(null,"Warning");
that's using a static method, but it could also be done the normal way = new ....

JOptionPane opt = new JOptionPane();
opt.showMessageDialog(null,"Warning");

now, where is the code, it's not in your controller, instead it's being used/called by your controller.
the code is in a class, in a package, imported.
Besjamain Greenaway
Ranch Hand

Joined: Dec 21, 2007
Posts: 45
Ok, I jarred my files, put I'm not allowed to upload them here... See if this works:

JAR with GUI for multiple frames
Besjamain Greenaway
Ranch Hand

Joined: Dec 21, 2007
Posts: 45
Did you manage to download the files? I couldn't upload any JAR's or ZIP's here on the forum...
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: A main JFrame with a button to open a new JDialog - MVC
 
Similar Threads
MVC - new frames
JFrame with multiple DialogBoxes
MVC in Swing for a modularised View
JFrame confusion
Dispose?