0

I'm asking for your help.

I'm developing an application in JavaFX who "scan" Mp3 files to get ID3tag.

Here is my problem. I did a foreach loop of a list for every .mp3 found but I'd like to increment a label which inform the progression of the list.

Here is my code

private ArrayList checkMp3File(ArrayList<String> lsMp3file, String sDir) throws UnsupportedTagException, InvalidDataException, IOException
{
    this.currentData = 1;
    int size = lsMp3file.size();
    ArrayList<DataSong> lsds = new ArrayList<>();
    for(String mp3file : lsMp3file)
    {
        this.labelUpdate.setText(this.current++ + " of " + " size");
        DataSong ds = new DataSong();
        Mp3File mp3 = new Mp3File(mp3file);
        ds.setLenghtOfMp3inSec(mp3.getLengthInSeconds());
        ds.setBitRateOfMp3(mp3.getBitrate());
        ds.setSampleRate(mp3.getSampleRate());
        ds.setVbrOrCbr(mp3.isVbr());
    }    

Actually, when the loop progress my window interface is completely freeze. And only when the loop is finished, the label updated. Someone can explain why ?

I already thank you for your answers.

EDIT :

Here is my fully code

public class LaunchOption extends Pane {

private final HBox launchAndSend = new HBox();
private final HBox browseAndField = new HBox();
private final HBox jsonAndAdvance = new HBox();

private ArrayList<DataSong> lsWithData = new ArrayList<>();
private String sendJson;
private File selectedDirectory;
private User user;

private int currentData;

private final ProgressIndicator pi = new ProgressIndicator(0);

private final VBox containerElement = new VBox();

private final TextArea displayJson = new TextArea();

private final TextField pathDir = new TextField();
private final TextField nbrOfData = new TextField();

private final Button btnScan = new Button();
private final Button btnSend = new Button();
private final Button btnCheckJson = new Button();
private final Button btnDirectoryBrowser = new Button();

private final Label nbMp3 = new Label();
public Label listAdvance = new Label();

private final Stage home;

public LaunchOption(Stage home){

    this.home = home;
    configureBtnCheckJson();
    configureBtnScan();
    configureBtnSend();
    configureLabelMp3();
    configureBtnDirectoryBrowser();
    configureTextAreaDisplayJson();
    configureTextFieldPathDir();
    configureTextFieldNbDataMp3();
    configureHBoxlaunchSend();
    configureHBoxBrowseAndField();
    configureHBoxJsonAndAdvance();
    configureContainer();
    this.getChildren().addAll(containerElement,launchAndSend);
}

private void configureLabelMp3()
{
    nbMp3.setText("MP3");
}



private void configureBtnScan(){
    btnScan.setText("Scan");
    btnScan.setOnAction(event->{
        ArrayList<String> Mp3FileData;
        Mp3FileData = mapFilesMp3(selectedDirectory.getAbsolutePath());
        System.out.println("ListSize = " + Mp3FileData.size());
        nbrOfData.setText(String.valueOf(Mp3FileData.size()));

        try {
            lsWithData = checkMp3File(Mp3FileData, selectedDirectory.getAbsolutePath());
        } catch (UnsupportedTagException ex) {
            Logger.getLogger(MusiScanMp3agic.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidDataException ex) {
            Logger.getLogger(MusiScanMp3agic.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(MusiScanMp3agic.class.getName()).log(Level.SEVERE, null, ex);
        }
            pi.setProgress(1);
    });

}



private void configureBtnDirectoryBrowser(){
    btnDirectoryBrowser.setText("Browse ...");
    btnDirectoryBrowser.getStyleClass().add("round-red");
    btnDirectoryBrowser.setOnAction(event-> {
        DirectoryChooser dc = new DirectoryChooser();
        selectedDirectory = dc.showDialog(home);

        pi.setProgress(0.35);

        if(selectedDirectory == null)
        {

            pathDir.setText("No directory selected");
        }
        else
        {
            pathDir.setText(selectedDirectory.getAbsolutePath());
            String Text = pathDir.getText();
            System.out.println(Text.toString());
        }
    });
}
private static String regexMp3()
{
    return "^.*\\.(mp3)$";
}

private ArrayList mapFilesMp3(String sDir){
    ArrayList<String> ls = new ArrayList<>();
    printFnames(sDir,ls);
    return ls;
}

private static void printFnames(String sDir, ArrayList<String> ls)
{
    File[] faFiles = new File(sDir).listFiles();
    for(File file : faFiles)
    {
        if(file.getName().matches(regexMp3())) 
        {
            // System.out.println(file.getAbsolutePath());
            ls.add(file.getAbsolutePath());
        }
        if(file.isDirectory())
        {
            printFnames(file.getAbsolutePath(), ls);
        }
    }
}

private ArrayList checkMp3File(ArrayList<String> lsMp3file, String sDir) throws UnsupportedTagException, InvalidDataException, IOException
{
    this.currentData = 1;
    int size = lsMp3file.size();
    ArrayList<DataSong> lsds = new ArrayList<>();
    for(String mp3file : lsMp3file)
    {
        System.out.println(this.currentData++);
        DataSong ds = new DataSong();
        Mp3File mp3 = new Mp3File(mp3file);
        ds.setLenghtOfMp3inSec(mp3.getLengthInSeconds());
        ds.setBitRateOfMp3(mp3.getBitrate());
        ds.setSampleRate(mp3.getSampleRate());
        ds.setVbrOrCbr(mp3.isVbr());
        if(mp3 != null){
            ds.setAbsoluteLocation(mp3.getFilename());
            ds.setLocation(removeSDir(mp3.getFilename(), sDir));
            if(mp3.hasId3v2Tag())
            {
                ID3v2 id3v2Tag = mp3.getId3v2Tag();
                if(!(id3v2Tag.getArtist() == null))
                {
                    ds.setArtist(id3v2Tag.getAlbumArtist());
                }
                if(!(id3v2Tag.getAlbum() == null))
                {
                    ds.setAlbum((id3v2Tag.getAlbum()));
                }
                if(!(id3v2Tag.getTitle() == null))
                {
                    ds.setTitle(id3v2Tag.getTitle());
                }
                if(!(id3v2Tag.getTrack() == null))
                {
                    ds.setTrackOnAlbum(id3v2Tag.getTrack());
                }
                if(!(id3v2Tag.getYear() == null) && !(id3v2Tag.getYear().isEmpty()))
                {
                   ds.setYearReleased(id3v2Tag.getYear());
                }
                if(!(id3v2Tag.getGenreDescription() ==  null))
                {
                   ds.setGenre(id3v2Tag.getGenreDescription());
                }
                if(!(id3v2Tag.getComposer() == null))
                {
                    ds.setComposer(id3v2Tag.getComposer());
                }
                if(!(id3v2Tag.getPublisher() ==  null))
                {
                    ds.setPublisher(id3v2Tag.getPublisher());
                }
                if(!(id3v2Tag.getOriginalArtist() ==  null))
                {
                    ds.setOriginArtist(id3v2Tag.getOriginalArtist());
                }
                if(!(id3v2Tag.getAlbumArtist() == null))
                {
                    ds.setAlbumArtString(id3v2Tag.getAlbumArtist());
                }
                if(!(id3v2Tag.getCopyright() == null))
                {
                    ds.setCopyright(id3v2Tag.getCopyright()); 
                }
                if(!(id3v2Tag.getUrl() == null))
                {
                    ds.setUrl(id3v2Tag.getUrl());
                }  
            }
        }
        lsds.add(ds);
    }
    return lsds;
}

I presume that what I should do is to make my checkMp3File method into a Task method which will do a background thread ?

NickyL
  • 11
  • 2
  • Possible duplicate of [Constantly Update UI in Java FX worker thread](http://stackoverflow.com/questions/20497845/constantly-update-ui-in-java-fx-worker-thread) – fabian Feb 09 '16 at 18:53

1 Answers1

1

There is not enough code to be sure but I think you are probably calling your method on the JavaFX application thread which then blocks your UI. You should read the documentation about concurrency in JavaFX. https://docs.oracle.com/javase/8/javafx/interoperability-tutorial/concurrency.htm

mipa
  • 10,369
  • 2
  • 16
  • 35