在foreach循环期间刷新标签

时间:2016-02-09 17:17:03

标签: javafx foreach label

我在寻求你的帮助。

我正在JavaFX中开发一个应用程序" scan" Mp3文件获取ID3tag。

这是我的问题。我为每个.mp3找到了一个列表的foreach循环,但是我想增加一个标签来告知列表的进展。

这是我的代码

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

实际上,当循环进度时,我的窗口界面完全冻结。 只有当循环结束时,标签才会更新。 有人可以解释原因吗?

我已经感谢你的答案了。

编辑:

这是我的完整代码

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

我认为我应该做的是将checkMp3File方法变成一个后台线程的Task方法?

1 个答案:

答案 0 :(得分:1)

没有足够的代码可以肯定,但我认为您可能在JavaFX应用程序线程上调用您的方法,然后阻止您的UI。 您应该阅读有关JavaFX中的并发性的文档。 https://docs.oracle.com/javase/8/javafx/interoperability-tutorial/concurrency.htm