Posts in the “java” category

Java StringBuffer versus String: When to use StringBuffer

The most common reason to use a StringBuffer instead of a String in your Java programs is when you need to make a lot of changes to a string of characters. For instance, a lot of times you’ll be creating a string of characters that is growing, as shown in this sample example program:

A Java Model View Controller example (Part 3)

<< Back to Part 2 of our Java MVC Example

MVC - Handling data-changing events

Whenever the user adds, edits, or deletes data, such as our Process data, all the proper GUI components need to be notified of this event so they can properly update their displays. In this application, when a user adds a new Process, this means that we need to update our MainProcessTable. We implement this as follows:

JavaFX TextArea: How to insert text at the cursor/caret position

JavaFX TextArea FAQ: How do I insert a string/text into a TextArea at the cursor/caret position?

Solution: First get the position of the cursor/caret in the TextArea, then insert your new text at that position:

int caretPosition = notesTextArea.getCaretPosition;
notesTextArea.insertText(caretPosition, "My new text");

JavaFX: How to set minimum dialog width/size

JavaFX tip: To set the minimum width of a dialog, such as a TextInputDialog, use the setMinWidth method on the dialog’s dialog pane, like this:

val d = new TextInputDialog("")
d.setTitle("Open URL")
d.setHeaderText("Open a URL")
d.getDialogPane.setMinWidth(500)  <-- solution

Java String replaceAll: Why isn’t my “replace” method working?

Java String replace FAQ: Why isn't my Java String replace / replaceAll / replaceFirst code working?

The Java String class has several methods that usually make it really easy to replace one text pattern with another. I say "usually" because what occassionally happens is that I forget that a Java String is immutable (it can't be changed), so I accidentally try some String replace, replaceAll, or replaceFirst code that looks like this:

Java sound example: How to play a sound file in Java

My Java sound/audio example: I'm working on a simple "meditation" application that plays a sound after a certain period of time (the sound of a gong), so I thought I'd share some source code out here that demonstrates how to play a sound file in a Java application like this.

(Note: I initially found this technique described at JavaWorld.com, but the code below is taken from my project.)

In this case the sound file I'm going to play is an "au" file, but I believe this same technique works with most other sound file types as well.

Java sound: A command line Java program to play a sound file (in headless mode)

As part of my ongoing HAL 9000 voice obsession, I downloaded a bunch of "HAL 9000" sound files last night. But, when you double-click a sound file on Mac OS X, it automatically plays through iTunes, which is good for some things, but bad for what I wanted to do. So, I wrote a quick little "Java sound" program to read the name of a sound file from the command line, and then play the sound file.

How to determine the directory your Java application was started in (user.dir)

If you ever need to determine what directory your Java code is being run from (essentially the current working directory), you can get this information from the system properties, specifically the System.getProperty or System.getProperties methods.

The following line of Java code shows how to determine what directory your Java application was started in. This information is stored in the user.dir system property, which you access like this:

How to create directories in Java

Java directory FAQ: How do I create a directory (or directories) in Java? (Also written as, "How do I make a directory in Java?)

To create a directory in Java, just use the "mkdir" or "mkdirs" methods of the Java File class. Here are a few examples to demonstrate this.

How to read a Java Properties file

Java Properties file FAQ: Can you show me how to read a Java Properties file?

Here is a snippet of Java source code that shows how to read a Java Properties file named Pizza.properties. It reads in two properties named CRUST and TOPPINGS.:

How to disassemble Java code with ‘javap -c’

One of my favorite Java subjects is code optimization and performance. Here I'd like to show you a couple of neat things you can learn with the javap -c command. This command lets you disassemble Java bytecode.

The first thing you need to have for this exercise is a little sample Java code. So in the examples below I create two test Java classes, appropriately named Test1.java and Test2.java. Although it's not explicitly stated below, the steps I'm going to follow are these:

Java: How to list the files in a directory

Java file directory list FAQ: How do I create a list of files in a directory using Java?

Here's a quick example of a Java class that demonstrates how to create a list of all files in a directory using just the Java File class. Specifically, this example shows how to list all the files in a directory, store those filenames in a String array, and then print the array.