在另一个类中更改TextArea

时间:2014-12-09 17:59:41

标签: java javafx

有一个类FXMLDocumentController。 有一个ThreadForFile类,它继承自Thread类(我从文件中读取数据) 按下按钮后(在FXMLDocumentController类中),我创建了一个ThreadForFile类的流程。 我需要在ThreadForFile类中读取时,TextArea中显示的字符串。但我无法弄清楚我是否传递参数TextArea,并在构造函数中更改它,然后此组件发生了更改。但是如果我为此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 个答案:

答案 0 :(得分:1)

您正试图访问JavaFX Control以外的JavaFX Application thread

切勿在控制器之外使用您的控件,或将它们作为参数传递给其他方法。相反,尝试将数据传递给控制器​​,控制器又将其设置为控制器内的控件。

一些有用的链接是将数据传递给Controller:

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