FileDialog doesn't show after get input from console

842 views Asked by At

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();
    }
}

}
2

There are 2 answers

0
Makky On BEST ANSWER

The FileDialog is displayed as frame but it might be behind other Active Windows.

0
melc On

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.