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.
This code is not working how you'd expect it to:
public void displayText(KeyEvent k){
txt = inputArea.getText();
outputArea.setText(txt);
if(k.getKeyCode() == 10) //return
outputArea.setText(txt + "\n");
}
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.
EDIT: Thanks; I forgot about those constants...
And unfortunately, it works - but not really. if(k.getKeyCode() == k.VK_ENTER)
returns true (this is a fact), but "\n" does not create the newline.
EDIT (take two): Full 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");
}
}
Then another class with the main:
public class Driver {
public static void main(String[] args){
GUI gui = new GUI();
}
}
EDIT: Basically, I'm trying to emulate Google Wave's ability to send text before the user presses Enter.