1

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

DSlomer64
  • 4,234
  • 4
  • 53
  • 88
  • Have you tried Right Click->Fix imports? That's how easy it is in NetBeans 11 – Sync it Jun 20 '20 at 04:05
  • To see the list of shortcuts in NetBeans go to Tools->Options->Editor and you can customise it – Sync it Jun 20 '20 at 04:06
  • Right-click Fix Imports does NOT add import statements. It just modifies the unresolved names in the file. Please compare the 3 hunks of code in my post. First line: great. Love it. But compiler never has liked it since (a) names not fully-qualified and (b) no import statements placed right after package statement provide qualification. Years ago, Alt+Enter offered to insert after the package statement an import statement as in second hunk of code, leaving first line above intact. Not so today. Alt+Enter adds fully-qualified prefix that makes code unreadable. I object. – DSlomer64 Jun 23 '20 at 06:12
  • Exactly what do I click next after Editor? Hints? Then Imports? Then what? There are virtually no words in this section to tell what the option might do. Please see edit to original post. – DSlomer64 Jun 23 '20 at 06:15

2 Answers2

0

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

Sync it
  • 1,180
  • 2
  • 11
  • 29
  • "I'm am not exactly understanding your problem but netbeans 8.2 & 11 doesn't alter your statement's for fixing imports" is simply not true for MY recent versions of Netbeans 8. And *I'm* not understanding, either! This is HORRIBLE. Waaaaah! – DSlomer64 Jun 24 '20 at 19:24
0

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.

AlexHomeBrew
  • 117
  • 9
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/33074681) – Yaniv Oliver Nov 07 '22 at 12:48
  • Right on! I guess I'll have to switch to a later version, but I'm not convinced that the "new" "owners" of Netbeans are listening with intent to fix nagging problems. "Take it or leave it" isn't much of a choice. Besides problems with import there are other problems but I won't clutter this thread with them. – DSlomer64 Nov 11 '22 at 17:18