VSCode won't import java.awt packages

Viewed 1337

I implemented ActionListener in my java class, but when I tried to auto-import (Shift+Alt+O), it gave this error:

Running the contributed command: '_vscode_delegate_cmd_ksicrwzq' failed.

The whole code is here:

import javax.swing.JFrame;

import javax.swing.JTextField;

public class myframe extends JFrame implements ActionListener{

myframe(){
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLayout(new FlowLayout());
    this.pack();
    this.setVisible(true);

    JTextField text = new JTextField();
    text.setPreferredSize(new Dimension(250,40));
}
3 Answers

I was having similar problem that is VS Code not showing imports for java.awt package as can be seen here. here.

After a lot of research, I came to this link explaining about Working with GUI in VS code.

Solution

Follow the following steps:

Open Command Pallete:

You can open command pallete by pressing Ctrl + Shift + P or ⌘ + Shift + P for mac Users.

Go to Java: Help Center

Command Pallete Search

Go to Students tab:

Navigate to the Student's tab in Java: Help Center

Enable AWT Development

Enabling AWT Development

This procedure solved my issue and now VS Code is providing suggestions for java.awt package.

See similar issue report in github: Click the bulbs to Organize imports throws the error.

After Cleaning Java Language Server Workspace from Command Palette, hovering over the code then use Quick Fix to import needed jars instead of keyboard shortcuts. Then the error wouldn't happen again.

Follow some steps for this

  1. CTRL + .
  2. Search (Filtered Types)
  3. Click On Edit in settings.json
  4. Replace java.awt.* with java.awt.List,
      "java.awt.List",
      "com.sun.*",
      "sun.*",
      "jdk.*",
      "org.graalvm.*",
      "io.micrometer.shaded.*"
  
Related