473,383 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Caret Position

hi, i am in the procces of writing a chat program for a main
Assigment, the problem im stuck on is one of the Carets position in a
JTextArea, when i hit the enter key, it appends the msg to the
conversation window but the caret in the msg area is at the start of
the next line, which in turn lets the enter key continualy send a
blank msg when its held down. I have a send button as well (as u do)
and i have that set inactive if the (!msgArea.equals("")) and the
caret goes to the very start when the button has been hit, but because
the caret is on the next line of the msgArea, the msgArea will never
be empty, ive tryed the setCaretPosition but that didnt seem to work
(i did if i set it to 1 but that lead to a NullPointerException ) So
its doing both: appending the message on the press of ENTER(good), but
doing the carrige return aswell (bad)

Heres some code:

public void keyPressed(KeyEvent ke)
{
send = msgArea.getText();
name = "They says: ";

if(ke.getKeyCode() == KeyEvent.VK_ENTER)
{
if(!send.equals(""))
{
sendMsg();
msgArea.setCaretPosition(0); //Dosn't work
}

else;
}
}

if i have poorly explainded this (probabal) explain what i havnt
expained well :S, otherwise ne advice appreciated :)
Jul 17 '05 #1
2 6272
On 7 Aug 2003 07:16:27 -0700, ne*************@hotmail.com (- ions)
two-finger typed:
hi, i am in the procces of writing a chat program for a main
Assigment, the problem im stuck on is one of the Carets position in a
JTextArea, when i hit the enter key, it appends the msg to the
conversation window but the caret in the msg area is at the start of
the next line, which in turn lets the enter key continualy send a
blank msg when its held down. I have a send button as well (as u do)
and i have that set inactive if the (!msgArea.equals("")) and the
caret goes to the very start when the button has been hit, but because
the caret is on the next line of the msgArea, the msgArea will never
be empty, ive tryed the setCaretPosition but that didnt seem to work
(i did if i set it to 1 but that lead to a NullPointerException ) So
its doing both: appending the message on the press of ENTER(good), but
doing the carrige return aswell (bad)

Heres some code:

public void keyPressed(KeyEvent ke)
{
send = msgArea.getText();
Maybe if you add .trim() to your .getTex():
send = msgArea.getText().trim();
This will cut any spaces or newlines from the beginning and from the end of
the string, so you can compare better below.
name = "They says: ";

if(ke.getKeyCode() == KeyEvent.VK_ENTER)
{
if(!send.equals(""))
{
sendMsg();
msgArea.setCaretPosition(0); //Dosn't work
Are you sure you don't want to clear the textarea after having added its
text somewhere else ?:
msgArea.setText("");
}

else;
}
}

if i have poorly explainded this (probabal) explain what i havnt
expained well :S, otherwise ne advice appreciated :)


Well, it was a bit jumbled, but I think I got the gist of it ;-)

Cheers.
Jul 17 '05 #2
On 8 Aug 2003 21:07:24 -0700, ne*************@hotmail.com (- ions)
two-finger typed:
Thanks mate, that seemed to do the trick, when enter is held down the
msgArea still does the carridge returns resulting in a blank line(s)
between the top and the text, but when it is eventualy sent only the
text is sent. i guess i can put up with that blank line tho :)


Actually, what you are describing here is normal for a (J)TextArea.

Enter is supposed to add an extra newline into the text area. Otherwise you
have to use a TextListener or the ActionListener you are already using, to
modify the text in the msgArea:
msgArea.setText(msgArea.getText().trim());

Cheers.
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Barry | last post by:
Retoring Caret Position after text field correction -------------------------------------------------------------------------------- Hi, my code has the following form - function...
4
by: torch | last post by:
I have trawled the internet for a solution to this one . All I need is the caret position as a number. I have seen quite a few text insertion functions that do something like this : ...
7
by: Paul Gorodyansky | last post by:
Hi, Say I have a text in my TEXTAREA box - 01234567890 I want - using script - insert say "abc" in the middle. Works almost OK in Internet Explorer (with one problem) based on their example...
4
by: cc | last post by:
Hi, Is possible set and get the caret position of cursor in a text field ? Thanks, Carlo
1
by: ryanmhuc | last post by:
Is there a way to get the caret position of a text input using FireFox. I am aware of ways to such with IE but have been unable to accomplish in FireFox and have not found a solution in the groups...
6
by: kurotsuke | last post by:
Hi, I need to know the position (X, Y cohordinates) of the caret. I'm using the function: private static extern int GetCaretPos(ref POINT lpPoint); I'm actually getting a result but I...
2
by: DaveR | last post by:
Can anybody help me to a)get the user's cursor insertion point (caret) from a multiline textbox, using client-side script I suppose, and then b)return the position to VB code behind? I have...
0
by: Oenone | last post by:
I would like to set the caret position in a (single-line) textbox to a position based on the number of pixels from the left of the control. For example, I want the caret to be positioned 50...
0
Jezternz
by: Jezternz | last post by:
Okay, so a while back I managed to retrieve it correctly. In this thread: http://bytes.com/topic/javascript/answers/786211-retrieving-caret-start-end-values#post3127765 However I have found...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.