blob: 00218fb1d1d77b56fd5dbe81f171b97ab03c14fd [file] [log] [blame]
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
= DevFaqNbIdiosyncracies
:jbake-type: wiki
:jbake-tags: wiki, devfaq, needsreview
:jbake-status: published
:keywords: Apache NetBeans wiki DevFaqNbIdiosyncracies
:description: Apache NetBeans wiki DevFaqNbIdiosyncracies
:toc: left
:toc-title:
:syntax: true
=== 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 link:http://bits.netbeans.org/dev/javadoc/org-openide-util/org/openide/util/ImageUtilities.html#loadImage(java.lang.String)[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 link:http://bits.netbeans.org/dev/javadoc/org-openide-util/org/openide/util/ImageUtilities.html#image2Icon(java.awt.Image)[ImageUtilities.image2Icon(someImage)] which manages memory better.
* *Loading resource bundles/localized strings* - Don't use `ResourceBundle` directly - instead, use link:http://bits.netbeans.org/dev/javadoc/org-openide-util/org/openide/util/NbBundle.html#getMessage(java.lang.Class,%20java.lang.String)[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 link:http://bits.netbeans.org/dev/javadoc/org-openide-awt/org/openide/awt/Mnemonics.html[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 link:http://bits.netbeans.org/dev/javadoc/org-openide-dialogs/org/openide/NotifyDescriptor.html[NotifyDescriptor] or link:http://bits.netbeans.org/dev/javadoc/org-openide-dialogs/org/openide/DialogDescriptor.html[DialogDescriptor] to define your dialog and its contents, then pass these to link:http://bits.netbeans.org/dev/javadoc/org-openide-dialogs/org/openide/DialogDisplayer.html#notify(org.openide.NotifyDescriptor)[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 `link:DevFaqFileObject.asciidoc[org.openide.filesystems.FileObject]`.
* *Quiting application* - you can of course still continue to quit using System.exit() but polite NBP apps should employ link:http://bits.netbeans.org/dev/javadoc/org-openide-util/org/openide/LifecycleManager.html[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 link:http://wiki.netbeans.org/DevFaqNbIdiosyncracies[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-02-07, and needs to be reviewed.