Total members 11893 |It is currently Wed Nov 13, 2024 12:42 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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




Author:
Newbie
User avatar Posts: 16
Have thanks: 0 time
Post new topic Reply to topic  [ 1 post ] 










Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com