Thu Apr 30, 2009 11:50 am
int key = e.getKeyCode();
if(e.getSource() == txtAreaSend)
{
if(key == KeyEvent.VK_ENTER)
{
sendMessage();
txtAreaSend.setText("");
}
}
Thu Apr 30, 2009 1:18 pm
[...]
if(key == KeyEvent.VK_ENTER && e.isAltDown() )
[...]
Thu Apr 30, 2009 1:49 pm
Sat May 09, 2009 4:01 pm
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.getKeyCode() != 10){
//
}
else{
if(e.isControlDown() && e.getKeyCode() == 10){
String strBefore = txtArea.getText();
txtArea.setText(strBefore+"\n");
}
else{
lblDisplay.setText(txtArea.getText());
txtArea.setText("");
int caretPosition = txtArea.getCaretPosition()-1;
txtArea.setCaretPosition(caretPosition);
}
}
}
//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();
}
}
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
Powered by phpBB © phpBB Group.