From jframe1 I have a table containing data from sql. I press the search button on jframe1 and it will appear jframe2 for me to enter the information to search. When I click the OK button on jframe2, the jframe1 table will change to only the data stream to search. I have to do. My problem is transferring data from jframe2 to jframe1
In some class, or a new one if you want to make one, set your current instance of jframe1 so you can access it when ever. Where ever jframe1 is stored, pass an instance of that class to jframe2 when initializing it.
From here you could get the jframe1 from jframe2, and I would suggest creating a method in jframe1 to set/send this "data" to it when jframe2 is read to do so.
If you are doing this with more than 1 Jframe I would suggest doing something similar to what I said above, but for any frame which can have data sent to it, implement an interface you could create to make your code more neat and structured. Then make a field in some class all frames have access to and this field could be a List or array of JFrames. Then if you want to send this "data" to all these frames, loop through the frames and check if the frame implements your interface, or something like this:
for(JFrame frame : objectWhereFramesAre.getFrames()){
if(frame instanceof YourInterface){
frame.setData(/*send data here*/);
}
}
Related
Hy everyone,
I was just learning some new things with Java GUI, and I got stuck with a problem. This is what it looks like:
I want a button that adds the data from the second JPanel in to the first one.But not only once. I want it to go to the first JPanel, everytime it is pressed.
So, the button should create a new JTextField in the first JPanel, below their respective categories "name" and "age" everytime it is pressed. It means that I have to modify the "Y" field everytime, so all the new JTextFields created by the button "add data" dont get pilled up.
I don't know how to make the button "add data" Works, the other things, I know how to do. I know how to make the button creates a JTextField, with the data I want to store, only once (using getText() and setText() ), but not how to create new JTetFields with diferente "y" field everytime it is pressed.
Thank you for the help.
When you click on the button to add people you should not display a new JPanel. Instead you should display a modal JDialog. A JDialog is just like a frame but generally is only displayed for a specific function and then closed.
On this dialog you will probably want two buttons: 1) Save Data and 2) Close. This will allow you to enter information for multiple people before closing the dialog.
To display the "person" information you should probably be using a JTable. A table allows you to display data in a row/column format. See the section from the Swing tutorial on How to Use Tables for basic information to get you stated. Note, in this cause you will want to use a DefaultTableModel for your table and then you can just use the addRow(...) method to save the "person" information.
When you create the class for the dialog to get the "person" information you will want to pass the DefaultTableModel to this class as a parameter. Then when you click the "Save Data" button you get the information from the two text fields and update the TableModel. The table will be updated automatically.
So your first step is to create the main frame with the JTable with the two columns. Then you create a simple "Add Person" button. This button will then add hard coded data to the table each time it is clicked by using the addRow(...) method. Once you get this working then you change the logic of the button to display the dialog and then you can enter the "person" information on the dialog and save it.
I have been trying to do this a while back but failed to find a way.
My Java program contains four JFrames. JFrame1 contains a table and a button that on clicking opens JFrame2. In JFrame2 there is a table and two buttons that on clicking opens JFrame3 and JFrame4 which contain a table and Save button each.
Now what I want to do is that when a user enters data in JFrame3 and clicks the save button the data is saved in the table of JFrame2 and JFrame1 repectively. Same thing for JFrame4 but the data should be appended not deleted.
The data that is saved in the JFrame1 from JFrame3 and JFrame4 is to be used to insert into the database.
These are my requirements. Can anyone help me out with this problem? Thanks in advance.
What you are doing is usually achieved through two approaches:
You can use the Card Layout to avoid creating a new JFrame each time, but rather have one JFrame and replace its components. This would allow you to have one class which stores this data and then the logic to show it on your different screens.
If (1) does not do it for you, the other option would be to pass on a reference of the parent frame to the child. Thus, when you create an instance of JFrame2, you also pass it along an instance of JFrame1. Each JFrame class will have methods which are responsible for accepting information and displaying it according to your requirements.
Option 1 is usually the most recommended.
You can use parameterized constructor or public variables or even public methods to pass values from 1 JFrame to another also using public static variables(not preferred) will be okay.
Let's say we have a JFrame frame that contains two JPanels, buttonPanel and dataPanel, and in this panel a single JButton button. When clicked, button creates and shows a JDialog dialog in its own window (as usual). Using several JTextFields and a submit button, the JDialog dialog creates a new Object dataObject encapsulating these input data. If we wish our dataPanel JPanel in the main application frame to display this dataObject, how should dataObject be appropriately passed to the JPanel residing in a foreign JFrame?
That was a mouthful even to me while writing it, so I'll attempt to clarify:
JFrame frame
JPanel dataPanel - meant to display data from Objects created in the JDialog
JPanel buttonPanel - contains a button to open the JDialog, into which some information will be entered and with said information our Object dataObject is constructed.
The goal here is to pass this dataObject (and it's constituent fields) to the dataPanel to be displayed. What is the most appropriate way to handle this? I considered keeping the Objects in dataPanel static and then calling a static method from the JDialog to add the new object, but it doesn't seem the proper thing to do.
Some guidance?
Much will depend on the structure of your program, including how the dialog is supposed to behave:
If the JDialog is modal and disappears when the submit button has been pushed, then the solution is easy -- extract the data from the dialog-related class after it returns which will be the line of code right after where you display the dialog. The dialog's submit JButton's listener could simply make the dialog no longer visible.
Otherwise if the JDialog is non-modal and disappears when the submit button has been pushed, then you may wish to attach a Listener to its Window, I believe a WindowListener, and then have your calling code extract the information when the listener indicates that the dialog has been closed or is closing.
Otherwise, if the JDialog is non-modal and is not supposed to become invisible when the submit button has been pressed but you need to update the calling program with new data, then I would have the calling class add a PropertyChangeListener onto the dialog-related class so that the dialog-related class can notify any listeners that submit has been pressed. This code would be in the dialog's submit JButton's listener.
I would give a dialog-related class a public DataObject getDataObject() method that the calling code can call once the dialog returns, allowing the class that displays the dialog to extract the pertinent information when needed.
Whatever you do, there is no reason to use static fields and many reasons not to. I strongly urge you to not even consider this.
For example of a modal dialog that returns:
// caveat: code has not been tested by compilation or running.
JButton myButton = new JButton(new AbstractAction("Show Dialog Button") {
public void actionPerformed(ActionEvent evt) {
MyDialogPanel myDialogPanel = new MyDialogPanel();
JDialog myDialog = new JDialog(myJFrame, "My Dialog",
ModalityType.APPLICATION_MODAL);
myDialog.add(myDialogPanel);
myDialog.pack();
myDialog.setLocationRelativeTo(myJFrame);
myDialog.setVisible(true);
// dialog now returns and we can get the data
// assuming that the wrapper object for your data
// is called "DataObject"
DataObject dataObject = myDialogPanel.getDataObject();
// and perhaps use it. Assume setDataObject is a method
// of the main GUI that displays the data object
setDataObject(dataObject);
}
});
I have a question, im making a application where you can first select a name from a jList in a jFrame. After you've selected a name and pressed the proceed button, a second jFrame pops up.
On this jFrame there are a couple of textfield which i want to automatically fill up with information about the selected name selected in the first jFrame.
And this information which is automatically filled in is different depending on the chosen name.
I already have the first jFrame with 4 names in a arraylist but now im stuck on the second part where the chosen name is transfered to the second frame along with the extra information which must be done automatically
I hope someone can help me, i would really appreciate it.
THank you.
Well, heres how I would do it:
Exit the current jframe
Pass the data in the arrayList to a function that creates a new JFrame
Fill the new JFrame
Creating a a Swing application with multiple JFrames can be problematic.
A better and simpler approach would be to use CardLayout and to have panels within a single frame. Your ArrayList could be a member variable in the main JFrame class and could be easily be updated with data as you progress.
See: How to Use CardLayout
From the already opened jPanel I open the jFrame "FastSearch" search window using the following method:
new FastSearch().setVisible(true);
which lists the search results. When I select one of the search results I want to send the value back to the parent jPanel which opened the child jFrame "FastSearch".
I thought it would look something like this:
ParentWindow.targetVariable = theValueFromTheOpenedForm;
But of course, it doesn't work.
How do I pass data between opened jPanel and jFrame?
Thanks in advance
You'll need to pass the object through to the FastSearch class in some way. Either in the constructor or through some other method. I'm assuming there is probably a listener interface.