views:

87

answers:

3

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.

+1  A: 

Best way is probably to pass that object to another class using it's constructor or proper setter method.

public void setWindow(Window w) {
  this.w = w;
  // now you can use "w" variable in whole class
}
Xorty
+2  A: 

I don't know if this is the Swing-standard way of doing things, but what I do when the parent window needs to be notified when a child window performed some action is to create my own listener interface for that child window. Then, I create a class that implements the interface in my parent window and pass an instance of that class to the child window. The child then calls the interface method when the particular action is performed. So in your situation, I would do the following:

(Note: It's better to use ActionListener instead of MouseListener for handling button clicks.)

Frame2Listener interface

public interface Frame2Listener{
  void theButtonWasClicked(String textBoxValue);
}

Frame1 (parent)

private void addButtonMouseClicked(java.awt.event.MouseEvent evt) {
  w = new window(new Frame2Listener(){
    public void theButtonWasClicked(String textBoxValue){
      TextFieldOn1stFrame.setText(textBoxValue);
    }
  });
  w.setVisible(true);
}

Frame2 (child)

public window(final Frame2Listener fl){
  button1.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
      fl.theButtonWasClicked(texto.getText());
    }
  });
}
public void setHandler(MouseListener ml){
  //delete this method
}
public String getString(){
  //delete this method
}

I'd love to hear from any Swing experts whether this is a good technique or not.

Hope you're liking Java! ;)

Michael Angstadt
Thank you !! it seems like a util solution ...
Rodrigo Zuñiga
+1  A: 

Well, I don't have enough rep to comment on mangst's solution, but wanted to add that with your usage of the words elegant and efficient, this seemed like a prime opportunity for some design patterns.

What mangst has described is an implementation of the Observer pattern (http://en.wikipedia.org/wiki/Observer_design_pattern). I would, however, suggest having a collection of objects implementing the interface as suggested by mangst. This way, upon a given update you are able to iterate through many observers to perform some action in each, whether that is changing data, presentation, etc. Should multiple windows ever need to receive the data/action from your child window it's simple to just add another to the list of Observers you already have.

There is also the Mediator pattern (http://en.wikipedia.org/wiki/Mediator_pattern) which could accomplish similar results.

There are a lot of cookie cutter approaches for very common computing problems, and by familiarizing yourself with the lexicon, you can easily implement tried and true approaches.

apiri
i was thinking of somethink like that !!thank you for the reference to the Designs pattern !! i should look into that first (is just that i was expecting some "direct java" solution, I suppose it's not that easy)And its way more elegant than my 2 first solutions !!Thank you very much!
Rodrigo Zuñiga
You're welcome!
apiri