tags:

views:

344

answers:

3

i m having 2 jframes.in 1 jframes,i am giving input in jtextarea,that should be displayed in another jframe jtextarea.how can i get that. i m using netbeans,i have designed jframes using swing. can anyone help me.

A: 

The following code perhaps illustrates that better than any cryptic explanation that previously stood above: :)

public class SourceFrame extends JFrame {

    private final JTextArea sourceArea = new JTextArea();
    private TargetFrame targetFrame; // somehow set this one

    public void addDocumentListener(DocumentListener listener) {
        sourceArea.getDocument().addDocumentListener(listener);
    }

    public void removeDocumentListener(DocumentListener listener) {
        sourceArea.getDocument().removeDocumentListener(listener);
    }

}

public class TargetFrame extends JFrame {

    private final JTextArea targetArea = new JTextArea();
    private final SourceFrame sourceFrame; 

    public void init() {
        sourceFrame.addDocumentListener(new DocumentListener() {
            public void changedUpdate(DocumentEvent event) {
                Document doc = event.getDocument();
                targetArea.setText(doc.getText(0, doc.getLength()));
            }
            public void insertUpdate(DocumentEvent event) {
                Document doc = event.getDocument();
                targetArea.setText(doc.getText(0, doc.getLength()));
            }
            public void removeUpdate(DocumentEvent event) {
                Document doc = event.getDocument();
                targetArea.setText(doc.getText(0, doc.getLength()));
            }
        }
    }

}

Ideally, one would have a public method for setting the text in TargetFrame and implement the listener somewhere else so none of both frames does need to reference the other one.

Mathias Weyel
there is no such method as addDocumentListener. Furthermore this is not the appropriate way to use the observer pattern in this case
Bozho
Whoops, forgot the getDocument() inbetween. As for the code, it wasn't intended to be appropriate but more to be short.
Mathias Weyel
then edit your answer so that I can undo my downvote
Bozho
[x] done edit. I have merged our approaches.
Mathias Weyel
A: 

The proper way to do this is using the Observer pattern (also called listener). Generally, the two main frame shouldn't know of the dependent frame. It should provide a way for notification whoever is interested. So in code:

public class SourceFrame extends JFrame {

    private final JTextArea sourceArea = new JTextArea();
    private TargetFrame targetFrame; // somehow set this one

    public void addTextChangeKeyListener(KeyListener listener) {
         sourceArea.addKeyListener(listener);
    }
    // a method to remove the listener might be required as well
}

And then in the TargetFrame you subscribe for key events that occur in the source text area:

public class TargetFrame extends JFrame {
    private final JTextArea targetArea = new JTextArea();
    private SourceFrame sourceFrame; // set this via constructor

    // this is called on initialization, perhaps from the constructor. 
    // Check what NetBeans has generated
    public void init() {
        sourceFrame.addDocumentListener(new DocumentListener() {
        public void changedUpdate(DocumentEvent event) {
            Document doc = event.getDocument();
            targetArea.setText(doc.getText(0, doc.getLength()));
        }
        public void insertUpdate(DocumentEvent event) {
            Document doc = event.getDocument();
            targetArea.setText(doc.getText(0, doc.getLength()));
        }
        public void removeUpdate(DocumentEvent event) {
            Document doc = event.getDocument();
            targetArea.setText(doc.getText(0, doc.getLength()));
        }
    }
}

The e.getSource() returns the component that created the event.

Bozho
Well, regarding your "appropriate" statements below my answer, let me point out that I would consider a DocumentListener a debatable thing and the listener in the other class with the cast to a concrete JTextArea is no good idea in any case.We could try a compromise, taking your architecture and my DocumentListener. The DocumentEvent lets you get the Document and from there the text can be retrieved so no need to rely on the type of text component used.
Mathias Weyel
ok, fair enough.
Bozho
+1  A: 

You might simply share the the same Document instance between two text components: thisTextArea.setDocument(thatTextArea.getDocument())

uaaquarius
OMG, sometimes it's just overlooking the forest because of all those trees...That's really how it should be done, yes!
Mathias Weyel