Java’s Preferences API is an easy way to save user preferences for your application without having to manually write/read a config file. This method is also platform independent so we don’t necessarily have to worry about where/how the data is saved or whether or not you have permission to do so.
Java
Save User Preferences with Java’s Preferences API
Centering a JFrame
It usually looks nicer to have your application start up in the center of the user’s screen rather than at the (0,0) coordinate. There are two methods to do this depending on which version of Java you’re using.
Detect link clicks in JEditorPane
If you’re using a JEditorPane to display HTML that potentially contains hyperlinks, there is no built-in function to automatically detect if the user clicks on a link. Creating a HyperlinkListener will solve the problem.
Gradient JPanel Backgrounds
Using gradient backgrounds in your application creates a clean and polished look. Doing so can really help improve the interface without too much effort.
Java File Extensions
In dealing with files, you’ll realize that there isn’t a method to get the extension which you sometimes need to appropriately handle certain file types.
JLabel Hyperlink
Since there isn’t a JHyperlink class in Java Swing, I made one by extending JLabel.
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.
Saving a JTable 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 an Excel spreadsheet. Using Apache’s POI, we can do this without too much frustration.
MD5 and SHA-1 Hashing
Java has a built-in MessageDigest class which makes it easy to implement an MD5 or SHA-1 hash (among others). Both of these algorithms produce one-way hashes (they cannot be changed back to their original form), and they are usually used to hash passwords or for checking the integrity of files (checksum).
Comparison of Sorting Algorithms
A while back I had to write a program that would compare different sorting algorithms in Java. The algorithms were Heap Sort, Merge Sort, and Quick Sort. The code for each of the algorithms is below — I’ll leave the analysis up to you.
