Tag Archives: Java

Leiningen passing Invalid Flags to Java Compiler

Just a note about some weirdness in my work process and it’s solution.

A few weeks ago, I started noticing some weirdness in trying to use some tools with Leiningen while developing a program in Clojure. When running tools like kibit, lein would fail with an error from javac about an invalid flag. Initially these flags were for attempts to set the file encoding. And the file encoding kept changing.

Over time, these failures became more frequent. Always about passing an invalid flag to the java compiler. Finally, I was unable to run a REPL, run a program, or build an uberjar.

Long story short, it looks like it was related to the JAVA_CMD environment variable. That had been set a loooong time ago and in fact pointed to the java compiler, not the VM. Simply removing the variable fixed things. Might have worked just by setting it to java rather than javac but I didn’t try it and so far nothing else has broken.

Still don’t know what caused things to get flaky in the first place since this variable was set so long ago.

Keeping the JavaFX UI Responsive

It’s common knowledge that the JavaFX user interface tookit is single-threaded. When your JavaFX-based program is doing things that can take some time, you need to run those tasks on a separate thread(s) to keep the interface responsive.

Recently, I’ve been working on a program that can spend a lot of time reading and writing to the disk, but at the same time I want to retain the ability for the user to change views of the UI as the work proceeds. I also want to provide the opportunity for the user to cancel the background task at any time. I thought I would provide a couple of examples of how I did that in the program.

Continue reading

Using Local Java JARS in Clojure Projects

Recently, I’ve been working on a Sudoku game program. Part of the program provides a user with the ability to generate new puzzles of a particular difficulty. Generating a puzzle usually requires two puzzle solvers: one that solves puzzles (slowly) like a human would, the other that solves puzzles (very quickly) like a computer would.

Rather than write my own from scratch, for this part of the development, I wanted to use an existing implementation of the machine-like solver. After a little research (more on this some other time), I found one I liked a lot — the Kudoku solver written in Java from attractive chaos.

But how does one use a local jar file in a Clojure Project? Read on…

Continue reading

Finding Mono-Spaced Fonts in JavaFX

There are many use cases where a mono-spaced (fixed-width) font is useful in programming. Programming editors and creating program listings come to mind. But there doesn’t seem to be a consistent way of obtaining a list of all of the mono-spaced fonts installed in the operating system.

Back in the days of Swing, you usually had to grab a list of font families (e.g. Arial, Times New Roman, etc.) from AWT and then create a BufferedImage to print the font to and check layout widths. In JavaFX, it seems a bit easier. Here’s one way to do it.

 


/**
* Return a list of all the mono-spaced fonts on the system.
*
* @return An observable list of all of the mono-spaced fonts on the system.
*/
private ObservableList<String> getMonoFontFamilyNames() {

    // Compare the layout widths of two strings. One string is composed
    // of "thin" characters, the other of "wide" characters. In mono-spaced
    // fonts the widths should be the same.

    final Text thinTxt = new Text("1 l"); // note the space
    final Text thikTxt = new Text("MWX");

    List<String> fontFamilyList = Font.getFamilies();
    List<String> monoFamilyList = new ArrayList<>();

    Font font;

    for (String fontFamilyName : fontFamilyList) {
        font = Font.font(fontFamilyName, FontWeight.NORMAL, FontPosture.REGULAR, 14.0d);
        thinTxt.setFont(font);
        thikTxt.setFont(font);
        if (thinTxt.getLayoutBounds().getWidth() == thikTxt.getLayoutBounds().getWidth()) {
            monoFamilyList.add(fontFamilyName);
        }
    }

    return FXCollections.observableArrayList(monoFamilyList);
}

It seems a little less complicated in that all of the needed functionality is available right in JavaFX. One thing that hasn’t changed is that it can still be slow. For example, if you want to populate a ComboBox with all of the mono-spaced fonts at the start of program execution, it can add a small but noticeable delay.

In my own case, on Windows, I have about 370 fonts on the system. Twenty-seven of those tested as mono-spaced, including quite a few that I never use, mostly non-English character sets that get installed somehow.

Still, good to know.

Focus Behavior Change between JavaFX 2 and JavaFX 8 when Selecting Rows in a TableView

For a little while now, I’ve been working on an application that manages a list of documents, providing multiple views that the user can edit.

The application looks something like this:

 

2015_04_18_Main_Screen_Capture

The user selects the document they wish to view or edit by selecting it from the large TableView in the middle of the window. The area on the right provides controls to view and edit details. (The area on the left is for filtering the documents displayed in the central table.)

Based on some early advice, I had watchers on the focus property of the fields that could be edited. When a control lost focus, any changes were written to the database. The user didn’t have to do anything to save their work. It just happened.

This worked with Java 7 and JavaFX 2. After the switch to Java 8 and JavaFX 8, things were not quite the same. If a user was making a change somewhere and then selected another document without moving to another editing view, the data was lost. The focus change notification did not arrive before the new document was selected in the table (repopulating the editing control before the data was saved.)

Continue reading

Overwhelming Java’s Swing

An odd thing happened recently. I have been working on a simulation that produces graphical and text updates as it progresses. That involves drawing a picture of the system and updating some textual elements of the user interface.

Things seemed to work fine when the simulation was run with “normal” inputs. However, when supplied with a set of parameters to produce a much simpler simulation, the user interface essentially locked up.

Continue reading