Boa noite pessoal,
Estou tentando criar uma janelinha, uma dialog, que somente exiba um progressbar para indicar que o sistema está funcionando. O meu problema é que até consigo fazer a janela aparecer, mas ou ela é renderizada antes ou depois da outra operação (no caso uma de salvamento) executar.
Para criar a janela eu criei as seguintes classes:
public class DialogUtilPB extends Application{
private static String caminhoPct = "./";
private static String caminhoPctFxml = caminhoPct + "fxml/";
private String title;
private String msg;
private Node node;
private CrtDlgPB control;
public DialogUtilPB(Node node, String msg){
this.node = node;
this.msg = msg;
}
public DialogUtilPB(Node node, String title, String msg){
this(node, msg);
this.title = title;
}
private static String getDlgPB() {
return caminhoPctFxml + "DialogPB.fxml";
}
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = null;
FXMLLoader fxml = new FXMLLoader();
try {
fxml.setLocation(DialogUtilPB.class.getResource(getDlgPB()));
root = (Parent) fxml
.load(DialogUtilPB.class.getResourceAsStream(getDlgPB()));
} catch (IOException e) {
e.printStackTrace();
}
Stage stage = new Stage();
preencheCrtMensagemPb((CrtDlgPB) fxml.getController(), msg, root);
stage.setTitle(title);
stage.setScene(new Scene(root));
stage.initModality(Modality.APPLICATION_MODAL);
if(node != null){
stage.initOwner(node.getScene().getWindow());
}
stage.sizeToScene();
stage.show();
}
public void setTitle(String title) {
this.title = title;
}
public void setMsg(String msg) {
this.msg = msg;
}
public void setNode(Node node) {
this.node = node;
}
private CrtDlgPB preencheCrtMensagemPb(CrtDlgPB x, String texto, Node root) {
x.setMensagem(texto);
x.setRoot(root);
control = x;
return x;
}
public void fechar(){
control.fechar();
}
}
public class CrtDlgPB implements Initializable{
@FXML
private Label lbMsg;
@FXML
private ProgressBar pbProgress;
@FXML
private Node root;
@Override
public void initialize(URL location, ResourceBundle resources) {
pbProgress.setProgress(ProgressBar.INDETERMINATE_PROGRESS);
}
public void setMensagem(String msg) {
this.lbMsg.setText(msg);
}
public void fechar(){
((Stage)root.getScene().getWindow()).close();
}
public void setRoot(Node root) {
this.root = root;
}
}
E tento invocar dessa forma:
public void salvar(){
System.out.println("aki 2 ");
//Crio uma thread
Task<Void> ts = new Task<Void>() {
@Override
protected Void call() throws Exception {
//Instancio a classe
DialogUtilPB x = new DialogUtilPB(root.getRoot(), "Salvando...");
try {
//Executo a janela
x.start(new Stage());
//Realizo um processamento qualquer
for(int i = 0; i<1000000;i++){
System.out.println("aki 1 " + i);
}
//Fecho a janela
x.fechar();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
};
Platform.runLater(ts);
}