Skip to content

DevFaqNbIdiosyncracies

Antonio Vieiro edited this page Jan 25, 2018 · 1 revision

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 String`s 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.

<hr/>

Applies to: NetBeans 6.8 and above

Apache Migration Information

The content in this page was kindly donated by Oracle Corp. to the Apache Software Foundation.

This page was exported from http://wiki.netbeans.org/DevFaqNbIdiosyncracies , that was last modified by NetBeans user LiborJelinek on 2011-08-14T15:48:03Z.

NOTE: This document was automatically converted to the AsciiDoc format on 2018-01-26, and needs to be reviewed.

Clone this wiki locally