Getting multiple lines of output trying to copy from JTextField to console

I am working on this program which connects to a remote server via telnet or SSH (user can choose the connection type) and the output is output to the console (default System.out

).

I wrote code to write to JTextField

everything that was written to the console.

What I wanted to do next was to try and make it so that the user can also Enter their own commands through JTextArea

, and whenever they hit the enter button, the code they typed will be sent.

For my own debugging purposes, I wanted to print to the console first the user's login to make sure I was sending the correct data.

I have this little piece of code here.

// jtxt is a JTextField i defined earler in code
// and the variable J is a String.

jtxt.addKeyListener(new KeyAdapter() {
    public void keyTyped(KeyEvent ke) {
        if (ke.getKeyChar() == ke.VK_ENTER) {

            j = jtxt.getText();
            System.out.println(j);
        }
    }
});

      

The only problem I ran into was I tested it myself and my jtextarea had the text "admin", so string j = "admin"

but when I printed it to the screen, it would print a million words "admin". "Any ideas? BTW, theres no loop even next to addKeyListener.

+3


source to share


1 answer


The problem is the user is holding down the enter key for too long. Your computer will recheck for keyTyped () while the current keypress is still in progress. Change the method name from keyTyped to keyReleased. This should fix your mistake.



0


source







All Articles