title: Java JTextArea - Adding a newline
body: <p>So I'm stumped. I, for some reason, seem to remember that Java does weird things when you try to add new lines to stuff... so I think that may be the problem here. </p>
  
  <p>This code is not working how you'd expect it to:</p>
  
  <pre><code>public void displayText(KeyEvent k){
      txt = inputArea.getText();
      outputArea.setText(txt);
      if(k.getKeyCode() == 10) //return
          outputArea.setText(txt + "\n");
  }
  </code></pre>
  
  <p>You can double-check to see if I have the getKeyCode() set to the right number, but I'm almost positive that that part is correct.</p>
  
  <p>EDIT:
  Thanks; I forgot about those constants...</p>
  
  <p>And unfortunately, it works - but not really. <code>if(k.getKeyCode() == k.VK_ENTER)</code> returns true (this is a fact), but "\n" does not create the newline. </p>
  
  <p>EDIT (take two):
  Full code:</p>
  
  <pre><code>import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;
  
  public class GUI extends JFrame implements KeyListener{
      private JFrame frame;
      private JTextField inputArea;
      private JTextArea outputArea;
      private String txt;
  
      public GUI(){
          frame = new JFrame("My Frame");
          inputArea = new JTextField(1);
          outputArea = new JTextArea(5,10);
          inputArea.addKeyListener(this);
          outputArea.setLineWrap(true);
          txt = "";
  
          frame.setLayout(new GridLayout(2,10));
          frame.add(inputArea);
          frame.add(outputArea);
          frame.setVisible(true);
          frame.pack();
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      }
  
      public void keyPressed(KeyEvent k){}
      public void keyTyped(KeyEvent k){}
  
      public void keyReleased(KeyEvent k) {
          displayText(k);
      }
  
      public void displayText(KeyEvent k){
          txt = inputArea.getText();
          outputArea.setText(txt);
          if(k.getKeyCode() == KeyEvent.VK_ENTER)
              outputArea.append("\n");
              //outputArea.setText(txt + "\n");
      }
  }
  </code></pre>
  
  <p>Then another class with the main:</p>
  
  <pre><code>public class Driver {
      public static void main(String[] args){ 
          GUI gui = new GUI();
      }
  }
  </code></pre>
  
  <p>EDIT:
  Basically, I'm trying to emulate Google Wave's ability to send text before the user presses Enter.</p>