I want to open a screen in JavaFX that will load a search in the database and bring it to a Table. I would like this search to be done by another Thread
, as it may take a while and I would like to update a ProgressBar
with the Thread
process.
I made this code that is returning me NullPointerException
to my fxml attributes.
I'm still wondering how initialize
of Controller
classes works, maybe this is the problem.
For the fxml I use Scene Builder.
This is the method of my UsuarioController
class that loads the Logs.fxml file:
private void carregarLogsDoUsuario(){
LogsController.setCodUsuario(usuario.getCodUsuario());
try {
Parent p = fxmlLoader.load(getClass().getResource(TelasFramework.LOGS_FXML));
Stage s = new Stage();
Scene cena = new Scene(p);
s.setScene(cena);
//this.controladorDeTela.getPalco().setScene(cena);
s.show();
} catch (IOException ex) {
FabricaDeDialogos.montaDialogoErro(ex, "Carregando Logs do usuário:");
ex.printStackTrace();
}
}
And this is the class LogsControlles
that controls the fxml and would perform the process:
public class LogsController implements Initializable {
//<editor-fold desc="FXML Scene Builder">
@FXML
private AnchorPane painelPrincipal;
@FXML
private TableView<Logs> tbLogs;
@FXML
private TableColumn<Logs, String> colunaAcao;
@FXML
private TableColumn<Logs, String> colunaData;
@FXML
private TableColumn<Logs, String> colunaHora;
@FXML
private ProgressBar barraDeProgresso;
@FXML
private Label labelInfo;
//</editor-fold>
private static int codUsuario;
UsuarioDAO usuDao = new UsuarioDAO();
@Override
public void initialize(URL url, ResourceBundle rb) {
System.out.println("Chamando método carregarLogs();");
carregarLogs();
}
private void carregarLogs() {
Task<Void> task = new Task<Void>() {
@Override
protected Void call() throws Exception {
Platform.runLater(new Runnable() {
@Override
public void run() {
System.out.println("Chamou método call();");
updateMessage("Carregando Logs do usuários: ");
barraDeProgresso.setProgress(0);
try {
colunaAcao.setCellValueFactory(new PropertyValueFactory<>("acao"));
colunaData.setCellValueFactory(new PropertyValueFactory<>("data"));
colunaHora.setCellValueFactory(new PropertyValueFactory<>("hora"));
barraDeProgresso.setProgress(getTotalWork() - getWorkDone());
tbLogs.setItems(FXCollections.observableList(usuDao.listarLogsAcaoDoUsuario(codUsuario)));
barraDeProgresso.setProgress(getTotalWork() - getWorkDone());
updateMessage("Logs carregados.");
System.out.println("Logs Carregados");
} catch (InterruptedException e) {
e.printStackTrace();
FabricaDeDialogos.montaDialogoErro(e, "Thread de carregamento de Logs interrompida.");
} catch (Exception ex) {
ex.printStackTrace();
FabricaDeDialogos.montaDialogoErro(ex, "Thread de carregamento de Logs interrompida.");
}
}
});
return null;
}
};
labelInfo.textProperty().bind(task.messageProperty());
barraDeProgresso.progressProperty().bind(task.progressProperty());
new Thread(task).start();
}
public static int getCodUsuario() {
return codUsuario;
}
public static void setCodUsuario(int codUsuario) {
LogsController.codUsuario = codUsuario;
}
}