How do I get the same name after a successful login?
I have user class that set user.setName after verifying the user name and password from text file in login JFrame and user.getName in other frame to greet user in the main menu.
There's lots of different ways to do it, but basically it boils down to Passing Information to a Method or a Constructor.
One thing you should do is to try and decouple the process. Your "main menu" doesn't care where or how the user name is generated/gained, it only needs it. Equally, your login process doesn't care what happens after the user is validated.
To that end, you should take the time to understand the model-view-controller paradigm, you will see it a lot and will help you solve similar questions.
Basically, a "controller" will display the login view, the login view will gather the credentials and the this information will be feed back through the model for validation. When successful, the controller can pass control on to the next controller which will actually display the "main menu", providing the login model as part of the information.
Again the "how" depends a lot on the overall solution, but you want to get to the point where you have the ability to change either or both the main menu and login windows and it won't have any affect on the other
You can also have a look at:
Open JFrame, only after successfull login verification with database. Using Eclipse?
Java and GUI - Where do ActionListeners belong according to MVC pattern?
Open a JPanel after pressing a button in a JFrame
for more details and ideas
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 currently making a Java Applet program that heavily depends on the radio buttons and a text field for it to compute the needed information. I want to know if there is a way to remind the user that he or she has forgotten to select a radio button like how catching does to an empty textfield.
Thanks in advance.
P.s. I am still a senior high student. Our professor and curriculum requires us to study Java Applet before moving on to a more advanced type of programming.
Java Applet actually defines a Server-side code, which coincidently creates an HTML page that will run on the user's browser. But it's important to remember that none of your Java code will run in the browser.
This means that when a client presses submit on your page, all the information will be sent to the server side (as is). At this point you can check for example, if specific value was not set, and present a page with error message to the client. In conclusion: cumbersome, and not pretty.
A much better solution, and what is usually done is: Create a Javascript function that will test if a radio button is selected, if not, it will show an error message (without leaving the page) and will prevent the submit code from running until it was fixed.
Here's an example of how it's done: Check if input radio ID is selected before form is submitted
This is my first java swing application and I have some questions about the organization.
I need to create a application that after log in redirect the user to (let's call it) "Normal user page", "Admin Page" or "Super user page". After reading some articles I figured out that java swing applications, use one JFrame and panels to hide or show content (some thing like single page application).
My questions are now:
Do I need to create 4 "main classes" (log in, normal user, admin, super) and each one extends one jframe because they are particularly different applications and this jframes have their own classes (panles), with them I hide/show content on them
OR, Log in is my main Jframe and after log in i show a different panel (normal user, admin or super) in a different window or dialog, and they have their panels to hide show content
When my first questions is right is this a good folder structure:
Folder app: Log in, normal, admin, super user class(frame)
Folder noram user: classes/panels related to him
.
.
.
Thanks in advance :))
Questions/Answers:
Do I need to create 4 "main classes" (log in, normal user, admin, super) and each one extends one jframe because they are particularly different applications and this jframes have their own classes (panles), with them I hide/show content on them
If you're creating a decent Swing GUI, likely none of your classes will extend JFrame, and in fact it is rare that you'll ever want to do this. More commonly your GUI classes will be geared towards creating JPanels, which can then be placed into JFrames or JDialogs, or JTabbedPanes, or swapped via CardLayouts, wherever needed. This will greatly increase the flexibility of your GUI coding.
You will likely have one master View class/JPanel, and could swap its content with other sub-view classes that will correspond to the different states of your non-GUI model class, whether it would be a normal user, admin, or super user.
OR, Log in is my main Jframe and after log in i show a different panel (normal user, admin or super) in a different window or dialog, and they have their panels to hide show content
I usually use a dialog to log in such as a JDialog, and then after verifying credentials, show the main GUI, again with the correct view sub-type based on the state of the model.
When my first questions is right is this a good folder structure:
Folder app: Log in, normal, admin, super user class(frame) Folder noram user: classes/panels related to him . .
Much more important I think is to separate your packages into model, view, control, and main. Then you could use sub-packages for the various sub-portions of your program.
I strongly urge you to read up on Model-View-Control pattern of GUI structure, and then study up on the many useful variants of this.
Okay so I'm making a Library admin program and I have created a special frame where the user would enter details about a new book. However my method for adding a new book is in a separate class (methods). My question is how can I get the information the user enters in the text fields? Do I have to use something like getters, or is there an easier way. Also keep in mind that I am using the GUI layout (thing) in netbeans, and that I have already actually made the form. (I know it's frowned upon but I'm pressed for time and this is how we were taught.) This is a school project by the way. Thanks.
Okay so I'm making a Library admin program and I have created a special frame where the user would enter details about a new book.
Usually, a detail window should be a dialog, and likely a modal dialog. I suggest that you display this information in a modal JDialog, not a JFrame. Do this and it will make extracting information from the detail window much easier.
However my method for adding a new book is in a separate class (methods). My question is how can I get the information the user enters in the text fields? Do I have to use something like getters, or is there an easier way.
This begs the question -- what's so hard about using getters? And in fact his is exactly what I suggest that you use! Please note that your question essentially boils down to, "how can I get information on the state of one class's object from within another class's object", and for this getter methods are almost mandatory.
Also keep in mind that I am using the GUI layout (thing) in netbeans, and that I have already actually made the form. (I know it's frowned upon but I'm pressed for time and this is how we were taught.) This is a school project by the way.
This is unrelated to your current problem and should have little effect on its solution other than if you've hard-coded your "form" as a JFrame, then scrap it and re-do it as a JPanel.
I suggest:
Create an addEditBook modal JDialog
Give it getter methods to allow outside classes to be able to query its textfields for their contents.
Display the dialog from the main program.
Since it is modal the main program's code flow will pause until the dialog has been dealt with.
In your OK and Cancel button, set the dialog's state (OK_STATE or CANCEL_STATE) and close the dialog. The easiest way to do this actually is to use a JOptionPane as your modal dialog since it has mechanism for just this sort of thing. This is easily accomplished if your addEditBook is geared to create a JPanel, one that you display in the JOptionPane.
Program flow will then resume in your main program from right after where you showed the dialog
query the dialog for the contents of its fields.
For examples of the JOptionPane solutions, including option panes that request information from multiple fields similar to your window above, please see:
How can I make a JFrame modal like a JOptionPane?
Multiple input in JOptionPane.showInputDialog
Edit
You state in comment:
Oh and I was wondering how can I make the field of a normal JOptionpane input dialogue come up with a word already in it like for editing it will show the information stored already?
Please see the example answers that I have listed above as you'll see that they're not examples of a "normal JOptionPane" but rather JOptionPanes that display a GUI that you create. And just the same as it's easy to query the state of this GUI after it is displayed, it's just as easy to set the state of the GUI via setter methods before it is displayed.
My question is how can I get the information the user enters in the
text fields? Do I have to use something like getters, or is there an
easier way
You need to add actionListeners for you buttons, which means you will be overriding a method called actionPerformed. You basically need to associate your actionListeners with your 'Ok' and 'Cancel' buttons. When the 'ok' button is pressed, you should get a callback in the associated actionPerformed method. Then you should try to fetch the values of your textfiled using the getText method. Collect all the fileds and set the bean you have created to store that data. Then you can call your business logic to save/modify the books info.
This is a crosspost to the thread in Javaranch (includes some images): http://www.coderanch.com/t/567472/GUI/java/Optimal-solution-creating-multiple-dialog
I'm trying to develop a simple swing desktop application where I imagine alot of different dialog's jumping around to fetch user input. Would need to present labels, textfields, passwordfields, combobxes, checkboxes etc in various dialog windows.
For example: creating the database firsthand, creating the first admin account, adding users, changing user accounts etc.
I have an understanding that JOptionPane is used to create simple quick & easy modal dialog's. I would really like to know why one would choose one over another in this case. Which one is more preferable to use: JOptionPane vs. JDialog
Also I could use some pointers how one should appropriately design and implement this.
Thank you.
Here's a statement I found on the Java website that says one key point about the difference between the two.
How to make Dialogs
A Dialog can be modal. When a modal Dialog is visible, it blocks user input to all other windows in the program. JOptionPane creates JDialogs that are modal. To create a non-modal Dialog, you must use the JDialog class directly.
So it sounds like you would use JOptionPane if you want a user to have to make a choice and close the box before returning to the main screen. If you use a JDialog box, then they can just click around it and get back to the main screen without making a choice. For example, say you wanted to make a user choose the number of results before clicking submit, you wouldn't want them to be able to click around that window and click submit. You would use JOptionPane to force them to select a value first before going back to submit.
Check out http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html it pretty much has everything you would need.
As i understand it, JOptionPane is great for what it can do, but you can't really change the functionality beyond that (not easily). JDialog is better to inherit from if you want to create your own custom Dialogs.