patternjavaMinor
Login window in Swing using MVP
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
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
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 changeprivate JTextArea password;toprivate JPasswordField password;. If you do that, changepassword.getText()toString.valueOf(password.getPassword()), because getText method in JPasswordField is deprecated.
- Then, you may change
private JTextArea usernametoprivate JTextField usernamebecause username is single-lined and JTextField is better for it.
- In the
SwingLoginViewconstructor, you are callinginitializemethod which is ONLY callinginitializeComponentsmethod. So you better callinitializeComponentsdirectly instead of callinginitializeand delete/remove the methodinitializefrom your code.
- In the
SwingUtilities.invokeLater(), changeJFrame mainWindow = new JFrame();toJFrame 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();topassword = new JPasswordField("password");and delete the next line. And changeusername = new JTextArea();tousername = 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.