Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Swing Error

Tags:

java

swing

I've been given a practical to do, and it comes with a ImageEditor file which I can show if needed however it's lengthy so I've not posted it on here.

I have to implement a save link and have been given the code which I have stored in a separate class file:

public class SaveAction extends AbstractAction{
    public SaveAction(String text, ImageIcon icon, String desc, Integer mnemonic){
        super(text, icon);
        putValue(SHORT_DESCRIPTION, desc);
        putValue(MNEMONIC_KEY, mnemonic);
    }

    public void actionPerformed(ActionEvent e){
       // Just print out a message for now.
       System.out.println("Save");
   }
}

And then creating an instance in the main class:

Action saveAction = new SaveAction(
    "Save", new ImageIcon("img/save.png"), "Save the image", KeyEvent.VK_S);

However it is coming up with the error:

The Constructor SaveAction(String, ImageIcon, String, int) is undefined.

Any help would be greatly appreciated

like image 800
DorianD Avatar asked Jun 15 '26 20:06

DorianD


1 Answers

You have the constructor as:

public SaveAction(String text, ImageIcon icon, String desc, Integer mnemonic)

and you are calling :

new SaveAction("Save", new ImageIcon("img/save.png"), "Save the image",KeyEvent.VK_S);

The error is due to this :

KeyEvent.VK_S must be an int and not Integer and you have Integer as the last argument. So try changing it or just cast it as new Integer(KeyEvent.VK_S)

like image 106
Abubakkar Avatar answered Jun 18 '26 11:06

Abubakkar