27.04.2013 Views

330 Java Tips.pdf - FTP Server

330 Java Tips.pdf - FTP Server

330 Java Tips.pdf - FTP Server

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Graphics, AWT, Swing II part<br />

else<br />

return false;<br />

Or<br />

you can test whether text = = null,<br />

then check if myJTextField.getText().length() == 0<br />

if it is not, do text=text.trim() to remove spaces,<br />

then check text.equals("") to see if it is an empty String<br />

--<br />

Lucio Benfante, Arnaud Berger and Jorge Jordao<br />

Q: Whats the difference between a Frame and a canvas? Can someone tell me<br />

the difference?<br />

Answer:<br />

A Frame is , well , a frame - a window, with borders,close/minimize/maximize buttons<br />

(usually), and can not contain other elements, such as a menubar ,buttons panels,<br />

etc.<br />

a Canvas is a user interface element (which you can place in e.g. a frame) onto<br />

which you can draw/display something..<br />

-by<br />

Nils O. Selåsdal<br />

Q: I know how to make a text box using Visual Basic that can only accept certain<br />

keys for example if I wanted to make a box where you can type only numbers in it or<br />

letters. However, when it comes to <strong>Java</strong> I have not been able to figure this out. Is<br />

there a way to do this and make a textbox where you can only type letters in it?<br />

Answer: First of all, 'ASCII' is not a synonym for 'text'. ASCII is just one of many ways<br />

of encoding text to numeric values. It is 7-bit and only includes values from 0 through<br />

127.<br />

Now, on to your question. With ASCII, 'numbers' and 'letters' are a little easier to<br />

check, since the only letters are 'A'-'Z' and 'a'-'z'. However, <strong>Java</strong> uses Unicode and<br />

has all sorts of fun extra characters.<br />

But you do have a few handy methods on the java.lang.Character class such as<br />

isDigit and isLetter<br />

Now, if you are using swing, then to achieve what I'm guessing you want is quite<br />

simple. Just hook a custom text document to the JTextField you're using and have it<br />

do the filtering.<br />

** Here's the part of The <strong>Java</strong> Tutorial that goes over it<br />

http://java.sun.com/docs/books/tutorial/uiswing/components/textfield.html#validation<br />

Here's someone's Swing FAQ on that<br />

http://users.vnet.net/wwake/swing/faq.html#text_check<br />

Here's a <strong>Java</strong>World article that describes how to do that to limit the length of input.<br />

Shouldn't be too hard for you to change to your specific needs.<br />

http://www.javaworld.com/javaworld/javaqa/2000-05/02-qa-0512-textbox_p.html<br />

All these links were from the first page of a search on http://www.google.com<br />

Google is your friend!<br />

file:///F|/350_t/350_tips/graphics-II.htm (4 of 6) [2002-02-27 21:18:10]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!