JavaFX问题-ProgressBar(无法从静态内容引用非静态字段'progressBar')

时间:2018-10-05 03:17:34

标签: java javafx static

我正在创建一个程序,该程序读取CSV文件(商品编号)并搜索网页(例如asdfjh.com/12547.hmtl、asdfjh.com/12548.hmtl、asdfjh.com/12549.hmtl、asdfjh.com /12550.hmtl)。

目的是在网站上获取eans并将其添加到CSV文件中。我有一个我不明白的问题。

代码:

package beginnDesignUndFunktion.code;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.ProgressBar;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

import java.io.File;
import java.io.IOException;

public class DesignController {
    static File file;

    @FXML
    ProgressBar progressBar = new ProgressBar();

    @FXML
    protected void handleButtonAction(ActionEvent event) {
        FileChooser chooser = new FileChooser();
    chooser.setTitle("Open File");
    file = chooser.showOpenDialog(new Stage());
    System.out.println(file.toString());
    try {
        int anzahl = CsvVerabeitung.countLines(file.toString());
        System.out.println(anzahl + " Dateien");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

@FXML
protected void handleSubmitButtonAction(ActionEvent event) {
    String path = file.toString();
    Reader.read(path);
    Writer.write();
    }

    @FXML
    protected static void setProgress(int y){
        int anzahl = 0;
        try {
            anzahl = CsvVerabeitung.countLines(file.toString());    
        } catch (IOException e) {
            e.printStackTrace();
        }
        double progress = ((double) y) / ((double) anzahl);
        System.out.println(progress);
        progressBar.setProgress(progress);
    }
}

与其他班级的交流有效。我在做什么错了?

enter image description here

2 个答案:

答案 0 :(得分:0)

确定现在是这样的:

package beginnDesignUndFunktion.code;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.ProgressBar;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

import java.io.File;
import java.io.IOException;

public class DesignController {
    File file;
    CsvVerabeitung c = new CsvVerabeitung();
    Reader r = new Reader();
    Writer w = new Writer();

    @FXML
    ProgressBar progressBar = new ProgressBar();

    @FXML
    protected void handleButtonAction(ActionEvent event) {
        FileChooser chooser = new FileChooser();
        chooser.setTitle("Open File");
        file = chooser.showOpenDialog(new Stage());
        System.out.println(file.toString());
        try {
            int anzahl = c.countLines(file.toString());
            System.out.println(anzahl + " Dateien");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @FXML
    protected void handleSubmitButtonAction(ActionEvent event) {
        String path = file.toString();
        r.read(path);
        w.write();
    }

    protected void setProgress(int y){
        int anzahl = 0;
        try {
            anzahl = c.countLines(file.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        double progress = ((double) y) / ((double) anzahl);
        System.out.println(progress);
        progressBar.setProgress(progress);
    }
}

和:

package beginnDesignUndFunktion.code;

import java.io.IOException;

public class Writer extends DesignController{

    Reader r = new Reader();
    CsvVerabeitung c = new CsvVerabeitung();
    DesignController d = new DesignController();

    protected void write() {
        Reader r1 = new Reader();
        String path = "C:\\Users\\marco\\Desktop\\CSV Verarbeitung\\src\\beginnDesignUndFunktion\\data\\test7Files2.csv";
        java.io.BufferedWriter FileWriter;

        {
            try {
                FileWriter = new java.io.BufferedWriter(new java.io.FileWriter(new java.io.File(path)));
                FileWriter.write("id;ean");
                int i=0;
                for(String x: r.number){
                    FileWriter.append("\n" + r.number.get(i) + ";" + r.ean.get(i) + ";");
                    i++;
                    d.setProgress(i);
                }
                FileWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

现在的问题:java.lang.StackOverflowError

此网站的制作方式:DDDD

真的不得不笑了!

btw @kleopatra:将锁定约定。 @slaw:progressBar不能正常工作。

编辑

The problem is the d.setProgress(i); call.

答案 1 :(得分:0)

解决不愉快...

复制Writer-Class的代码并将其放入Controller-Class。

这不是很好的解决方案,但可以使用