有一个类 FXMLDocumentController。
有一个 ThreadForFile 类继承自 Thread 类(我从文件中读取数据)
按下按钮(在 FXMLDocumentController 类中)后,我创建了 ThreadForFile 类的流程。
我需要在类ThreadForFile中,读取时,将字符串显示在TextArea中。但我不知道如果我传递一个参数 TextArea,并在构造函数中更改它,那么这个组件就会发生变化。但是如果我将类字段分配给这个 TextArea,它会显示错误:
线程“JavaFX应用程序线程”java.lang.NullPointerException中的异常
ThreadForFile 扩展了 Thread
private String path;
private long ms;
private int id;
private String name;
private boolean stop = false;
private HTMLEditor web;
private DB db = new DB();
private Button doc;
public void setMS(long ms){
System.out.print(ms);
this.ms =ms;
}
public ThreadForFile(String name,String path,long ms,int id,Button doc) {
this.path = new String();
this.path = path;
this.ms = ms;
this.id = id;
this.name = name;
this.doc = new Button();
this.doc = doc;
}
public void Stop(boolean stop){
this.stop = stop;
}
public void run( ) {
try {
doc.setText("Zsczsc");
System.out.print("asdasd");
File file = new File(path);
File file1 = new File("C:\\out.txt");
BufferedReader br = new BufferedReader (new InputStreamReader(new FileInputStream(file), "UTF-8"));
String line = null;
while( (line = br.readLine()) != null){
if(!Thread.interrupted()) //Проверка прерывания
{
if(!stop){
PrintWriter out = new PrintWriter(file1.getAbsoluteFile());
try {
sytem.out.print(line+"\n");
} finally {
out.close();
}
Thread.sleep(db.getParam().getMS()*1000);
System.out.print("ms" + db.getParam().getMS());
}
}else{
return;
}
}
} catch (Exception ex) {
System.out.print(ex.toString());
Logger.getLogger(ThreadForFile.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void read()throws FileNotFoundException, IOException
{
}
FXMLDocumentController
<!-- language: JavaFX -->
public class FXMLDocumentController implements Initializable {
private long ms = 5;
private ObservableList<ThreadForFile> threadForFile = FXCollections.observableArrayList();
private ObservableList<threadFile> threadFile = FXCollections.observableArrayList();
private int index ;
private DB db = new DB();
private Parametrs param = new Parametrs();
@FXML
private Button btnAdd;
@FXML
private Button btnStop;
@FXML
public Button btnDel;
@FXML
private Button btnStart;
@FXML
private TextField textFValueText;
@FXML
private TextField textFMs;
@FXML
private Button btnUpdate;
@FXML
public static TextArea textArea;
@FXML
private Label nameLabel;
@FXML
private Label pathLabel;
@FXML
private TextField textFName;
@FXML
private TextField textFPath;
@FXML
private TableColumn nameCol;
@FXML
private TableColumn pathCol;
@FXML
private TableColumn statCol;
public static FXMLDocumentController doc;
private ResourceBundle bundle;
@FXML
private TableView table;
@FXML
private void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
}
@Override
public void initialize(URL url, ResourceBundle rb) {
bundle = rb;
System.out.println("You clicked me!");
FileRead file = new FileRead(FXMLDocumentController.this);
Tab1();
Tab2();
Tab3();
param = db.getParam();
System.out.print(param.getMS() + " " + param.getValue());
}
public void Tab1()
{
}
public void Tab2()
{
//root.getChildren().addAll(btn,table,btnStop,btnStart,btnDel,nameField,pathField,name,path);
btnAdd.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent mouseEvent) {
threadFile.add(new threadFile(textFName.getText(),textFPath.getText(),1));
threadForFile.add(new ThreadForFile(threadFile.get(threadFile.size()-1).getName(),threadFile.get(threadFile.size()-1).getPath(),ms,threadFile.size(),btnAdd));
threadForFile.get(threadForFile.size()-1).start();
index = table.getSelectionModel().getSelectedIndex();
System.out.print(index+ "\n");
}
});
.
.
.
.
.
.
.
.
.
.
}
最佳答案
您正在尝试访问 JavaFX 应用程序线程
之外的 JavaFX 控件
。
切勿在 Controller 之外使用控件或将它们作为参数传递给其他方法。相反,尝试将数据传递到 Controller , Controller 又将其设置到 Controller 内的控件。
一些有用的链接是将数据传递到 Controller :
Passing Parameters JavaFX FXML
How to have constructor with arguments for controller?
有关 JavaFX 中多线程的一些有用链接:
关于java - 在另一个类中更改 TextArea,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27385636/