Your Computer Science Resource
Apr
25th

Simple XML Reader

Nowadays XML is a popular format for configuration files and transferring messages across networks. For reading node values from an XML file, I’ve created a small utility class. There are two useful methods in this class, getElementValue() and getElementValues(). In this article, I will briefly show how you can read in values from an XML file using this simple class.

Read the rest of this entry »

Oct
27th

HOW-TO: Wrap Strings in Java (Swing)

Ever wonder why there’s no method to wrap a String based on a certain number of characters in Swing? Me, too. Having the ability to wrap Strings can be especially useful if you don’t know how long your Strings are going to be, such as when you have translated text in a JOptionPane (or any other GUI element). Fortunately, we can write a nice utility class that does exactly what we need — StringWrapper.

Read the rest of this entry »

Aug
13th

Java Relative Date and Time

In an application I’m working on, I needed the ability to show the date and time in a relative manner. In other words, rather than displaying the full date and time like “August 13, 2009 at 3:07 pm”, I wanted to be able to determine when this date is relative to now and show this instead (e.g. “one hour ago” or “one hour from now”). The result? A simple yet useful class which converts a Calendar or Date object into a String representing the relative date.

Read the rest of this entry »

Jul
31st

PROJECT: TrayTweet v.2.0

Just updated my previous TrayTweet project to version 2.0. All those interested please click here for more information.

Jul
9th

PROJECT: TrayTweet

I recently published a free Twitter system tray client called TrayTweet. While the code is not open source, I will post some of the highlights.

Read the rest of this entry »

Jul
9th

HOW-TO: Save User Preferences with Java’s Preferences API

Users have certain expectations of your program. One of those expectations might be to have their preferences saved. This might include their username and password, the location of the application window, the size of the window, etc. Java’s Preferences API is an easy way to accomplish tasks like this without having to manually write and later read / parse a config file. This method is also platform independent; we don’t have to worry about where or how the data is saved — just that it is being saved and that it is persistent. Let’s see how to do it!

Read the rest of this entry »

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.

Read the rest of this entry »

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…

Read the rest of this entry »

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…

Read the rest of this entry »

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!

Read the rest of this entry »

Page 1 of 41234»