recent posts related to java, jdbc, spring, etc.

How to determine the current monitor size with multiple displays - Java Swing, JavaFX

Note: This code is currently a work in progress. I know of possible approaches, but I don’t know of a perfect working solution yet.

I’m currently trying to find the right way to find the current monitor size, when you’re writing a Java Swing application to work in a multiple-monitor configuration. I always use three monitors, so I can test this pretty easily.

A Java TimerTask, Timer, and scheduleAtFixedRate example

As a quick note, if you ever need to use a Java TimerTask, you can define one like this:

class BrightnessTimerTask extends TimerTask {
    public void run() {
        // your custom code here ...

and you can then instantiate it, create a Timer, and schedule the task like this:

How to cast a null value in Java

Until a little while ago I don’t think I had ever thought about intentionally casting a null value in Java, but then I ran into a problem and realized that the solution was to cast a null value, like this:

FileDialog d = new FileDialog((java.awt.Frame) null);

You have to do that in this case because FileDialog has several one-argument constructors, including one that takes a JFrame and another that takes a JDialog. If you just put null in the constructor the Java compiler or your favorite IDE will complain, so you have to cast the null value to one of those specific types, and this syntax shows how to do this. (My app uses multiple frames, and at the moment I’d rather put null in the FileDialog constructor than try to determine which frame is currently in the foreground.)

A Java FIFO queue class

In my spare time lately I’ve been writing this Android football game, and for the game I needed a simple FIFO queue. I haven’t used Java much lately, and after a quick Google search showed things that were more complicated than what I had in mind, I wrote my own FIFO queue.

A Java tuple class

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:

A Java method to determine if an integer is between a range

Last night I was writing some code for my Android football game, and decided it would be best if I had a between method, so I could write some code like this to show that I wanted to test to see if a number was between an integer range:

if (between(distance, 8, 10)) { ...

That could would be interpreted as, “If the distance is between the values 8 and 10, do whatever is in the code block.” (I can make that code more readable in Scala, but in Java I think that’s the best I can do.)

How do I round a float or double to an integer in Java?

Java math FAQ: How do I round a float or double to an integer in Java?

Solution: Use Math.round() to round a float (or double) to the nearest integer (int) in Java.

You can see how this works in the examples that follow, where the result is shown in the comment after each line: