Posts in the “java” category

How to create a simple Swing HTML viewer with Java

I've been working on writing my own Java text editor on and off for several years now, and one feature I just added to this editor is the ability to view (or preview) HTML and CSS content. Using just the base Swing classes this turned out reasonably well -- I'm not looking to write a complete browser here -- so I thought I'd share some source code to show how this works.

Blurry text in a Mac OS X Java Swing application (solution)

I noticed recently that a Scala/Java Swing application I am developing on Mac OS X 10.9 has blurry text when it’s run as a Mac application. The text looks fine when I run the application through SBT, but looks blurry when I package it and run it as a Mac OS X application.

A solution is to put this text in the application’s Info.plist file:

How to get the default system font in a Java/Swing application

I haven’t tested this with other Java components, but if you want/need to get the “system font”, this code gets the default system font from a JEditorPane component:

val outputArea = new JEditorPane
val fontFamily = outputArea.getFont.getFamily

That code is written in Scala, but as you can see, it converts easily to Java. On Mac OS X 10.10, fontFamily ends up being “Lucida Grande”.

How to use the Commons IO project to create, delete, and move files and directories

I wrote the following Java class today that is a "helper" class for a project I'm working on to create a web interface for Nagios. The Nagios part isn't too relevant, because mostly what I'm trying to show here is how to use the Apache Commons IO project to make it easier to interact with files and directories from Java code.

In short, the following Java class shows how to use the Commons IO project to accomplish the following tasks. Without any further delay, here is the complete source code for my class:

Java: How to list all files in a directory that match a filename extension

I just ran across this Java method I used to create a list of all files in a directory that match a specific filename pattern, or more specifically, matched the same filename extension.

The Java "list files in a directory" source code

First, here's the source code for this Java "list files" method. I'll follow this code with a brief description:

What is a Java NoSuchMethodException?

Java exception FAQ: What is a Java NoSuchMethodException?

Answer: Using Java, you can get a NoSuchMethodException when you're using reflection and try to dynamically use a method on a class, and the method does not actually exist. The following example Java class shows how this NoSuchMethodException can be generated:

What is a Java ClassNotFoundException?

Java Exception FAQ: What is a ClassNotFoundException?

Answer: A ClassNotFoundException can happen when you use Java's Reflection capability to dynamically create a class. Here's some example code where I intentionally throw a ClassNotFoundException by trying to create and use a class ("FooClass") that I know doesn't exist:

A free, complete, Java Mac OS X application

Over the last three days I've created a new Java Swing application for the Mac OS X platform, and today I'm giving away all the source code for this project (free of charge). The complete project includes both (a) all the Java source code and (b) all of the Ant build script code needed to build this application. The end result of the build is what appears to the end user to be a native Mac OS X application.

JFrame examples and tutorials (collection)

Today is organization day for me, and in an effort to organize everything I've ever written about a Java JFrame, here is a collection of links to all my Java JFrame examples and tutorials.

Two Java inheritance tests / interview questions

Summary: Two Java inheritance tests that you might run into during a Java programming job interview.

This is a fun test to give to newbie Java developers. Just read the following code, and assuming that this code is compiled and runs, what do you think it will print?

Java Mac AppleScript ScriptEngine null (ScriptException)

A funny thing about using AppleScript with Java -- and three years later with Scala and Akka -- is that you tend to run into the same errors. So glad this URL kept a transcript of my earlier problem ("Problems running AppleScript with Java headless argument"), the solution of which is shown in this image. The conversation was also logged in a less convenient format here.