Over a million developers have joined DZone.

How to Create a Hint in the NetBeans Java Editor

DZone's Guide to

How to Create a Hint in the NetBeans Java Editor

· Java Zone ·
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

I have a bad tendency to use JOptionPanes and System.out for debugging, rather than the IDE's state of the art Debugger. What's almost as bad is that I tend to leave those method calls in my code. But now I've solved my problem. I created a hint in the NetBeans editor that tells me about all the instances where I'm using these 'antipatterns'.

Helpful marks appear in the right side of the editor, so that I can see everywhere in my code where these method calls are found. As the user of my own hint, I can change the way it is displayed, i.e., either as a warning (as above) or as a squiggly red error mark. I didn't need to do any coding for that part of my hint. By registering it in the layer, it landed on its own two feet in the Options window for these purposes:

To create hints yourself, similar to the above, you need a reasonable understanding of the new 6.0 Java Language Infrastructure. The NetBeans Java Language Infrastructure Tutorial will get you up and running, after which you should read the Java Infrastructure Developer's Guide and the Retouche Developer FAQ.

Armed with all that knowledge, you are ready to get stuck into the hint infrastructure. To get you warmed up, here's how I created the above hint. First, register it in the layer file:

<folder name="org-netbeans-modules-java-hints">

<folder name="rules">

<folder name="hints">
<folder name="general">
<attr name="SystemFileSystem.localizingBundle" stringvalue="org.netbeans.modules.demohint.Bundle"/>
<file name="org-netbeans-modules-demohint-WrongDebugMethodologies.instance"/>



Then create the class referred to above, WrongDebugMethodologies.java, with this content:

public class WrongDebugMethodologies extends AbstractHint {

private static final List<Fix> NO_FIXES = Collections.<Fix>emptyList();

private static final Set<Tree.Kind> TREE_KINDS =

public WrongDebugMethodologies() {
super(true, true, AbstractHint.HintSeverity.WARNING);

public Set<Kind> getTreeKinds() {

return TREE_KINDS;

public List<ErrorDescription> run(CompilationInfo info, TreePath treePath) {

Tree t = treePath.getLeaf();

Element el = info.getTrees().getElement(treePath);
String name = el.getSimpleName().toString();

if (name.equals("showMessageDialog")) {
return Collections.<ErrorDescription>singletonList(
(int) info.getTrees().getSourcePositions().getStartPosition(info.getCompilationUnit(), t),
(int) info.getTrees().getSourcePositions().getEndPosition(info.getCompilationUnit(), t)));


return null;

public void cancel() {
// Does nothing

public String getId() {
return "Wrong_Debug"; // NOI18N

public String getDisplayName() {
return NbBundle.getMessage(WrongDebugMethodologies.class, "LBL_WrongDebug");

public String getDescription() {
return NbBundle.getMessage(WrongDebugMethodologies.class, "DSC_WrongDebug");


Finally, add the following strings to your bundle:

LBL_WrongDebug=Hey buddy, you probably want to remove that JOptionPane...
DSC_WrongDebug=JOptionPane shouldn't be used, probably.

Required modules: Editor Hints (Experimental), File System API, Javac API Wrapper, Java Hints, Java Source, Utilities API. As you can see at least one of these is experimental, requiring the implementation version to be used.


Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}