I am new to Java and I would like to know which would be the best approach to create my app’s navigation.
I will now explain my app functionality and the approach I chose.
Leaving all the networking and other details behind, I will concentrate on the GUI part, since this is what I am here for.
Basically my app does the following :
- When you start it, you are presented with a login screen, and 2 buttons(Register and Login).
- The sign up screen is a basic register screen with it’s accordingly components.
- If the users enters correct login information, he will be presented with a view containing some data.
Now, I thought to make a JDialog/JFrame/Frame that is global for the app and when the users clicks a button, remove the previous content of it and “re-paint” new components.
Snippet from my sign up screen :
private JButton signUp;
private final JDialog parent;
.....
signUp = new JButton("Sign Up");
signUp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
parent.getContentPane().removeAll();
parent.validate();
parent.pack();
Account v = new Account(parent);
v.init();
}
});
// add all the components to the parent frame
parent.add(signUp);
.....
By calling Account v = new Account(parent); I have passed the global frame(JDialog in my case) to be “repopulated” with other contents using v.init();.
So my idea is : all the “views” I need to my app should have a “parent” field (which gets the global frame when the constructor is called) on which I add the components I need. Every time, before I call the .init() method for a view, I will remove all the previous content.
I am not sure this is a good approach since I read about Threads and MVC. On the other hand my approach seems really simple and I am not sure if I should complicate it with multithreading or MVC.
By using my approach I have managed to make a quite flexible UI, I can “play” with the frame’s properties (size, focus, enabling,etc.) from any view.
NOTE: If I have not been clear enough, when I say “view” I am referring to the class that receives the parent field when created and uses .init() to add content inside my parent frame.
EDIT: I do not need any code from you guys, just a simple piece of advice. I do this because I don’t want further in the development of the app to encounter some serious issues because of my approach on the navigation style.
You can either remove/readd new content calling
Or use
CardLayoutshwoing actual card with necessary UI.