macdevcenter.com
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Building a Simple Java Application in Mac OS X
Pages: 1, 2, 3

Let the user pick the color

Instead of hard-coding the color into the application, we could allow the user to select the background color when the application starts up. We'll do this by displaying a JColorChooser that allows the user to pick from a palette of colors. One nice resource for viewing your widget options for Swing components is the Visual Index to the Swing Components. It is part of the Java Tutorial. Just remember -- they all look better running on a Mac.



The JColorChooser allows the user to select a color and it returns a color that can be used as the argument to the setBackground() method that we used in our first iteration of EachSquare. Alter the EachSquare.java code as follows.


package NineSquares;

import javax.swing.JPanel;
import java.awt.Color;
import javax.swing.JColorChooser;

public class EachSquare extends JPanel {
 public EachSquare() {
  setBackground( JColorChooser.showDialog(
    this, "Choose Background Color", getBackground()));
 }
}

To run the JColorChooser from a dialog box, we call the JColorChooser.showDialog() method. It takes three arguments. The first is the component that we're setting the color for. The second is a useful message to the user. The third is the initial color of the component being colored in. It is up to you to make sure these items all match.

If you are getting and setting the background color and send a message to the user such as "Choose Font Color," you will baffle the user. Notice that you have isolated the changes from the other objects. Your NineSquares object and MainFrame object do not need to know how the color is being selected. Save your changes, compile and run the application, and the following screen will pop up before you see your floating window.

Screen shot.

NineSquares

Now let's add more than one square to our frame. When you are arranging visual components within a container you are leaving the world of the Aqua interface guidelines. The Java way is for you to rely on Sun's Layout Managers. You can choose how you want the basic container to be laid out, and then add components to the container and let the Layout Manager decide where they go.

In our case, we will add nine squares in a three-by-three grid using the GridLayout. Instead of creating one EachSquare object, we will create nine of them and add them to the the MainFrame content pane. The MainFrame.java file needs to be modified like this.

package NineSquares;

import javax.swing.JFrame;
import java.awt.GridLayout;

public class MainFrame extends JFrame{
 public MainFrame() {
  super("Nine Squares");
  getContentPane().setLayout(new GridLayout(3,3));
  for (int i = 0; i<9; i++) {
   getContentPane().add(new EachSquare());
  }
  setVisible(true);
  setDefaultCloseOperation(EXIT_ON_CLOSE);
 }
}

We need to import the GridLayout class, and then we set the layout of the content pane to a new GridLayout that is three by three. Next, we create nine EachSquares and add them to the content pane. We're getting a lot with very few code changes. Now let's take a look at the changes to EachSquare.java.

package NineSquares;

import javax.swing.JPanel;
import java.awt.Color;

public class EachSquare extends JPanel {
 public EachSquare() {
  setBackground(new Color((int)(Math.random()* 16777215)) );
 }
}

We've just created a color in a new way. Instead of specifying a constant color or using a JColorChooser, we're picking a random number between 0 and 2^24-1. You'll notice that we didn't have to import the Math class. This is because it is part of the java.lang package and is automatically available to you. When you run the application, you get a window that looks something like this.

Screen shot.

Responding to mouse clicks

So far, our application isn't very responsive. In this next version, we'll still bring up nine squares where the colors are randomly selected, but this time we'll give the user the chance to alter them. If a user clicks on a square, a JColorChooser dialog will pop-up and allow them to change the background color. The only changes we will make are in the EachSquare.java file. It will now look like this.


package NineSquares;

import javax.swing.JPanel;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JColorChooser;

public class EachSquare extends JPanel {
  public EachSquare() {
    setBackground(new Color((int)(Math.random()* 16777215)) );
    addMouseListener(new MouseClickListener());
  }
  public class MouseClickListener extends MouseAdapter{
    public void mouseClicked(MouseEvent e) {
      setBackground(JColorChooser.showDialog(
        EachSquare.this, "Choose Background Color", getBackground()));
    }
  }
}

The big change here is the MouseClickListener. This subclass of MouseAdapter waits around for a message that there's been a mouse click that it cares about. It then creates a JColorChooser with a beginning color matching the background color of this panel. When the user selects the new color and dismisses the dialog box, the background color for the panel is set to this new choice.

The MouseClickListener is actually defined from inside the EachSquare class and is called an "inner class." The line addMouseListener( new MouseClickListener()) is what associates the MouseClickListener with this particular panel. When a panel is clicked on, its MouseClickListener responds. The only other changes to the file are to add the appropriate imports.

Summary

Although this wasn't really an introduction to Java, I hope it helps you see how easy it is to create simple applications for Mac OS X using the language. You may find that you work better with an IDE that does most of this visual work for you. With most of them, you can drag these visual components onto a workspace and quickly customize the properties to get the look and action that you want. For an application of this scope, it wasn't that hard to implement it from scratch using simple tools.

As a challenge, take a look at this month's issue of Mac Tech. Its challenge is to get you to write a version of Dots. The puzzling thing, however, is that Mac Tech has specified the IDE you must use and a choice of languages. Java is not among your choices. For kicks, let's take a shot at Dots in Java. Feel free to use the discussion forums below to enlist the help of others.

Daniel H. Steinberg is the editor for the new series of Mac Developer titles for the Pragmatic Programmers. He writes feature articles for Apple's ADC web site and is a regular contributor to Mac Devcenter. He has presented at Apple's Worldwide Developer Conference, MacWorld, MacHack and other Mac developer conferences.


Read more Java Programming on the Mac columns.

Return to the Mac DevCenter.