Consider the following scenario:
I have a JFrame “Main” which has a jButton “caravanDataButton” which opens another JFrame “CaravanData“.
CaravanData has several JTextFields. Its purpose is to ask the user questions and transfer the answers back to Main once the user has completed.
My question: What is the most efficient way of transferring data from a child JFrame to a calling parent JFrame (Or from this example from CaravanData back to Main)
Ideally I would like to do the following:
class Main
{
public Main
{
CustomVariable data = new CaravanData();
}
}
However, a constructor doesn’t have a return type. But this would be the best way because the variable “data” wouldn’t be assigned a value until the class CaravanData had finished its business.
My alternative solution is to use getters and setters. But I have to wait until the user has completed the form before I retrieve the data. I used a while loop:
class Main
{
public Main
{
CaravanData caravanData = new CaravanData();
while (caravanData.isUserFinished == false)
{//...Do nothing}
// Once the user has finished - collect the data:
CustomVariable data = caravanData.getRelevantData();
}
}
class CaravanData
{
...
public boolean isUserFinished()
{return ifUserHasCompletedForm;}
public CustomerVariable getRelevantData()
{
...
return data;
}
}
I don’t think this method is efficient at all. Is there a better technique? I have a few JFrames because there are a lot of question to be asked. I wasn’t sure how to word this to find answers on the net. I did think of using threads? Stopping one process until the other finishes. But I was under the impression you only use threads when a task consumes to much time from the EDT?
Try to use a JDialog as your child Window. The
setVisible(true)method of JDialog blocks till the dialog is disposed (usually by callingsetVisible(false)from inside the dialog)After that you can load all data by your
getRelevantData()method.