How to make the 'open with' dialog box?

后端 未结 3 1533
南方客
南方客 2021-01-17 06:37

I made a program to search for .txt files.

If I click a file it means that the \"open with\" dialog box should appear, and that dialog box will contain a list of al

3条回答
  •  甜味超标
    2021-01-17 06:59

    You should use FileChooser for this. Take a look here:

    //Create a file chooser
    final JFileChooser fc = new JFileChooser();
    ...
    //In response to a button click:
    int returnVal = fc.showOpenDialog(aComponent);
    
    
    public void actionPerformed(ActionEvent e) {
        //Handle open button action.
        if (e.getSource() == openButton) {
            int returnVal = fc.showOpenDialog(FileChooserDemo.this);
    
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = fc.getSelectedFile();
                //This is where a real application would open the file.
                log.append("Opening: " + file.getName() + "." + newline);
            } else {
                log.append("Open command cancelled by user." + newline);
            }
       } ...
    }
    

提交回复
热议问题