user3054314
user3054314

Reputation: 5

Changing TextArea in another class

There is a class FXMLDocumentController. There is a class ThreadForFile which has been inheriting from class Thread (which I read data from a file) After I pressed the button (in the class FXMLDocumentController) I create a flow of class ThreadForFile. I need to in the class ThreadForFile, when reading, the string displayed in the TextArea. But I can not figure out if I pass a parameter TextArea, and change it in the constructor, then there is a change in this component.But if I assign the class field this TextArea, it displays an error : 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");

            }
        });
.
.
.
.
.
.
.
.
.
.
}

Upvotes: 0

Views: 1390

Answers (1)

ItachiUchiha
ItachiUchiha

Reputation: 36722

You are trying to access a JavaFX Control outside the JavaFX Application thread.

Never use your controls, out of your Controller or pass them as a parameter to other methods. Instead, try to pass data to the controller, which in turn will set it to the controls inside the controller.

Some useful links are on passing data to the Controller :

Passing Parameters JavaFX FXML

How to have constructor with arguments for controller?

Some useful links on multi-threading in JavaFX:

How do I safely modify JavaFX GUI nodes from my own Thread?

JavaFX working with threads and GUI

Upvotes: 1

Related Questions