Reputation: 1345
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?
Upvotes: 2
Views: 2483
Reputation: 168825
This is where you need to leave the magic of NetBeans aside and RTM.
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
Upvotes: 3
Reputation: 1108662
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:
Just pass null
instead of this
.
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.
Upvotes: 4