java在另一个类中更改TextArea
有一个类FXMLDocumentController。
有一个从类Thread继承的类ThreadForFile(我从文件中读取数据)
按下按钮(在类FXMLDocumentController中)后,我创建了一个类ThreadForFile的流。
我需要在ThreadForFile类中读取文本区域中显示的字符串。但是我不知道如果我传递了一个参数TextArea,并在构造函数中更改了它,那么这个组件中就发生了更改。但是如果我将class字段分配给这个TextArea,它会显示一个错误:
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
ThreadForFile extends 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");
}
});
.
.
.
.
.
.
.
.
.
.
}
# 1 楼答案
您正试图访问
JavaFX Control
外部的JavaFX Application thread
永远不要在控制器之外使用控件,也不要将它们作为参数传递给其他方法。相反,尝试将数据传递给控制器,然后控制器会将其设置为控制器内的控件
一些有用的链接用于向控制器传递数据:
Passing Parameters JavaFX FXML
How to have constructor with arguments for controller?
有关JavaFX中多线程的一些有用链接:
How do I safely modify JavaFX GUI nodes from my own Thread?
JavaFX working with threads and GUI