I'm programming a desktop application in JAVA usin NetBeans.
My question is this:
1st: I have a Jframe that is the principal frame of the application.
2nd: In some moment I want to create a new object of some kind, so I press addButton y create a new JFrame (I create a new class window that extends Jframe) with some text fields in it.
private void addButtonMouseClicked(java.awt.event.MouseEvent evt) {
w = new window();
w.setVisible(true);
}
this is a screenshot simplifie with a simple string instead of a full class http://img820.imageshack.us/img820/3361/screenshotlw.png
3rd: In this new window I read the text fields and create the object when press some button.
Finally when a press the accept button i want the new frame to get the object in the 2nd frame.
QUESTION: what is the most elegant/eficcient/easy/better way of getting the object in the 2nd frame from the firs one?.
MY FIRST SOLUTION was to create a static method setNewData() on the first windows, and the second windows calls this method when you press the button.
Now I came with a NEW SOLUTION: On the second frame I HAVE a method to set a mouselistener on the button. And a getString() FUNCTION.
On the first frame i got this:
private void addButtonMouseClicked(java.awt.event.MouseEvent evt) {
w = new window();
w.setHandler(ml);
w.setVisible(true);
}
MouseListener ml = new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
TextFieldOn1stFrame.setText(w.getString());
}
};
And on the second frame i got this
public void setHandler(MouseListener ml){
button1.addMouseListener(ml);
}
public String getString(){
return texto.getText();
}
SORRY FOR THE LONG TEXT I'M NEW ON JAVA AND DON'T KNOW IF THIS SOLUTIONS ARE THE BEST
BTW SORRY ABOUT MY ENGLISH TOO.