-1

I want to use the file chooser of the operating system from java.

I am currently using JFileChooser but that looks ancient!

I saw this question : How to use the default File Chooser for the operating system? java

But there are problems with that!

I have tried:

try {
    
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception ex) {
    ex.printStackTrace();
}

No difference!

I tried using JFileDialog but firstly as far as i have read on the internet it is better to avoid awt until you have a very good reason to use it(according to https://stackoverflow.com/a/21279145/14911094) and also i feel some features that JFileChooser has awt hasn't(or i am not aware of it yet!)

Like i want to only allow user to select directories but not quite possible in FileDialog(or i don't know it)

And moreover the question i linked is quite old so i guess there is a new way to do it now?

I current am using JFileChooser here is a piece of code that will help you reproduce what i have now :

import javax.swing.*;
import javax.swing.event.*;

import java.awt.*;
import java.awt.event.*;

public class Try{
    public static void main(String[] args) {
        try {
            
           UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {
    }
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setDialogTitle("Choose directory");    
    fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    fileChooser.addActionListener(new MyActionListener());
    fileChooser.showOpenDialog(new JFrame());
    }
}

class MyActionListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent actionEvent) {
        switch (actionEvent.getActionCommand()){
            case "ApproveSelection":{
                // DO SOMETHING
                break;
            }
            case "CancelSelection":{
                // DO SOMETHING
                break;
            }
        }
    }
}
Jaysmito Mukherjee
  • 1,467
  • 2
  • 10
  • 29
  • *"I have tried: `..getSystemLookAndFeelClassName());` No difference!"* When did you try it? The call must be made before the file chooser is established. It's usually called before *any* components are created. – Andrew Thompson Apr 10 '21 at 17:37
  • @AndrewThompson sorry it got skipped while copying the code to the question! i added it – Jaysmito Mukherjee Apr 10 '21 at 17:40
  • 1
    (1-) *here is a piece of code that will help you reproduce what i have now* - it will not compile therefore we can't test it. Every question should have an [mre]. We should not have to waste time reminding you on every question. – camickr Apr 10 '21 at 18:49
  • @camickr i am sorry – Jaysmito Mukherjee Apr 10 '21 at 19:01
  • Does it work if `MyActionListener` is removed? If not, why is it in there? I'd ask the same question re each line of code. Comment each out in turn - if they don't change the result, remove them. – Andrew Thompson Apr 10 '21 at 19:17
  • As an aside, this is [the result of setting the PLAF to the system PLAF](https://i.stack.imgur.com/xzIo0.png) here. If the setting of the PLAF is commented out, [here is how it looks](https://i.stack.imgur.com/HwpJz.png). Those look quite different to me. – Andrew Thompson Apr 10 '21 at 19:21
  • @AndrewThompson no i tried removing everything excepti the initialization ans sho Dialoog but no effect it is looking same as the second picture you linked but i got another solution which seems to work as of now – Jaysmito Mukherjee Apr 11 '21 at 10:55

1 Answers1

0

So, I could not doscover why is setLookAndFeel is not working for me!

But i have got a solution to the problem!

So, here is the code:

import java.util.*;
import java.io.*;
import java.awt.*;

public class Try2{
    public static void main(String[] args) throws Exception{
        Process p = Runtime.getRuntime().exec("zenity --file-selection --directory");
        Scanner sc = new Scanner(p.getInputStream());
        System.out.println("-----------------------------------");
        while (sc.hasNextLine()) {
            System.out.println(sc.nextLine());
        }
        System.out.println("-----------------------------------");
    }
}

This is the closest to what i want

Jaysmito Mukherjee
  • 1,467
  • 2
  • 10
  • 29