Your Computer Science Resource

Archive for the ‘Tutorials’ 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…)

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
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…)

Jan
12th

HOW-TO: Evaluate a Mathematical String Using ScriptEngine

Suppose you have a String containing some mathematical operation(s). For example, we need to perform the following calculation, but for various reasons, the data type is a String: (10*(34+92)-23)/4. Rather than figuring out ways to parse it (e.g. split or some fancy regex), we can use Java’s ScriptEngine to evaluate the function as is.

(more…)

Jan
10th

HOW-TO: Read a Text File Using BufferedReader

As simple as this may seem to experienced Java programmers, I still see many posts in the Java Forums from beginners asking this very question: How do you read a text file line-by-line using a BufferedReader? Let’s find out.

(more…)

Jan
9th

HOW-TO: Save a BufferedImage to Disk

Recently, I showed you how to take a screenshot and display it in a JFrame using Java’s Robot class. What if you wanted to save the image to the disk as a GIF, JPG, or PNG? It’s actually really easy. Let’s find out how!

(more…)

Jan
5th

HOW-TO: Save JTable to Excel Spreadsheet using Apache POI

Have you ever needed to save the contents of a JTable? One convenient way to save this data is to make it available as a Microsoft Excel spreadsheet. At first, this may not seem like an easy task, but with Apache’s POI API, we can do it without too much frustration.

(more…)

Page 1 of 212»