When I want to open a FileDialog after I get some input from the console it fails.
See the code below.
When I first call openFileDialog
and then chooseOption
it works fine.
Does anybody know how this is possible?
public class SomeClass
{
int choice = 2;
Scanner keyboard;
String filter = "*.xml";
public void mainMenu() {
keyboard = new Scanner(System.in);
choice = ChooseOption();
FileDialog loadDialog = openFileDialog("Choose file", filter);
useInfo(loadDialog);
}
public int ChooseOption() {
System.out.println("Make your choice: \n"
+ "0) option A \n"
+ "1) option B");
try {
choice = keyboard.nextInt();
} catch(Exception e) {
System.out.println("Invalid input, try again.\n");
ChooseOption();
}
return choice;
}
public FileDialog openFileDialog(String title, String filter) {
System.out.println("Which file you want to use? \n");
FileDialog loadDialog = new FileDialog(new Frame(), title , FileDialog.LOAD);
loadDialog.setFile(filter);
//loadDialog.pack();
loadDialog.setVisible(true);
return loadDialog;
}
public void useInfo(FileDialog loadDialog) {
if( loadDialog == null || loadDialog.getFile() == null ) {
useDefaultFile();
doSomthing();
} else {
doSomthingElse();
}
}
}
The
FileDialog
is displayed asframe
but it might be behind otherActive Windows.