Java Validated FileChooser

Fri, Nov 4, 2011 - 10:55am -- Isaac Sukin

I happened to be working on a Java project recently where I needed to let the user save files through the GUI. The Swing toolkit has a nice JFileChooser class that lets you show a file save dialog so that the user can choose the directory and name of the file to save. However, not all filenames are valid, and Java doesn't validate them for you by default. If you try to save a file with an invalid name, Java will throw an error, and this is often considered the only way to know if the filename is valid. That's bad practice though since you should never rely on an exception being thrown as a condition of your program running correctly; by definition, exceptions are unreliable and sometimes unpredictable. Additionally if your file saves successfully then you have to immediately delete it (because you were only saving it to test the filename) and that's messy. So I wrote a ValidatedFileChooser class that checks various criteria to make sure that filenames are valid before attempting to save the file, and alerts the user if one of the criteria fails. The class is below, and I'm releasing it to the public domain.

Counting Characters in a Textarea with JavaScript

Tue, May 17, 2011 - 4:01am -- Isaac Sukin

About a year ago, I wrote about how I was using the JavaScript onKeyPress event to count the characters in a textarea. It turns out that among newer browsers, the trick I employed only works in Firefox. Other browsers (IE, Chrome, Safari, Opera) don't register keypress events for non-character keys like Control and Backspace. KeyUp and KeyDown have their own problems, but we can get around this by updating the character counter on both the KeyDown and KeyUp events. I've built a test below so that you can try it for yourself.

Fancy jQuery Slide-Out Effects for Large Page Elements

Wed, Aug 25, 2010 - 12:04pm -- Isaac Sukin

Sometimes, there are things in my blog posts that just don't fit nicely into the width of the content area. This is a problem with code snippets and images in particular; I only have a certain amount of horizontal space, but often that's not enough.

Inspired by a solution I witnessed in action at (and the place Lullabot discovered it, I finally decided to solve this problem using some fancy jQuery. Now, all code blocks on this site fit correctly into the content area, with any excess text hidden until your mouse hovers over the code block. All images are automatically shrunk, until your mouse hovers over them, at which point they will enlarge to their original size. Pretty sweet! And it's all cross-browser-compatible.

Counting characters with the JavaScript KeyPress event

Wed, Jul 28, 2010 - 3:38pm -- Isaac Sukin

Update: It turns out that this still isn't the best way to count characters in JavaScript. Updated post here.

I wrote the character counter for my Facebook-style Statuses module a long time ago. I originally wrote it using the "onKeyPress" JavaScript event, but that had problems with pressing non-letter keys, so I switched to using the "onKeyUp" event and never looked back. But this week a new issue appeared in my queue rightly pointing out that the character counter doesn't update while a key is being held down, even though new letters are being added to the textbox. So (with much more knowledge and experience under my belt this time) I dove back in and investigated. Here's what I found: the keypress event is called at an awkward time, when symbols for e.g. the backspace key are still being processed as part of the length of the string in the textarea but before they get processed as the removal of a character.

Subscribe to RSS - snippet