Open Folder to select File

Asked

Viewed 1,252 times

2

How do I open the file selection box and only choose songs with the extension .mp3?

I am using Windowbuilder but the menu does not provide such option for quick creation.

  • 1

    Post your code so the community can analyze.

  • 1

    Take a look here: https://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html. Well, I believe this is the path of stones to your doubt.

  • 1

    Despite a little poorly formulated the question, it is possible to answer yes within the context asked. For this I voted to leave open.

1 answer

3


According to these two answers found in Soen:

Adding the JFileChooser

Apparently JFileChooser not this gift by default, but you can add, follow the steps:

  1. Go to system and click on Choose Component:

    enter image description here

  2. Now search for your component add it.

    enter image description here

Selecting the file types

You’ll need the method getDescription and accept, example:

JFileChooser fileChooser = new JFileChooser();

fileChooser.setFileFilter(new FileFilter()
{
   public String getDescription() {
       return "Audio Files (*.mp3)";
   }

   public boolean accept(File f) {
       if (f.isDirectory()) {
           return true;
       } else {
           String filename = f.getName().toLowerCase();
           return filename.endsWith(".mp3");
       }
   }
});

I have not tested the code yet, but it seems to work, please let me know if there is any problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.