Reputation: 13
I have a few questions about Java swing GUI, FYI this is my first time using a GUI so I really don't know much about this stuff
I have code that is basically a user class with name and number attributes which I plan to use as a login page. So what I did was create a new GUI class for this user class completely separate from the user class (as in one class for functions and another for the GUI). Was this wrong? as in should I have placed the user methods and the GUI in one class?
I have 2 GUIs, a welcome GUI and a login GUI. How do I ensure that one leads to the next? I tried to make the welcome frame invisible (the login frame is made visible in its own GUI) but that didn't work.
The login GUI is similar to the one above and has frame.setVisible(true);
Upvotes: 1
Views: 200
Reputation: 347204
1- I have a code that is basically a user class with name and number attributes which I plan to use as a login page, so what I did was create a new GUI class for this user class completely separate from the user class (as in one class for functions and another for the GUI) , was this wrong? as in should I have placed the user methods and the GUI in one class?
A common concept in UI development is "model-view-controller" (don't worry about controller just yet). This means that your "data" is modelled in some way (ie User
) and your "view" (UI) takes that model and makes decisions about how the model should be presented to the user. It also helps manage the interaction between the user and the model.
So, yes, keeping your "data" independent from your "ui" is the right strategy. Always remember, it's the UI's responsibility to determine "how" the data is formatted, the model is just a means to manage the data in some meaningful way.
2- I have 2 GUIs, a welcome GUI and a login GUI, how do I ensure that one leads to the next? I tried to make the welcome frame invisible(the login frame is made visible in its own GUI) but that didn't work
This is a little broader in concept. Typically, we might recommend using a CardLayout
to "flip" between views, but this would assume you want to revisit those views at some point.
On a more "abstract" point of view, you would use some kind of "controller" to make decisions about what should happen based on the current state.
This means, if "welcome" has not been presented, you'd present the "welcome" view. When the user is ready to move beyond it, "welcome" would notify the "controller" and the controller would then decide what to do next.
ie. Do you have previously saved credentials or not? If so, you could auto login the user and move on, otherwise you'd need to present the "login" view in order to get the credentials and allow the controller(s) to authenticate the user.
This moves you onto the "observer pattern" (aka listeners), where an interest party registers interest in been notified when some state has changed.
When trying to design these kind of systems, always be asking yourself some basic questions
Take a look at Java and GUI - Where do ActionListeners belong according to MVC pattern? for simple implementation example
Upvotes: 2