Scala, Java, Unix, MacOS tutorials (page 370)

I wrote a longer Java/Mac/Swing tutorial yesterday titled "Handling the Java on Mac OS X About, Quit and Preferences menu events", but if you're just interested in handling the Mac OS X Preferences menu item in a Java/Swing application, here's a shorter version of that information.

Update: I've taken the approach shown in this article and turned it into a much more robust Mac OS X app, which I'm selling for a whopping $0.99.

Follow this link for a free trial of my "Mac OS X Hide Your Desktop Icons" application.

I wrote a very small "Java on Mac OS X" application that I'd like to share here today. This one is a little different, so let me explain the problem.

I've found that using Mac OS X is different from using Windows, in that with Mac applications, I almost never use them in full-screen mode. Typically they occupy 1/2 or 2/3 of my over screen, and then I have to see my Mac Desktop behind the application window. I actually prefer everything about this approach except for one thing: My Desktop is usually very cluttered, and I don't like to see that clutter while I'm working.

When writing Java GUI code for the Mac OS X platform, you'll want to properly handle the Mac About, Quit, and Preferences menu items and events. Fortunately doing this is very simple, and I'll demonstrate that in this "Java on Mac OS X" tutorial.

Java SwingUtilities FAQ: Can you demonstrate an example of the Java SwingUtilities invokeLater method?

I don't know if this is the "proper" way to handle having multiple keystrokes for one action, but until I find a better approach I thought I'd share this here.

For my Java text editor application, on Mac OS X I want the user to be able to use any of the following keystrokes to increase the font size by one unit:

  • [Apple][=]
  • [Apple][Shift][=]
  • [Apple][+]

Each of those keystrokes might be interpreted by a user as being equivalent to [Apple][+], so I want to be sure I support all three of them.

If you need to get the position of the caret (cursor) in a JTextComponent (++JTextField, JTextArea JEditorPane or JTextPane), this turns out the be easy. Just use the getCaretPosition method of these classes, as shown here:

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:

Lately I've been doing a lot of Java programming on Apple's Mac OS X platform, and my most recent effort has been to handle drag and drop events in my Java Swing application. Not satisfied to handle "simple" drag and drop events, I decided I wanted to take my application even farther, and let users drag files and images to my Java application icon in the Mac Dock.

As part of the early fallout from Oracle's acquisition of Sun, Sun CEO Jonathan Schwartz has resigned from Sun, leaving with a parting haiku on Twitter. (See haiku on Wikipedia.)

Mr. Schwartz struck me as an interesting CEO. He seemed like one of the first CEOs to post his ideas on a blog, and his strategy of trying to make Sun a more "open" company in an effort to save them was very interesting.

MacBook battery life FAQ: Do you have any tips on how I can improve my MacBook battery life?

Yes I do, and wow -- did I learn something while digging into this -- I had no idea I was supposed to calibrate my MacBook Pro battery every month. I just found this out while trying to help a friend who was having some MacBook battery problems (her new MacBook is not holding a battery charge very long).

<< Back to "Java exec with ProcessBuilder and Process, part 1"

A complete Java class that executes a system command

Now that you've seen that snippet of code, here's a complete Java class named ProcessBuilderExample that can execute a Unix/Linux system command:

Continuing with the organization efforts again today, here is a collection of Java JButton tutorials and examples:

As a way of helping to organize this website, here is a collection of links to my current JScrollPane tutorials and examples:

Still organizing things around here, and here is a list of JDialog tutorials and examples I have created:

As I continue my efforts to organize the content on this site, here is a collection of JOptionPane tutorials and examples.

[toc hidden:1]

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.

The easiest way to copy a file in Java is to download the Apache Commons IO library; just download their library, then use the methods of their FileUtils class to copy a file.

However, if you're just as interested in the technical details of how to copy a file in Java, or just want a method to copy a file in Java, the method below, taken from my Java file utilities class, shows how this actually works:

Funny iPad Photo: In the category of "iPad humor" ... 

I have to say, I like Apple products quite a bit; I have an iPhone, MacBook Pro, and an iMac, and I'm sure the iPad will end up being very successful once people get their hands on it. But in the meantime, I love this funny "iPad" image. It sums up a lot of feelings about the Apple iPad introduction:

The JOptionPane showOptionDialog is generally pretty straightforward, so without any introduction, here's the source code for a quick JOptionPane showOptionDialog example:

Java write/save FAQ: Can you share an example of how to write to a file in Java?

Sure. Here are two "Java file save" or "Java file write" examples, taken from my Java file utilities article (Java file utilities to write, save, open, read, and copy files). I'm not going to explain these too much, but if you need a simple method to write to a file in your Java programs, I hope these methods will help you.