• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Besjamain Greenaway
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you manage to download the files? I couldn't upload any JAR's or ZIP's here on the forum...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!