Your Computer Science Resource

Archive for the ‘Programming’ Category

Oct
27th

HOW-TO: Wrap Strings in Java

Ever wonder why there’s no method to wrap a String based on a certain number of characters? 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). Unfortunately, there is no built-in function to do this within the String class. But of course, we can write a nice utility class that does exactly what we need — StringWrapper.

(more…)

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.

(more…)

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.

(more…)

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!

(more…)

Jun
9th

HOW-TO: Center a JFrame On The Screen

If you want to add a nice touch to your GUI application, center your window on the 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. Fortunately for us, 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.

(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. Of course, that doesn’t mean we can’t do it on our own. All it takes is one line of code!

(more…)

May
7th

HOW-TO: Use the Levenshtein Distance Algorithm

For an anti-plagiarism application I developed about a year ago, I needed a way to calculate the similarity between two strings. I wasn’t sure how to go about doing this at first, but then I came across the Levenshtein Distance Algorithm which gets the job done well. In this example, I’ll show you my Distance class implementing the Levenshtein Distance Algorithm, and how to use it to compare strings (specifically the minimum distance between two strings needed to make them equal). This is also very useful for determining how similar (or dissimilar) two strings are.

(more…)

May
6th

Java JLabel Hyperlink

Have you ever needed a hyperlink in your project? One convenient place for this is in the “About” dialog with a link to your website or e-mail. Unfortunately, there isn’t a JHyperlink class in the standard library, but we can make our own, right? Let’s see how to do it!

(more…)

Page 1 of 3123»