I'm currently building an application that will require many different 'states' e.g. A screen for a user to log-in, a welcome page etc.
I want to design this application with MVC in mind but being somewhat of a newbie when it comes to MVC so I'm not sure what the best way to handle the multiple states would be.
What I have done in the past is use one big controller which changes the content of a JFrame, for example the user would click a button on a particular view (which is just a JPanel with various components), which would notify the controller and if necessary the controller would switch the current JPanel on the JFrame. E.g. the user clicks the login button and the controller removes the login JPanel and switches it with the welcome page. This of course means that the single controller must keep instances of these different views and the single controller eventually becomes very large due to interacting with all the different views.
So my question is how can I implement something similar to what I described above but with a greater separation of concerns?
P.S I've looked into using Card layout but I don't feel it provides enough separation when building a large application.
Related
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);
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
Just how much do I need to expose to other parts of the program? ie You're welcome view doesn't need to know about the login view, as it could do things to the login view which are out side of it's scope of responsibility
How hard or how much work would I need to do to change any part of it?! So you get the welcome screen to open the login screen, but now you want to add in "auto login", just how much work are you going to have to go to make that work? Would it have been easier if the welcome and login views were independent of each other and controlled through some other mechanism?
Take a look at Java and GUI - Where do ActionListeners belong according to MVC pattern? for simple implementation example
I'm using NetBeans 8 IDE to design a desktop application. How can I transit among different forms (e.g. login page and homepage)? Should I design multiple .java JForms or is it possible to completely delete previous components and bring new ones?
Is there something like hidden tabs that aren't shown to the user but really exist? I am looking for a high performance way to toggle forms.
Were you to have the form constructed programmatically - although this is a bit inefficient - you could use a switch statement in a loop with cases that contain the individual forms, and statements to clear the existing form components. There's also an easy JavaFX navigation solution using a larger-than-viewport area (containing each form in a horizontal series) wherein the forms get the viewport one-by-one, but it looks like you're using swing; sorry if it's of no help in this case.
I am making some application for myself, just to practice on my designs and GUI.
My application is divided into two sides, first side is the model / logic however you call this, second side is the visual side, where you handle the gui, buttons and view.
So now my application has a feature that pops up and asks the user if he wants to use some feature, and then if he clicks yes, it will open a new JFrame window with many configurations.
these configurations will "probably" be in the Config class.
My question is, what is the best way to transfer data from the GUI to the model? Since you have to create a button listener in order to detect button clicks or text, what is the best proper way to update the configuration after the button was clicked?
For example, you have an application, and in order to start it you need to click on the button, I have two ideas in my head:
Transfer the Config object to the area where you handle the button & listen to it
Make Config static, and set up a Set() method, so you can set configurations without any objects, like Application.setConfiguration(config, type)
But I heard that statics are NOT always good, so I wondered, using static in this case is OK and good or are there better ways to do this? Or passing the config object to the GUI area is OK aswell?
This is how my structure looks like:
(source: gyazo.com)
I am working on an application that uses Swing. I have successfully created a main GUI for the user to work from. However, I would like to allow the user to change his/her settings. How should I go about creating the settings window? Would using a new JFrame called 'Settings' be the best way to handle this, or is there something better to use than a second JFrame?
(Note: The settings JFrame, on exit, will not close the main GUI, it will use the DISPOSE method)
I would like to handle this in a way that consumes the least amount of memory, but maintaining a professionalized look to the application.
Have you considered a CardLayout? http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html
Personally, I find the use of a separate dialogue to be a bit dated for configuration settings. I prefer tabbed layouts, which are card layouts decorated with a tab bar across the top.
You could easily wrap your application in a near-top-level card layout and add a menu action to switch to the configuration card, with the "acknowledgement" or "cancel" buttons switching back to the main application card.
In the end, it is really about what your users prefer, but remember a lot of them might prefer what they know, even if it is not a better solution. You have to find a balance, and if your implementation rocks, then eventually they will want your approach to the problem to be used in other applications.
A perfect example of this is tabbed browsing, as opposed to multiple windows. Personally, I can't imagine going back to multiple-window browsing now that I have become accustomed to browsing tabs, but at one point in time, multiple windows was the only game in town.
In the end, if you find out you made the wrong choice, keep you code clean enough to easily implement with either solution. As long as your configuration screen is just a plain JPanel (or wrapped in just a JPanel), it shouldn't be very hard to do.
here is a class that does just this kind of thing:
http://tus.svn.sourceforge.net/viewvc/tus/tjacobs/ui/dialogs/ParamDialog.java?view=log
you have to look at the ApplicationListener interface, especially at the 'handlePreferences' method of that interface.
I'm creating a standalone SWT desktop application that has around 10 different screens (few wizards, help, forms, etc). Some elements on screen don't change at all (like header, background, etc) and there is a working area that changes depending on what is clicked, etc.
What is the best way to manage application screens? Do I need to create all screen at startup and then show/hide them depending on what is clicked? Or do I need to create those screens dynamically?
Also, I couldn't find any way to show/hide a Composite, do I need to dispose it and then create again?
What is the best practice? I'm new to SWT developing outside of Eclipse so any help would be beneficial.
Deciding whether to create screens up front or creating them the first time they need to be displayed is a decision that needs to be made on a per application basis. If there is a good chance that all the screens are going to need to be used on a particular application run and the number of screens is low (10 screens is relatively low) then you may want to create them at application startup so the UI is snappier once the application loads.
If you use bindings then you may need to come up with a dispose strategy (even if you only dispose the bindings) so you don't have too many events flying around.
Control has a setVisible(boolean) method (and Composite inherits from Control) which you can use to show and hide a component. Note this will only prevent the composite from being shown on the screen, the layout manager will still allocate a blank space for it. Many SWT layouts have a way to exclude a control from the layout which will get rid of the blank space. For example if you are using GridLayout then you would set the exclude variable on you GridData object to true when you hide that control.
Another option is to use StackLayout. This lets you stack a bunch of Composites on top of each other and then choose which on is on top. This might be a good fit for you if you have static areas plus a working area like you described. I would put the header, footer, and an empty composite with a StackLayout in a class file. I would then put each screen that will be displayed in the working area in their own classes. You can either have these screen classes extend Composite and then set up themselves in the constructor or you can use a factory method to setup the screen. Either one is a common and acceptable practice and comes down to a matter of taste.