How do i make netbeans insert missing import statements as once was an option

1.6k Views Asked by At

I have an error on this old line of code that used to compile a year or so ago:

   public class TreeWalker extends SimpleFileVisitor<Path>;

In the past, maybe upon my pressing Alt+Enter but maybe automatically, Netbeans had an option to insert imports. For the line above, Netbeans would insert:

import java.nio.file.SimpleFileVisitor;
import java.nio.file.Path;

but now all I can see to do causes me to wind up with this too-hard-to-read version:

public class TreeWalker extends java.nio.file.SimpleFileVisitor<java.nio.file.Path>;

Pressing Alt+Enter on lines like the original TreeWalker line above offered an option to include the imports I listed above. That option is not there anymore. Just options to use java.nio.file.Path or use java.nio.file.Path for all Path in source and both produce the same objectional line of code.

The old Netbeans 7 or 8 option Add import for java.nio.file.Pathand this is still an option in Netbeans 14, but whatever it used to say, it's gone. Maybe this became frowned upon because some majority considered bad taste to have an excessively-long list of imports. Now I'm frowning. I'm getting eyestrained and brainfogged trying to figure out what dozens of once-clean-to-my-eyes-and-brain lines of code apparently still say.)

Pressing Alt+Shift+I or Ctrl+Shift+I produces the same sorts of verbose lines throughout what I used to consider very clean, readable code. So does Fix Imports... in Source.

Is the option to provide proper imports gone forever or is it somewhere I haven't looked yet?

EDIT

Here are my first few imports:

package copy;
import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.*;
import java.util.*;
import javax.swing.*;

I delete them and then right-click Fix Imports.

No import statements are added. Every affected line of code has a correct prefix added to it, which makes the code unreadable. Compare first line above (easily read) with the third (with two long prefixes making reading difficult).

How do I get Netbeans 8.2 to insert acceptable import statements and leave my statements alone?

SECOND EDIT

If I click Tools > Options > Editor > Hints > Imports, then maybe this is where my problem is. Not all boxes were checked, so I have just checked every box. And it warns me not to do Star imports and here's the popup of the choices I have and NONE is about adding an import statement and if I pick ANY of those, the line of code gets "mangled".enter image description here

So I cleared Star import from warning me. Same problem. So I cleared ALL the boxes. Same problem. I could play on-off for 15 minutes with the Import options, but (a) I don't know if this is even where the trouble lies and (b) if it IS the problem area FOR ME, how do YOU have the options set?enter image description here

2

There are 2 best solutions below

1
Sync it On

I'm am not exactly understanding your problem but netbeans 8.2 & 11 doesn't alter your statement's for fixing imports

I am using netbeans 11.0

Here is an code sample with unresolved imports enter image description here

I right click and select fix imports

enter image description here

And netbeans add's the imports at the top of my class without altering my statements

enter image description here

So i never get your problem which you described here

public class TreeWalker extends java.nio.file.SimpleFileVisitor<java.nio.file.Path>;

Also it also gives you the choice of selecting classes from conflicting packages

enter image description here

If you are looking for keyboard shortcut go to Tools->Options->Keymap

enter image description here

I myself haven't completly figured out netbeans after using it for more than 2 years so if this post did help or if you have found an answer please comment below

2
AlexHomeBrew On

I have a similar question (in Netbeans 14) which I have not been able to solve: the generated code when using Designer always seems to add fully qualified attributes rather than using the imports. And the generated code is not modifiable so I can't just manually fix it.

For example, I have the line import javax.swing.JPanel; in my imports. Yet here is an example of the generated code:

    topPanel = new javax.swing.JPanel();
    cNameLabel = new javax.swing.JLabel();
    cNameTextField = new javax.swing.JTextField();
    rLabel = new javax.swing.JLabel();
    rTextField = new javax.swing.JTextField();
    sDirectionLabel = new javax.swing.JLabel();
    sDirectionComboBox = new javax.swing.JComboBox<>();
    oCodingLabel = new javax.swing.JLabel();
    cPreferenceComboBox = new javax.swing.JComboBox<>();
    pLabel = new javax.swing.JLabel();
    pTextField = new javax.swing.JTextField();
    filesLabel = new javax.swing.JLabel();
    fileListPanel = new com.mycompany.spm.widget.FileListPanel();

Note the first line doesn't use my import statement.

I have gone through every tab and sub-tab in the Tools -> Options window and didn't find any switch that fixes this. I use the Fix Imports... all the time but it doesn't fix this.

Also, IMHO having lots of import statements is a good thing. It gives me an idea how much the code needs other code, all in one place. Also, I have it folded most of the time so I never see it unless I need to.