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

Java color FAQ: Do you have a list of color keys I can use with the Java UIManager class?

Solution

If you're asking this question about the list of color keys in the UIManager class, you probably already know that you can write code like this to use color values from the Java UIManager class:

I don't need to do add data to the JList in my current Java/Swing application, but out of curiosity I did some research to see what you would have to do if you wanted to add data to a JList, and here are the results.

It turns out that if you need to add or edit JList data, you're probably better off creating your data as an instance of a DefaultListModel (as opposed to on object array or Vector).

As I'm preparing a different Java Swing tutorial, I thought I'd share a test class I just created to look at the default entries from the Java UIManager class.

Here's the source code for my Java class that prints all the Java Entry objects from the UIManager class. As you can see, I call the getLookAndFeelDefaults method of that class, and then call the entrySet method of that class to get all the default entries:

Here's another JList example, this time sharing some JList code I use to render images and text in JList cells.

Before going on, if you just want to display a set of images in a JList, all you have to do is create your images as an array Icons, and then add this array to your JList, and you're done. The JList is smart enough to render a Java Icon as an image, so in the most simple case, that's all you have to do.

Several friends have asked me how to upload photos/pictures in Facebook, so I keep the following handy email message around. In case anyone else needs to know how to upload pictures in Facebook, here are my instructions:

Facebook photos - how to upload photos/pictures (initially)

In the first Facebook photo upload scenario, it's important to know that you store your photos in a photo album. So the first time you upload pictures to Facebook, you'll need to create a Facebook Photo Album, as described in these steps:

Continuing my exploration of the Java JList, here's some sample JList/JScrollPane code, showing how I add a JList to a JScrollPane.

To get started, somewhere at the top of my class I declare my JList and JScrollPane, like this:

private JList menuList;
private JScrollPane menuScrollPane;

Later in my Java class I then set up my JList, like this:

If you're in the market for free Java components for your Swing applications, it looks like there is a decent collection of free Java components to choose from.

I was going to make a list of different free Java component websites, but it looks like the Java Desktop people already have a list of Java components, so I recommend just visiting that link. (Beware that they are not all free, however.)

I need to work on this JList ListSelectionListener example some more, but I thought I'd share it in its current state, in case it will help anyone get started.

I've been working with the Java JList a fair amount recently, so I thought I'd include a short collection JList examples here while everything is still fresh.

Java ping FAQ: How do I ping a computer from a Java program (or Java class, or Java method)?

I've been working on a new Java networking application, and as part of network debugging, I wanted to be able to ping a server from my Java program. I thought writing a "Java ping" class/program would be straightforward, but in short, it wasn't, so I wrote a little helper class to let me call the system ping command, and use the output from it.

One thing I've learned as I'm getting back into Java/Swing development is that there is more than one SwingWorker in the world these days.

While I was away, Sun introduced this new SwingWorker in Java 6:

When you're working with a Java project in Eclipse and you need to add a new jar file to your project, you don't need to close down Eclipse or your current Eclipse project (as I've seen a few people do). In fact, here are all the steps you need to take to add a new jar file to your Eclipse build path:

This morning when I saw some Java JFrame code on a mailing list, it made me think that I needed to put a simple JFrame example out here, something that would show how to properly construct and display a JFrame without getting into a discussion of anything else. Here are two examples that show the correct technique.

1) A simple Java JFrame example

To that end, here is the source code for a simple "JFrame example" demo class. This example shows how to construct a JFrame, and make sure it's properly displayed using the SwingUtilities invokeLater method:

Perl string processing FAQ: How can I process every character in a Perl string?

I recently had to write some Perl code to process every word in a file, and that made me wonder how to process every character in a Perl string. I didn't know how to do this, but I just cracked open my copy of the Perl Cookbook, and found a couple of possible solutions.

Today I'm releasing Version 1.0 of a very simple, free Mac OS X application named "Meditation".

This Mac Meditation application is really just a very simple timer that plays a "gong" sound at the end of the time period you specify. (So, if you want a Mac OS X timer application for some other reason, Meditation may fit your needs.)

JFrame color FAQ: How do I set the JFrame background color?

In general, to set the JFrame background color, just call the JFrame setBackground method, like this:

jframe.setBackground(Color.RED);

Note that there are many more things you can do with the Java Color class, including:

JButton listener FAQ: A common Java JButton question is "How do I add a listener to a JButton?", or the equivalent, "How can I tell when a JButton is pressed?"

JButton listener solution

In short, you typically want to add an ActionListener to a JButton, as shown in the following source code snipet:

JFrame title FAQ: How do I set the title of a Java JFrame?

Solution

There are two ways to set the JFrame title. First, you can set the JFrame title when you construct your JFrame, like this:

JDialog FAQ: How do I capture the JDialog close event?

Solution

To get the event when a JDialog is closed, you can add a window listener using addWindowListener to your JDialog, or add a component listener, using addComponentListener.

I've been getting back into the Java/Swing world during the last few days, and I thought I'd share some example code here. In my current application, one of the things I just worked on was setting the initial JFrame size to a value I liked.