HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Login window in Swing using MVP

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
loginmvpswingusingwindow

Problem

I am trying to grasp MVP (like so many) and although there are quite a few resources out there, I'm not sure I really get it.

I am trying to do this without frameworks to really see what's going on.

Here's my code:

main method (only method in a special class called Main):

SwingUtilities.invokeLater(() -> {
    JFrame mainWindow = new JFrame();
    mainWindow = new JFrame("MainFenster");
    mainWindow.setSize(500, 500);
    mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainWindow.setLocationRelativeTo(null);
    mainWindow.setLayout(new BorderLayout());
    LoginView loginView = new SwingLoginView(mainWindow);

    LoginModel loginModel = new LoginModelImpl();
    LoginService loginService = new LoginServiceImpl(loginModel);

    LoginPresenter presenter = new LoginPresenter(loginView, loginService);
    loginView.setPresenter(presenter);

    mainWindow.setVisible(true);

});


View interfaces

```
public interface View {
// Not sure what woudl go here, but just in case...
}

public interface LoginView extends View {

void setErrorMessage(String errorMessage);

void setNotificationMessage(String message);

void navigateToHome(); // Should go here?

void setPresenter(LoginPresenter presenter);

interface LoginViewEventListener {
void loginButtonClicked(String username, String password);
}

}

public class SwingLoginView implements LoginView {

private JFrame mainFrame;
private LoginPresenter presenter;
private JTextArea errorMessage;
private JTextArea password;
private JTextArea username;
private JTextArea notificationMessage;
private JPanel panel;

public SwingLoginView(JFrame mainWindow) {
this.mainFrame = mainWindow;
inititialize();

}

private void inititialize() {

initializeComponents();

}

private void initializeComponents() {
errorMessage = new JTextArea();
errorMessage.setText("Hello");

notification

Solution

Following are some improvements in your code. Implementing them is your choice:

  • In the class SwingLoginView, you may change private JTextArea password; to private JPasswordField password;. If you do that, change password.getText() to String.valueOf(password.getPassword()), because getText method in JPasswordField is deprecated.



  • Then, you may change private JTextArea username to private JTextField username because username is single-lined and JTextField is better for it.



  • In the SwingLoginView constructor, you are calling initialize method which is ONLY calling initializeComponents method. So you better call initializeComponents directly instead of calling initialize and delete/remove the method initialize from your code.



  • In the SwingUtilities.invokeLater(), change JFrame mainWindow = new JFrame(); to JFrame mainWindow = new JFrame("MainFenster"); and delete the next line. This will shorten your code.



  • If you are implementing point number 1 and 2, change password = new JTextArea(); to password = new JPasswordField("password"); and delete the next line. And change username = new JTextArea(); to username = new JTextField("username"); and delete the next line. This will again shorten your code.

Context

StackExchange Code Review Q#160908, answer score: 3

Revisions (0)

No revisions yet.