DevFaqNbIdiosyncracies

Common calls that should be done slightly differently in NetBeans than standard Swing apps (loading images, localized strings, showing dialogs)

There are a few cases where NetBeans has convenience classes or facilities that you should use, instead of doing them the way you may be used to. They are:

  • Loading images - Don't use ImageIO.read() or Toolkit.loadImage() - instead, use ImageUtilities.loadImage() - it has an optimized image caching strategy, and will play nicely with NetBeans module class loaders
  • Creating icons from images - Rather than use new ImageIcon(someImage), use ImageUtilities.image2Icon(someImage) which manages memory better.
  • Loading resource bundles/localized strings - Don't use ResourceBundle directly - instead, use NbBundle.getMessage() - it will play nicely with NetBeans class loaders, and Strings resolved this way can be branded using the standard branding mechanism (this is the way you change the title of your application from "NetBeans" to something else). Also, do not hold a reference to a resource bundle - just call NbBundle.getMessage() every time - bundles are cached for a period of time, the call is fast. In a large application, holding resource bundles eats memory wastefully
  • Assigning mnemonics to labels and buttons - use Mnemonics to assign text and mnemonic to a widget with one call using one key value pair in properties file and annotate the mnemonic with & character. Also do not reuse the same text if it is used in different UI components. This is more freindly to localization.
    Tip: Check 'Generate Mnemonics Code' checkbox in properties of your form if you are using NetBeans GUI editing support.
  • Showing dialogs - instead of creating a JDialog and showing it, or using JOptionPane, use NotifyDescriptor or DialogDescriptor to define your dialog and its contents, then pass these to DialogDisplayer.notify - such dialogs will play nicely with NetBeans' windowing system, global actions, etc.
  • Reading/writing/listing files - in most cases, rather than work with java.io.File, you will want to work with org.openide.filesystems.FileObject.
  • Quiting application - you can of course still continue to quit using System.exit() but polite NBP apps should employ LifecycleManager instead. Typical Usage pattern is LifecycleManager.getDefault().exit() that is equals to System.exit(0) you don't provide custom LifecycleManager.

Applies to: NetBeans 6.8 and above

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo