【问题标题】:refresh label during a foreach loop在 foreach 循环期间刷新标签
【发布时间】:2016-05-19 19:08:37
【问题描述】:

我正在寻求你的帮助。

我正在用 JavaFX 开发一个应用程序,它可以“扫描”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 方法,它会做一个后台线程?

【问题讨论】:

标签: javafx foreach label


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2021-07-16
    • 2017-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多