Your Computer Science Resource

Archive for June, 2009

Jun
9th

HOW-TO: Center a JFrame On The Screen

If you want to add a nice touch to your GUI application, have your JFrame automatically center on the user’s screen. Not only does this looks much better than having it start up in the default (0, 0) location, but it takes very little coding effort to make this happen. There are two methods for doing this, depending on which version of Java you are using. I will show you how to do it both ways, though most of you can follow the first method.

(more…)

Jun
8th

HOW-TO: Detect link clicks in JEditorPane

If you are using a JEditorPane to display HTML, and that HTML contains links, there is no built-in function to automatically detect if the user clicks on a link. What’s the solution to this problem? Create a HyperlinkListener. Here’s how…

(more…)

Jun
8th

HOW-TO: Java Gradient Backgrounds

Nowadays, if you are developing a GUI application, it looks more “professional” if your window’s background is gradient (i.e. the background fades from light to dark, or dark to light). This gives your application a more polished look and can greatly improve the interface without much effort. Here’s how to do it…

(more…)

Jun
1st

HOW-TO: Retrieve a File’s extension in Java

For some strange reason, there’s no getFileExtension() or getExtension() method in Java’s File class (perhaps because Files do not necessarily always have extensions in every platform?). Nevertheless, that doesn’t mean we can’t do it on our own. All it takes is one line of code!

(more…)