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 4 of 6

The main() method instantiates JPad and passes to its constructor the array of command-line arguments on the event-dispatch thread. After creating JPad's UI, the constructor opens the file identified by the first command-line argument when at least one argument is specified.

Swing UIs and the event-dispatch thread

In the JavaWorld article "Swing threading and the event-dispatch thread" author John Zukowski explains why it's important to create Swing UIs on the event-dispatch thread.

Creating the UI

JPad's constructor creates the JPad UI by leveraging the javax.swing package's JCheckBoxMenuItem, JLabel, JMenu, JMenuBar, JMenuItem, JScrollPane, and JTextArea classes, as shown in Listing 2.

Listing 2. Swing organizes JPad's UI into a menu bar and a content pane

// ...
private JTextArea ta;
// ...
private JLabel lbl;
// ...
public JPad(String[] args)
{
   // ...
   JMenuBar mb = new JMenuBar();
   JMenu mFile = new JMenu("File");
   JMenuItem miNew = new JMenuItem("New");
   miNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,
                                               KeyEvent.CTRL_MASK));
   // ...
   mFile.add(miNew);
   // ...
   mb.add(mFile);
   // ...
   JMenu mFormat = new JMenu("Format");
   final JCheckBoxMenuItem cbmiWordWrap = new JCheckBoxMenuItem("Word Wrap");
   // ...
   mFormat.add(cbmiWordWrap);
   // ...
   mb.add(mFormat);
   // ...
   setJMenuBar(mb);
   getContentPane().add(new JScrollPane (ta = new JTextArea()));
   // ...
   getContentPane().add(lbl = new JLabel("JPad 1.0"), BorderLayout.SOUTH);
   setSize(400, 400);
   setTitle(DEFAULT_TITLE);
   // ...
   setVisible(true);
   // ...
}

The constructor first initializes the JFrame superclass and creates/installs the menu system (although I've omitted most of this code for brevity). Next, it creates the scrollable text area and status bar, and adds them to the frame window's content pane. Lastly, it initializes and displays the window.

Why use setTitle(DEFAULT_TITLE);?

Experienced Swing developers might note that I've used setTitle(DEFAULT_TITLE); instead of super(DEFAULT_TITLE); to initialize the frame window's default titlebar text in Listing 2. I wrote the code this way because the JavaFX 2 application will also require setTitle(). For the sake of example, I wanted JPad to be as consistent with JPadFX as possible.

Handling events

Event handling is one of the more involved aspects of Swing development, which makes refactoring a Swing app's event-handling infrastructure to JavaFX both challenging and rewarding. Here, we'll look at how JPad handles typical notepad events triggered by the user, namely: action, document, menu, caret, and window events.

Action events

An action event is fired when the user selects a menu item. For example, when the user selects New from the File menu, an action event is sent to New's registered action listener, as shown in Listing 3.

Listing 3. JPad responds to a New action event

ActionListener al;
al = new ActionListener()
{
   @Override
   public void actionPerformed(ActionEvent ae)
   {
      if (fDirty)
         switch (JOptionPane.showConfirmDialog(JPad.this, SAVE_CHNGS,
                                               TITLE_AYS,
                                               JOptionPane.YES_NO_OPTION))
         {
            case JOptionPane.YES_OPTION: if (doSave()) doNew(); break;
            case JOptionPane.NO_OPTION : doNew();
         }
      else
         doNew();
   }
};
miNew.addActionListener(al);

The New menu item's action listener first examines fDirty to learn whether or not the current document has changed. If this Boolean field variable is true (meaning that the document has changed), the user is prompted to save changes before a new document replaces the current one.

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