The FileDialog
is displayed as frame
but it might be behind other Active Windows.
FileDialog doesn't show after get input from console
-
01-09-2022 - |
Вопрос
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 following code works using jdk6,
import java.awt.FileDialog;
import java.awt.Frame;
import java.util.Scanner;
public class Dialog {
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.setModal(true);
//loadDialog.pack();
loadDialog.setVisible(true);
return loadDialog;
}
public void useInfo(FileDialog loadDialog) {
if (loadDialog == null || loadDialog.getFile() == null) {
System.out.println("useDefaultFile();");
System.out.println("doSomthing();");
} else {
System.out.println("doSomthingElse();");
}
}
public static void main(String[] args) {
Dialog d = new Dialog();
d.mainMenu();
}
}
It is highly possible that when the dialog is shown it is behind other active windows.
Не связан с StackOverflow