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 2: Refactoring Swing JPad's basic UI features to JavaFX

Migrate JPad's content pane, menu system, and event handling to JavaFX

  • Print
  • Feedback

Page 6 of 6

Listing 8. A change listener detects when a document is dirty

ChangeListener<Number> cln;
cln = new ChangeListener<Number>()
{
   @Override
   public void changed(ObservableValue ov, Number oldval, Number newval)
   {
      fDirty = true;
   }
};
ta.lengthProperty().addListener(cln);

Like JPad, JPadFX only enables the Edit menu's Cut and Copy menu items when the text area contains selected text. Unlike Swing, which uses a caret listener for this purpose, JavaFX accomplishes this task by registering a change listener with the text-area control's selected text property, as shown in Listing 9.

Listing 9. A change listener detects when a document contains selected text

ChangeListener<String> cls;
cls = new ChangeListener<String>()
{
   @Override
   public void changed(ObservableValue ov,
                       String oldval,
                       String newval)
   {
      if (ta.getSelectedText().length() != 0)
      {
         miCopy.setDisable(false);
         miCut.setDisable(false);
      }
      else
      {
         miCopy.setDisable(true);
         miCut.setDisable(true);
      }
   }
};
ta.selectedTextProperty().addListener(cls);

Conclusion to Part 2

Refactoring a Swing application to its JavaFX 2.0 equivalent involves migrating basic and advanced UI architectures. So far you've learned how JPadFX differs from its Swing JPad counterpart in terms of the use of a border pane for managing controls, menu system differences (including observable lists), the scene and stage, and event handling (including change listeners). In Part 3 we'll wrap up this tour of JPadFX by looking at how it interprets Swing JPad's original dialog boxes, clipboard, and drag-and-drop support.

About the author

Jeff Friesen is a freelance tutor and software developer with an emphasis on Java and Android. In addition to writing Java and Android books for Apress, Jeff has written numerous articles on Java and other technologies for JavaWorld, informIT, Java.net, and DevSource. Jeff can be contacted via his website at TutorTutor.ca.

Read more about Enterprise Java in JavaWorld's Enterprise Java section.

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