Java

Jul
9th

Save User Preferences with Java’s Preferences API

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.

Continue reading »

Posted in Java | Leave a comment
Tags:
Jun
9th

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.

Continue reading »

Posted in Swing | Leave a comment
Tags:
Jun
8th

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.

Continue reading »

Posted in Swing | 2 Comments
Tags:
Jun
8th

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.

Continue reading »

Posted in Swing | Leave a comment
Tags: ,
Jun
1st

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.

Continue reading »

Posted in Java | Leave a comment
Tags: ,
May
6th

JLabel Hyperlink

Since there isn’t a JHyperlink class in Java Swing, I made one by extending JLabel.

Continue reading »

Posted in Swing | Leave a comment
Tags: , ,
Jan
12th

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.

Continue reading »

Posted in Java | Leave a comment
Tags:
Jan
5th

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.

Continue reading »

Posted in Java | 5 Comments
Tags: ,
Dec
28th

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

Continue reading »

Posted in Java, Security | Leave a comment
Tags: , ,
Dec
28th

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.

Continue reading »

Posted in Data Structures & Algorithms, Java | Leave a comment
Tags: , ,