Bumpy Bumpy - 6 months ago 21
Java Question

return value after the class is terminated

Good day.

I am making a JPanel class which contains controls for the user to input.
The JPanel class will be called by the Parent class and it is inserted to a JDialog.

I figured that I can pass the value I wanted through setDialogData method so I can customize the texts of the labels. but here's the question. I have a save button inside that JPanel and I'm trying to figure out, How will I send the data over the the Parent class before the JDialog closes?

public class Dialog_Input extends javax.swing.JPanel {

public Dialog_Input() {
initComponents(); //lets assume this has two buttons and a couple of controls..
}
public void setDialogData(String id, String name, String desc){
lblDialog_ID.setText(id);
lblDialog_Name.setText(name);
lblDialog_Desc.setText(desc);
}

Answer

First, you should add a onClose listener - which sets the variables on close... After that add some variables in your parent class which will be set when the Dialog Closes, and setup some setters. after that add a "Parent-class-object" variable into your child-class and add this object to the constructor In the constructor set the class JPanel to the passed JPanel like this:

public class Dialog_Input extends javax.swing.JPanel {

private JFrame/*or other Object of parent Class*/ parentFrame; 

public Dialog_Input(JFrame parent) {
    parentFrame = parent;
    initComponents(); //lets assume this has two buttons and a couple of controls..
    //setup onCloseListener
}
public void setDialogData(String id, String name, String desc){
    lblDialog_ID.setText(id);
    lblDialog_Name.setText(name);
    lblDialog_Desc.setText(desc);
}

//method being called on close
private void onClose(){
    parentFrame.setA(settingA);
    parentFrame.setB(settingB);
    parentFrame.setC(settingC);
}

finally call the JPanel/Dialog with this as object to be passed

Comments