Posts in the “java” category

Java on MacOS: A default Mac menubar without a JFrame

Yesterday I ran into a pretty obscure situation in my Imagen application where I needed to show a modified Mac menubar, even though I didn't have a Java JFrame displayed at the time. The way Imagen works is that I show a JFrame, then hide it, and the application then waits for the user to do something with the application icon in the Mac Dock, typically dropping an image onto that icon.

Ant classpath: How to build a classpath variable in an Ant script

Summary: An Ant classpath example.

Here's a quick example showing how I typically build a variable to represent my classpath in an Ant build script.

Our Ant classpath example

This snippet of code below shows how I use the Ant fileset task to  create a variable named class.path by including all jar files from my lib directory using the pattern **/*.jar. This syntax can be read as "Include all files named *.jar in the lib directory and all of its sub-directories".

Java JLists: How to render images in JList cells

Here's another JList example, this time sharing some JList code I use to render images and text in JList cells.

Before going on, if you just want to display a set of images in a JList, all you have to do is create your images as an array Icons, and then add this array to your JList, and you're done. The JList is smart enough to render a Java Icon as an image, so in the most simple case, that's all you have to do.

Java: How to copy an image to the clipboard

Java Swing clipboard FAQ: How do I copy an image to the clipboard in a Java/Swing application?

Last night I needed to do just this, get an image that I'm currently displaying in a Java JFrame, and copy that image to the clipboard. I won't discuss the solution here too much, other than to say that in the example source code below, a MetaFrame is a customized subclass of a JFrame, and when I call the getCurrentImage() function, it simply returns a Java Image type.

Here's the source code:

A Java tuple class (Tuple2 or Pair, if you prefer)

After working with Scala for a long time, I had to come back to Java for a while to work on an Android app. Right away I missed a lot of things from the Scala world, including all of the built-in Scala collection methods, and other things as simple as the Scala Tuple classes.

If you haven’t used them before, a Scala Tuple class lets you write code like this:

Tuple<String, Integer> t = new Tuple<>("age", 41);

If you’re comfortable with generics, the Java implementation of a Tuple class like this is simple:

A Java “approximately equal” method (function)

As a quick note, here’s the source code for a Java “approximately equal” function that I use in an Android application:

/**
 * determine whether two numbers are "approximately equal" by seeing if they
 * are within a certain "tolerance percentage," with `tolerancePercentage` given
 * as a percentage (such as 10.0 meaning "10%").
 *
 * @param tolerancePercentage 1 = 1%, 2.5 = 2.5%, etc.
 */
public static boolean approximatelyEqual(float desiredValue, float actualValue, float tolerancePercentage) {
    float diff = Math.abs(desiredValue - actualValue);         //  1000 - 950  = 50
    float tolerance = tolerancePercentage/100 * desiredValue;  //  20/100*1000 = 200
    return diff < tolerance;                                   //  50<200      = true
}

You call the function like this:

boolean closeEnough = approximatelyEqual(1000, 950, 20);

Hopefully the comments describe the function well enough, so I’ll leave it at that, except for two related notes:

How to print an exception stack trace using Log4J (or Commons Logging)

Log4J exception FAQ: "How do I print the stack trace of an exception using Log4J or Commons Logging?"

Printing the stack trace of a Log4J exception seems to be something of a trick question. In reviewing Java code from different developers at different organizations I see a lot of people working very hard to print a stack trace using Log4J, including a lot of variations of calling e.printStackTrace() method. In this short tutorial I’ll show how to solve this problem.

A Java WeakHashMap class example

I ran across some Java source code today that I thought I should share. This code demonstrates how to use the Java WeakHashMap class, which can serve as a nice cache some times. I particularly like how this class responds to available memory and the Java garbage collection system.

Let me share the Java source code here first, and then I'll provide a description of how it works:

JOptionPane showInputDialog examples

I thought I'd share a collection of JOptionPane showInputDialog examples today. I'll start with the easiest example first, then try to add a little complexity as we go along.

First up, here's a simple JOptionPane showInputDialog example where I display a dialog that prompts a user to enter their name:

Mac Java: How to open a URL in a browser

A quick note here that if you need some Java code to open a URL in the default browser on a Mac OS X system, I just ran across this Java method in one of my programs that should do the trick:

A Java FileReader class example

Java file FAQ: Can you demonstrate how to use the Java FileReader class?

I have a number of examples on this site about how to read a file with Java, but I've never dug into it from the aspect of how and why to use a Java FileReader, so I thought I'd take a few moments and dig into the FileReader class in this article.

The Java FileReader class

The FileReader javadoc provides a nice introduction to the FileReader class:

How to copy a file in Java

The easiest way to copy a file in Java is to download the Apache Commons IO library; just download their library, then use the methods of their FileUtils class to copy a file.

However, if you're just as interested in the technical details of how to copy a file in Java, or just want a method to copy a file in Java, the method below, taken from my Java file utilities class, shows how this actually works:

Java JDBC drivers and driver downloads

In need of a Java JDBC driver for your database? I thought I'd put together a quick list here showing the URLs where you can download the latest JDBC drivers for databases like Postgresql (Postgres), MySQL, and Microsoft SQL Server.

MySQL JDBC driver

MySQL Connector/J is the official MySQL JDBC driver. Here's a link to their JDBC driver: