how to access jtextarea from one jframe to other jframe
i m having 2 jframes.in 1 jframes,i am giving input in jtextarea,that should be display开发者_JAVA百科ed in another jframe jtextarea.how can i get that. i m using netbeans,i have designed jframes using swing. can anyone help me.
You might simply share the the same Document instance between two text components:
thisTextArea.setDocument(thatTextArea.getDocument())
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.
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.
精彩评论