I have the following code, in the same java file.
import javax.swing.SwingUtilities;
import java.io.File;
public class MainClass2{
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
public void run() {
javax.swing.JFileChooser jfc = new MyFileChooser();
File file = jfc.getSelectedFile();
}
});
}
}
class MyFileChooser extends javax.swing.JFileChooser{
public MyFileChooser(){
System.out.println("constructor call");
}
@Override
public java.io.File getSelectedFile(){
System.out.println("call to getSelectedFile");
return null;
}
}
When I run it, the output gives me
call to getSelectedFile
constructor call
call to getSelectedFile
Shouldn't the output be
constructor call
call to getSelectedFile
I'm using java 5.
解决方案
MyFileChooser's constructor is equivalent to:
public MyFileChooser() {
super(); // ***
System.out.println("constructor call");
}
The first call to getSelectedFile() is made by MyFileChooser's base class constructor, which is invoked implicitly at the point marked *** above, before the System.out.println("constructor call").
Here is the stack trace:
MyFileChooser.getSelectedFile() line: 16
AquaFileChooserUI.installComponents(JFileChooser) line: 1436
AquaFileChooserUI.installUI(JComponent) line: 122
MyFileChooser(JComponent).setUI(ComponentUI) line: 670
MyFileChooser(JFileChooser).updateUI() line: 1798
MyFileChooser(JFileChooser).setup(FileSystemView) line: 360
MyFileChooser(JFileChooser).(File, FileSystemView) line: 333
MyFileChooser(JFileChooser).() line: 286
MyFileChooser.() line: 11