Domanda

I have to copy the contents of a jtextarea to another jtextarea.How can it be done.I have done the following:But this program is writing the text form one jtext area to another character by character. I want it to copy it when the user hits the enter key(carage returm.how do i do it? Thanks in advance.

import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.JTextComponent;

public class JtextareaTest {

    static JTextComponent jtextArea1;
    static JTextArea jtextArea2;

    public static void main(String[] args) {
        jtextArea1 = new JTextArea(10, 20);
        jtextArea2 = new JTextArea(10, 20);
        jtextArea1.setEditable(true);
        jtextArea2.setEditable(true);
        MyDocListen listener = new MyDocListen();
        jtextArea1.getDocument().addDocumentListener(listener);
        JFrame frame = new JFrame("JTextArea");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //Create and set up the content pane.
        Container newContentPane = frame.getContentPane();
        JPanel jpanel = new JPanel();
        jpanel.add(jtextArea1);
        jpanel.add(jtextArea2);
        newContentPane.add(jpanel, BorderLayout.CENTER);
        frame.setContentPane(newContentPane);
        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }
}

class MyDocListen implements DocumentListener {

    @Override
    public void insertUpdate(DocumentEvent e) {
        getText();

    }

    @Override
    public void removeUpdate(DocumentEvent e) {
    }

    @Override
    public void changedUpdate(DocumentEvent e) {
    }

    public void getText() {
        StringBuffer sb = new StringBuffer();
        String str = JtextareaTest.jtextArea1.getText();
        sb.append(str);
        JtextareaTest.jtextArea2.setText(sb.toString());
    }
}
È stato utile?

Soluzione

A DocumentListener is used for detecting changes to a text document. You want to use it when you want something to happen every time the text changes.

You said that you want something to happen every time the user pushes the enter key. Because of this, as @camickr rightfully pointed out, you should consider using a Key Binding instead. Read up on the doc page, as that is the most correct way to handle hot keys in a JComponent.

The quick-and-dirty way to do it would be to use a KeyListener instead, which listens for key presses at a low level. Note that this is not the most correct way to do it, and that it may make your code difficult to maintain.

If you do wish to use a KeyListener, then do the following: change your MyDocListen class to some other class that implements KeyListener, and implement KeyListener.keyTyped() to check if it was the enter key that was pushed, and if it was, call getText():

class MyKeyListener implements KeyListener {
    public void getText()
    {
        StringBuffer sb = new StringBuffer();
        String str = JtextareaTest.jtextArea1.getText();
        sb.append(str);
        JtextareaTest.jtextArea2.setText(sb.toString());
    }

    @Override
    public void keyPressed(KeyEvent e) {}

    @Override
    public void keyReleased(KeyEvent e) {}

    @Override
    public void keyTyped(KeyEvent e) {
    if (e.getKeyChar() == KeyEvent.VK_ENTER) {
            getText();
        }
    }
}

Then just change the line

MyDocListen listener= new MyDocListen();

to

MyKeyListener listener = new MyKeyListener();

and be sure to add the KeyListener to the JTextArea itself, not its document. To do this, replace this line:

jtextArea1.getDocument().addDocumentListener(listener);

with this:

jtextArea1.addKeyListener(listener);

and that should fix your problem.

Altri suggerimenti

or use this->

private void jButtonKeyTyped(java.awt.event.KeyEvent evt) {     
    char c == evt.getKeyChar();
    if(c==KeyEvent.VK_ENTER){
    yoursecondtextarea.setText(yourfirsttextarea.getText());
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top