tags:

views:

48

answers:

2

I am using netbeans for developing java dextop application,I have created a JFilechooser which will let user to save a new file created.

But the this int returnVal = newFileChooser.showSaveDialog(this); line of the following code gives this error:

method showSaveDialog in javax.swing.JFileChooser cannot be applied to given types
 required: java.awt.Component
 found: netsim.NetSimView

here class name is NetSimView and source package is netsim

private void newMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
    newFileChooser=new JFileChooser();
    int returnVal = newFileChooser.showSaveDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = newFileChooser.getSelectedFile();
    } else {
        System.out.println("File access cancelled by user.");
    }
}

How to fix this error?

+3  A: 

It's expecting an instance of java.awt.Component as argument in showSaveDialog() method, but you aren't passing a valid argument.

You have 2 options:

  1. Just pass null instead of this.

  2. Let the class netsim.NetSimView extend a java.awt.Component.

Hint: those blueish code things in the 1st sentence are actually links. Click and learn.

BalusC
+3  A: 

This is where you need to leave the magic of NetBeans aside and RTFM.

The JavaDocs for JFileChooser.showSaveDialog(Component) explicitly state that the argument has to be a Component (or by implication, something that extends component).

The Component is used to provide a position for the chooser.

Further down the docs. add.

Parameters: parent - the parent component of the dialog, can be null; see showDialog for details

Andrew Thompson