I am trying the save the state of a previous frame and then carry it over to the other frame. More like saving the data of the textfields and areas so that when i press next button some text fields,and variables will be initialized in the next forms Labels and when you press back your previous data will still remain in your form inteface.Pls help
More like data binding but across different forms
You can use any varible for that, and pass it as parameter to every JFrame you create.
But it sounds more like you want to use a CardLayout for your JFrame and use different cards that can be shown for the user. See How to use CardLayout.
If you are trying to create a wizard like UI, you should look up Sun(oracle)tutorial here.
Use something like Java's XMLEncoder.
Related
In windowsbuilder, I would like to change information from a seperate window, more specifically a table, from my main window. Is it possible to use my information from one seperate window to change it in another box?
Basically, take information from here:
And put it into this table:
Is it possible? If so, how? Thank you in advance.
Yes, it is possible - but not in Windows Builder.
Create an object which will hold the information from the first window and pass the information from the window to the object. Next, pass the object to the second window (how you do this will depend on how you've configured your window objects). Finally, populate the table with the information from the object.
As far as I know, there is no direct way to do this in Windows Builder, although the above method will work fine provided you can pass objects from the first window to the second.
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.
I have 5 JFrames in my application and I want the values from all 5 JFrames to be sent to a single JFrame. And it is a process where I have to go one frame to another and the value entered previously should not be lost and must be visible at the end of the process.
Easy example is,
I key in my name in the first frame,
then I key in my Address in the second frame,
then my mobile number in the third frame
and so on till the last frame where I want my keyed in details in the previous forms to be in the final frame to display my data in JTextfields. Is this possible? Because if it is a single form, I know how to do it. But when it is multiple forms in this situation I am lost. Please help.
This has nothing to do with Swing or JFrames and all to do with the general issue of getting information from one object into another. Yes it's possible -- give the classes that you wish to extract information from "getter" methods, and then call them when you want the information. If you want to gather this information in an event-dependent fashion, then you will need to have one class listening for state changes brought on by events in the other classes. A PropertyChangeListener can work well for this.
Or if you use modal JDialog windows instead of JFrames, you will always be notified when the dialog has returned and is no longer visible, since the calling code's program flow resumes from right after where it told the dialog to become visible.
Next we can discuss whether having 5 separate JFrames is a good idea or not. I'm guessing you know my opinion on this, else I wouldn't have mentioned the subject.
When specific action is performed, I want to replace button with text field. I know that I have to call button1.setVisible(false); but I don't know how to create text field on the exact same place. I am using NetBeans designer, if you can give me a hint, how to add 2 components at same place, and switch between then, something like switching between layers in photoshop, if something like that is possible, would be great. Thanks
For many components in one space, use a CardLayout as see in this short example.
I'm looking to build out a Java GUI with a table area and an area that will display the data of a selected row of the table. I've never tried a multi-frame set up before so before I venture to do this I wanted to check with others. Is it difficult to have two frames and have them passing data back and forth? The idea would be that I could move the details frame anywhere I like on the screen or to a second monitor and allow the table to go full-screen if the user wants. Any input or examples are appreciated.
don't to create two of more JFrames use JDialog instead,
reuse this JDialog for another action(s)
create one JFrame and one JDialog for displaying details
have to determine if and which of JTables row(s) is selected
better would be to set ListSelectionMode to the SingleSelection
maybe would be better to invoke (show that already exist) JDialog from JPopupMenu Action
You should have no problem in doing what you are after. You can have public methods in each frame which expose properties and/or structures and you then pass the instance of one JFrame to the other. This should allow you to pass data back and forth.
That being said however, I think that this scenario is valid only when you have one, two, or at most three JFrames. Having a lot of frames calling each other could result a maintenance nightmare.
there are several possibilities to do so:
you can add one of the jframes as a listener to anothe, or both to each other. For this, you have to implement a listener mechanism, like in java.awt. You can pass the information contained in the event objects - this would be the most clean alternative
you can pass the instance of the detailframe directly in the constructor of the main frame and call operations from main frame on detail frame. this is the simplest way, but you will need lot of code changes if you have some new features to add