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”.

A Java JScrollBar example

I took a little time today to add some new features to my "JustWrite" text editor, and one of the features I'm experimenting with is an animation similar to the current horizontal scrolling area shown at the top of Apple's Mac web page. When you first hit that page the scrolling area is offset a little bit, and then in an animation the scrollbar moves more to the center of the scrolling display area.

A Java text area with a background image (watermark image)

Java image FAQ: How can I place an image in the background of a Java text component, such as creating a background image for a JTextArea, JTextPane, or JEditorPane? (i.e., How do I create a Java background or watermark image.)

Java font size - how to change the JTextArea, JTextPane, JEditorPane font size

I'm currently writing a customized text editor in Java, and as part of that, I want to make it easy for the user to increase or decrease the font size in the text editing area (technically a JTextPane). I didn't expect this to be easy, but I've been pleasantly surprised that the following approach seems to work just fine:

Java undo and redo in a Swing application

Here's a step-by-step approach for how to implement "undo" and "redo" behavior in your Java text components, i.e., the JTextArea JEditorPane and JTextPane I should note that you don't get this behavior for free -- you have to implement it yourself, hence this undo/redo tutorial.

JEditorPane line/word wrapping problem

I had a problem this weekend with the JEditorPane line wrap (word wrap), where it was wrapping lines on the right margin right in the middle of words. Wherever my sentence hit the right margin, that's where the line wrapped (with no consideration for word boundaries), leaving me with lines that looked like this:

How to create and use a JScrollPane in a Java Swing application

Summary: A simple Java JScrollPane source code example.

When you're first learning how to use Java Swing components, like a JScrollPane it can be a little hard to figure out how to get started. To that end I am trying to share several complete Java source code examples that demonstrate how to work with components like these.