Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Practical JavaFX 2, Part 1: Architecture of a Swing-based notepad

How will Swing JPad's UI features map to JavaFX 2.0?

  • Print
  • Feedback

Page 3 of 6

File offers the following menu items:

  • New creates a new empty document and prompts users to save the current document if it has unsaved changes.
  • Open opens an existing file. Users are prompted to save the current document if there are unsaved changes.
  • Save saves the current document to its associated file or a different file.
  • Save As saves the current document to a different file.
  • Exit exits the application. Users are prompted to save the current document if there are unsaved changes.

Edit offers the following menu items:

  • Undo undoes the last edit; disabled when there are no new edits.
  • Cut cuts the selected text to the clipboard; disabled when no text is selected.
  • Copy copies the selected text to the clipboard; disabled when no text is selected.
  • Paste pastes clipboard text over selected text or at the caret position; disabled when the clipboard has no text.
  • Select All selects all of the text area's text.

Additionally:

  • Word Wrap can be enabled or disabled by checking or unchecking the Format menu item.
  • About JPad is a menu item under Help that lets users obtain information about the application.

The JPad application also lets users specify a file to be opened as a command-line argument. If a user drops a file being dragged onto its text area, JPad will open that file after letting the user save any recent changes.

Setting up a JavaFX 2 development environment

Oracle's JavaFX website extensively documents the JavaFX architecture. That is also where you'll find JavaFX development software for your 32-bit or 64-bit Windows or Mac OS X platform. I used the JDK 7u2 with JavaFX SDK integrated software package (which includes the JavaFX 2.0.2 SDK) on a Windows XP platform to develop my JavaFX 2 JPad application.

Compiling and running JPad

In order to explore JPad in your development environment, download its source code and resource file: JPad.java and icon.png. After extracting these files to your current directory, execute the following commands to compile JPad.java and run it. Note that the third command tells JPad to open and display the contents of JPad.java:

javac JPad.java
java JPad
java JPad JPad.java

Architecture of JPad

JPad is implemented as a single JPad top-level class. As with many Swing applications, JPad extends the javax.swing.JFrame class, which makes it a kind of frame window. You can see this in Listing 1.

Listing 1. The JPad class encapsulates the JPad application

// ...
public class JPad extends JFrame
{
   // ...
   public JPad(String[] args)
   {
      // ...
      if (args.length != 0)
         doOpen(new File(args[0]));
   }
   private void doExit()
   {
      // ...
   }
   private void doNew()
   {
      // ...
   }
   private void doOpen()
   {
      doOpen(null);
   }
   private void doOpen(File file)
   {
      // ...
   }
   private boolean doSave()
   {
      // ...
   }
   private boolean doSaveAs()
   {
      // ...
   }
   private String read(File f) throws IOException
   {
      // ...
   }
   private void write(File f, String text) throws IOException
   {
      // ...
   }
   public static void main(final String[] args)
   {
      Runnable r = new Runnable()
      {
         @Override
         public void run()
         {
            new JPad(args);
         }
      };
      EventQueue.invokeLater(r);
   }
}

Listing 1 shows you JPad's skeletal framework, which consists of a constructor, several private methods, and the main() entry-point method. (I've commented out everything else because it isn't relevant at this point.)

  • Print
  • Feedback

Resources

More from JavaWorld

  • Find more of Jeff's writing in Java Tutor, his blog on JavaWorld.
  • See the JavaWorld Site Map for a complete listing of research centers focused on client-side, enterprise, and core Java development tools and topics.
  • JavaWorld's Java Technology Insider is a podcast series that lets you learn from Java technology experts on your way to work.