Issue
I am using Netbeans 11.2. Basically I want to use a method called getNext()
and when I type for example variable.g
and wait for the autocomplete, then the first option is getClass()
. I just press the down arrow key to select getNext()
and I press tab, but the autocomplete only writes get
(meaning now I have variable.get
) and it selects again the first option getClas()
, and I have to press down arrow key again to select getNext()
to write the full sentence.
Is there a way to make autocomplete write getNext()
in just one tab press?
Solution
There is no direct way to resolve this, because autocompletion is working correctly. There are quite a few possibilities with .g
, .get
, etc., and NetBeans has no way of knowing which option you want, so it has to show them all, and let you pick.
However, there is a simple workaround: you can easily create a new code template. Then, if you enter an abbreviation of your choosing, and press [tab], that abbreviation will be replaced with the text getNext();
as follows:
- Navigate to Tools > Options > Editor > Code Templates and select Java from the Language drop list.
- You will see a table with columns titled Abbreviation and Expanded Text. For example, there is an entry with the abbreviation "En" and its associated expanded text is "${no-indent}Enumeration".
- All you need to do is add an entry in that table for
getNext();
.
To do that:
- Click the New button to open the New Code Template dialog.
- Enter an unused abbreviation in the Abbreviation field, and click OK. I chose yy.
- In the Expanded Text textbox enter the value you want to be generated. In your case this would simply be
getNext();
. - Finally click OK to close the Options window.
This screen shot shows the abbreviation yy
mapped to the expanded text getNext();
:
Then, in your edit window just type yy and press tab to generate getNext();
.
Notes:
- I noticed that after generating the expanded text, the code completion window is displayed. I don't know why that happens, but close it by pressing the escape key. So in my example you would enter
yy[tab][esc]
to generategetNext();
- The syntax shown in some of the other templates are more complex than this example because NetBeans uses FreeMarker for as its template engine, but for your case just specifying the text to be inserted is all that is needed.
Answered By - skomisa
Answer Checked By - Timothy Miller (JavaFixing Admin)