Java Jfilechooser doubt code

Asked

Viewed 79 times

1

I understood that here I urge the class JFileChooser:

JFileChooser arquivo = new JFileChooser();

I take the file object and call the class method JFileChooser(correct me if I’m wrong):

arquivo.setDialogTitle("Selecionar Arquivo");
arquivo.setFileSelectionMode(JFileChooser.FILES_ONLY);
int retorno = arquivo.showOpenDialog(this);

In the next passage I did not understand. I call the method getSelectedFile(); class JFileChooser and then play it within a class? I’ve never seen it happen in php.

What happened to arquivo.getSelectedFile();? Where did I play this? File file is what?? num is an incomplete class? Or File is a file variable like File?

if(retorno == JFileChooser.APPROVE_OPTION) {

    File file = arquivo.getSelectedFile();

  jTextField5.setText(file.getPath());

}else {


}

1 answer

1

The Java language is a strongly typed language, that is, every declared variable needs an explicit specified type.

What is happening on this line is that the method getSelectedFile() returns the selected object in the component JFileChooser, on a guy named File, and you are assigning this object returned to variable file of the same type. The fact that the variable name is file is only option of the author of the code, has nothing to do with the type of it, you could call arquivo also, but the variable would still be of the type File.

There is nothing abnormal there, it is a value assignment of the return of a method to a variable, with the difference that java requires that the variable declared to the left has to inform what its type.

  • The.getSelectedFile(); returns something new Numseioque(); that’s it??

  • @Edenilsonconception no, this method, as I explained, returns a path from the file selected in Jfilechooser, and this path is File type, which is the java type to handle running operating system files.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.