Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

Need help about KeyListener in jTextArea

Sun May 03, 2009 8:52 am

Handling KeyListener with JTextArea:
I have a problem with my program in here. I am making a chatting application and i just need a final touch. This is the code snippet i make to represent my problem. First, i'd like to tell that i want to make the sending process like the one in yahoo messenger. So i use TextArea for the sending object (to support multiline).
The output i desire is, each time i press enter, the text in txtArea will b sent to lblDisplay and each time i press Ctrl+Enter, the text in txtArea will have to move to the next line (just like in YM).
But, in here, each time i press enter, the cursor move to the second line instead to the first line (you can test the code) and that create one blank line each time i send to the messageArea in my chatting application. But if i use the button to send it (instead of enter), the cursor is normal (it's not moving to the second line). I duno how to manipulate that. And the other problem is that, the Ctrl+Enter is not working as i wish. Would anyone please give me a hand so that i can complete my project. It's still pending just because of this minute problem. Thank you very much for the help.
Code:

import java
.awt.event.*;
import javax.swing.*;

public class TextAreaCtrlEnter extends JFrame implements KeyListener, ActionListener{

    JLabel lblDisplay;
    JTextArea txtArea;
    JButton btnSend;
    JPanel panelObject;

    public TextAreaCtrlEnter() {
        panelObject = new JPanel();
        getContentPane().add(panelObject);
        lblDisplay = new JLabel("");
        txtArea = new JTextArea(5,20);
        btnSend = new JButton("Send");
        panelObject.add(lblDisplay);
        panelObject.add(txtArea);
        panelObject.add(btnSend);
        setVisible(true);
        setSize(250,300);

        txtArea.addKeyListener(this);
        btnSend.addActionListener(this);

    }

    //KeyListener Interface
    public void keyTyped(KeyEvent e) {}
    public void keyReleased(KeyEvent e) {}
    public void keyPressed(KeyEvent e) {
        if (e.getSource() == txtArea) {
            if ((e.getKeyCode() == KeyEvent.VK_ALT) && (e.getKeyCode() == KeyEvent.VK_ENTER)) {
                txtArea.setText(txtArea.getText());
                txtArea.setText("\n");
            }
            if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                lblDisplay.setText(txtArea.getText());
                txtArea.setText("");
                txtArea.requestFocus();
            }

        }
    }

    //ActionListener Interface
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == btnSend) {
            lblDisplay.setText(txtArea.getText());
            txtArea.setText("");
            txtArea.requestFocus();
        }
    }

    public static void main(String args[]) {
        TextAreaCtrlEnter obj = new TextAreaCtrlEnter();
    }
}
 


If you want to find a complete project find it here:
finished-projects/java-chat-t644-20.html



Post a reply