1

I'm working on a assignment for university. The program use server/client(through sockets) along with a Model-View-Controller approach.

I'm creating a login screen with a JFrame, this login screen must call another JFrame depending on who just logged in. As the title says, I'm trying to pass the controller from the Login JFrame to another JFrame. With a JInternalFrame I would do it like this with a button:

NOTE: I want the second frame to be independent, I don't want to add JInternalFrames to a JDesktopPane

JFrame (I remove most of the autogenerated code):

package javaapplication1;
import Controller.Controller;
/**
 *
 * @author invitado
 */
public class MyFrame extends javax.swing.JFrame {
    private Controller controller;
    /**
     * Creates new form myFrame
     */
    public MyFrame() {
        controller = new Controller();
        initComponents();
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    MyInternalFrame internal = new MyInternalFrame(controller);
    MyDesktopPane.add(internal);
    internal.setVisible(true);
    }       
}

InternalFrame:

package javaapplication1;
import Controller.Controller;
/**
 *
 * @author invitado
 */
public class MyInternalFrame extends javax.swing.JInternalFrame {
    private Controller c;
    /**
     * Creates new form MyInternalFrame
     */
    public MyInternalFrame(Controller c) {
        this.c = c;
        initComponents();
    }
}

This works for me all the time, but when I try it with a JFrame instead of a JInternalFrame a line gives me an error on new MyFrame2().setVisible(true);

public static void main(String args[]) {
        /* Set the Nimbus look and feel */
                  . . .
/* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MyFrame2().setVisible(true);
            }
        });
    }

Should I put the parameter controller in MyFrame2().setVisible(true); or there is another way to do it?

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 1
    Okay, this is probably going to mess with, but the frame shouldn't know about the controller, it should generate events to which the controller can respond and then make further decisions. In your current implementation, the frame is making decisions about what should happen next, when that isn't it's responsibility, that's the controllers – MadProgrammer May 20 '17 at 01:29
  • 1
    See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson May 20 '17 at 01:37
  • 1
    @AndrewThompson thanks for the feedback. – Luis Restrepo Hoyos May 20 '17 at 02:18

0 Answers0