0

I'm writing a simple Java Swing GUI application following the MVC design pattern. I've got a MainFrame.java which instantiates LoginDialog.java (modal JDialog). The user then logs in with username and password or can register (by clicking on Jbutton) which instantiates RegisterDialog.java (JDialog instantiated from LoginDialog). I've also got a Controller.java class which connects to a Database.java, and allows interaction with database. MainFrame object can interact with Database through controller object. On the RegisterDialog, when the user presses submit, I want to verify that the username is not already taken. How can I get data from Database from RegisterDialog? Also how can I get data from LoginDialog (should I have MainFrame listen to a DataRequestEvent that would be fired from LoginDialog?)

public class Controller {
    private Database db;
    public Controller(){
    db = new Database();
    }
}
public class MainFrame extends JFrame{
    private LoginDialog loginDialog;
    private Controller controller;
    public MainFrame(){
        controller = new Controller();
        loginDialog = new LoginDialog();
        loginDialog.setVisible(true);
        //...
    }
}
public class LoginDialog extends JDialog{
    //form fields...
    private JButton regBtn;
    private RegisterDialog registerDialog;
    public LoginDialog(){
        registerDialog = new RegisterDialog();
        regBtn = new JButton("Register");
        regBtn.addActionListener(new ActionListener(){
            registerDialog.setVisible(true);
        });
    }
}

public class RegisterDialog extends JDialog{
    //...
    private JButton submitBtn;
    public RegisterDialog(){
        submitBtn = new JButton();
        submitBtn.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                validateUsername();
            }
        });
    }
    public void validateUsername(){
        //here I need to check whether username is already in DB (sqlite)
    }       
}
Superdav
  • 29
  • 6

1 Answers1

2

As discussed here, "not every interaction needs to pass through your application's controller," but authentication and registration are reasonable candidates for your controller to manage. As outlined here, your controller may manipulate your data model directly, and listening views should update themselves accordingly. As shown here, Swing provides a number of ways to implement the observer pattern in order to handle such notifications. The exact details depend on how your application handles modality, but your authentication and registration dialogs can fire a suitable PropertyChangeEvent to notify listeners as needed. Examples may be found here and here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thanks for your answer, can you suggest me a book or site that can teach me more on Swing Development (at a professional level)? – Superdav Jan 08 '15 at 06:10
  • I started with [Bloch](http://stackoverflow.com/a/6067501/230513) and read a lot of source code. – trashgod Jan 08 '15 at 16:44