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.
2 Answers
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:
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.

- 1
- 1

- 283,665
- 25
- 256
- 373
-
Thanks for the help to be honest the only reason why I thought making one single form for the adding of new books just looked more professional, but the fact that I had to ask about it made me believe that it just isn't worth the time or effort. Thanks for your help. 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? – Justin Michel Jul 13 '13 at 13:15
-
@JustinMichel: you're welcome. Please see edit to answer above. – Hovercraft Full Of Eels Jul 13 '13 at 13:17
-
Oh yes I see sorry about the misunderstanding. I do understand now, what you mean although I am having trouble actually implementing it. Could you by chance show me what the code would look like. I am sorry for this and I swear I am not trying to make you do the whole project for me I really just need the help. I did look at the links you gave me but I just don't understand them. – Justin Michel Jul 13 '13 at 13:31
-
@JustinMichel: I already showed you what similar code should look like in my answers in the links. If you're asking for me to show you what **your** code should look like, then no, and you should not even be asking this (something I feel strongly about). This is something that *you* should try to implement yourself. If you still are stuck, then ask a new question and show your code attempt. – Hovercraft Full Of Eels Jul 13 '13 at 15:37
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.

- 67,789
- 12
- 98
- 136