Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Making custom TextField, need help with backspace
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Bo0sT




PostPosted: Fri Jul 25, 2008 4:35 am   Post subject: Making custom TextField, need help with backspace

Hey, i am in the process of making an applet rpg and I want the player to be able to enter their name, however i have not been able to put a Scanner, BufferedReader or TextField on my applet screen "public void paint (Graphics)", so i decided to make my own TextField, it is going well, i used KeyListener to gather keyboard input and add it to a String object as the user types in a name, but one little problem.

*I can't get backspace to work, all i need it to do is subtract one letter from the end of the String, and its not working, this is a snip-it of code from my keyPressed method...

code:

StringBuffer removeLastLetter = new StringBuffer (name); // String is converted to StringBuffer so that it is easier to work with
removeLastLetter = removeLastLetter.deleteCharAt(removeLastLetter.length()); // StringBuffer deletes last letter in String
name = removeLastLetter.toString(); // StringBuffer is converted back to String
repaint(); // Paint method containing "g.drawString(name,100,110);" is refreshed


for some reason or another this isn't working and i dont feel comfortable posting my entire code, can anybody please help me? if there is an easier way to do this or w/e?

P.S: if u have an answer please PM me because i dont know wen i will be bak on this board, thanks vary much

P.P.S: don't mean to be rude and i do appreciate any help u guys can give me but.... please dont post if u dont have an answer, thanks.
Sponsor
Sponsor
Sponsor
sponsor
Dan




PostPosted: Fri Jul 25, 2008 4:53 am   Post subject: RE:Making custom TextField, need help with backspace

It might be a better idea to find out why TextFields are not working in your applet rather then trying to make your own and since you do not want to post your code we can't help much with that.

As for your backspace problem this is also hard to debug with out seeing all your code or even where this snip is from. However toy might want to check out the java doc for StringBuffer and the function deleteCharAt (like where it says "Throws: StringIndexOutOfBoundsException - if the index is negative or greater than or equal to length()."). Remember in java indexs start at 0.

P.S. I (and others) will not awnser programming questions via PM or e-mail becues it is not just you that can learn from the help but any one who has the same problem and happens to find your post.

P.P.S. You can have the forum send you a e-mail when there is a reply to your post.
Computer Science Canada Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
Bo0sT




PostPosted: Fri Jul 25, 2008 1:50 pm   Post subject: Re: Making custom TextField, need help with backspace

ok, thanks for the answer. but could u help me with this

say u had an assignment that asked you to get to user to enter a word, and then the program outputs the word without the last letter. do you think u could solve that? i think that would answer my questing

tyvm
DemonWasp




PostPosted: Fri Jul 25, 2008 2:02 pm   Post subject: RE:Making custom TextField, need help with backspace

To remove the last character from a String object in Java you could just do:

Java:
myString = myString.substring ( 0, myString.length - 2 );


Or something like that. The -2 might be off, but I don't think so.

Either way, Tony is definitely right. It will be WAY easier for you to determine exactly why you can't get a TextField to display in the Applet, rather than making your own. Building custom GUI components is a good exercise, but generally poor practice, because most libraries are better than what you'd write on your own*. Plus, inevitably you will run into something else you want to do in the Applet that won't work immediately, and you'll be back to : make it work in the Applet, or make your own. The work to just get existing stuff working together is generally WAY less than the work to DIY.

* Here I don't include the efforts of the Eclipse group on the Standard Widget Toolkit (SWT), because that stuff is awesome.
Bo0sT




PostPosted: Fri Jul 25, 2008 2:12 pm   Post subject: Re: Making custom TextField, need help with backspace

i tried that, it doesnt work, and as for my TextFields wont work, i think its because im painting other stuff on the screen and i guess the TextField has its own buffer or w/e

actually if you could put an example of how to put a TextField on a paint applet with some g.drawStrings(); ect on the same screen that would be vary, vary much appreciated, thank you.
Vermette




PostPosted: Fri Jul 25, 2008 2:57 pm   Post subject: Re: RE:Making custom TextField, need help with backspace

DemonWasp @ July 25th 2008, 14:02 wrote:

Java:
myString = myString.substring ( 0, myString.length - 2 );

Or something like that. The -2 might be off, but I don't think so.


That would do exactly what he wants, But if length <2 (or in general endIndex parameter is > length) you'd get an IndexOutOfBoundsException.

Are you forgetting to add the TextField component to your container?
Bo0sT




PostPosted: Fri Jul 25, 2008 4:24 pm   Post subject: Re: Making custom TextField, need help with backspace

i added it and did setVisibe(true); all that, and it just doesnt show, i also tried the code u posted, it works but i took a diffrent approach that also works. the thing is it keeps looking and it, letter by letter, deletes the whole word, but i think i can figure that out, thanks for alll the help, i really do appreciate it
Dan




PostPosted: Fri Jul 25, 2008 5:56 pm   Post subject: Re: RE:Making custom TextField, need help with backspace

DemonWasp @ 25th July 2008, 2:02 pm wrote:

Either way, Tony is definitely right. It will be WAY easier for you to......


Despite rumors you may have heard, Tony and i are not the same person Razz


@Bo0sT, if you are still interested you can find out more about using text fields here: http://java.sun.com/docs/books/tutorial/uiswing/components/textfield.html

We would still be more then willing to help you get it working if you posted more of your code.
Computer Science Canada Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Mon Jul 28, 2008 8:42 am   Post subject: RE:Making custom TextField, need help with backspace

Whoops, sorry Dan. I guess my brain mis-parsed your avatar as looking like Tony's. Such is my mind at times.

On-topic, it occurs that sometimes GUI libraries require a call to finalize() or similar methods before they'll render. Boost, were you able to get other components to render, or not?
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: