How can i do to block the main thread when show a dialog
945740Jul 21 2012 — edited Jul 23 2012I have a problem for blocking the main ui thread when show a dialog (the dialog was created by the main Application), and when the dialog dismissed, the main Application can go on the next step.
The following is my code:
public class TitledPaneExample extends Application {
private TitledPaneExample instance;
StackPane mainModalDimmer;
/*
* (non-Javadoc)
*
* @see javafx.application.Application#start(javafx.stage.Stage)
*/
@Override
public void start(Stage primaryStage) throws Exception {
instance = this;
primaryStage.setTitle(this.getClass().toString());
final StackPane layerPane = new StackPane();
// layerPane.setDepthTest(DepthTest.DISABLE);
layerPane.setStyle("-fx-background-color: BLACK;");
VBox vb = new VBox();
vb.setStyle("-fx-background-color: BLUE;");
vb.getChildren().add(new Label("1"));
vb.getChildren().add(new Label("2"));
vb.getChildren().add(new Label("3"));
vb.getChildren().add(new Label("4"));
Button bt1 = new Button("bt1");
bt1.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
System.out.println("bt1 try to new dialog >>>");
Dialog di = new Dialog(instance, "some hint here !");
System.out.println("bt1 dialog new successfully, try to show");
di.show();
System.out.println("bt1 dialog hide ?<<<<");
}
});
vb.getChildren().add(bt1);
layerPane.getChildren().add(vb);
mainModalDimmer = new StackPane();
mainModalDimmer.setId("MainModalDimmer");
mainModalDimmer.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
mainModalDimmer.setVisible(false);
mainModalDimmer.setStyle("-fx-background-color: RED;");
layerPane.getChildren().add(mainModalDimmer);
Scene sc = new Scene(layerPane, 800, 600);
primaryStage.setResizable(true);
primaryStage.setScene(sc);
primaryStage.show();
}
/**
* Show the given node as a floating dialog over the whole application, with
* the rest of the application dimmed out and blocked from mouse events.
*
* @param message
*/
public void showModalMessage(Node message) {
mainModalDimmer.getChildren().add(message);
mainModalDimmer.setOpacity(0);
mainModalDimmer.setVisible(true);
mainModalDimmer.setCache(true);
TimelineBuilder
.create()
.keyFrames(
new KeyFrame(Duration.seconds(1),
new EventHandler<ActionEvent>() {
public void handle(ActionEvent t) {
mainModalDimmer.setCache(false);
}
}, new KeyValue(mainModalDimmer
.opacityProperty(), 1,
Interpolator.EASE_BOTH))).build()
.play();
}
/**
* Hide any modal message that is shown
*/
public void hideModalMessage() {
mainModalDimmer.setCache(true);
TimelineBuilder
.create()
.keyFrames(
new KeyFrame(Duration.seconds(1),
new EventHandler<ActionEvent>() {
public void handle(ActionEvent t) {
mainModalDimmer.setCache(false);
mainModalDimmer.setVisible(false);
mainModalDimmer.getChildren().clear();
}
}, new KeyValue(mainModalDimmer
.opacityProperty(), 0,
Interpolator.EASE_BOTH))).build()
.play();
}
/**
* @param args
*/
public static void main(String[] args) {
launch();
}
public class Dialog extends TitledPane {
private TitledPaneExample owner = null;
private TitledPane instance;
Dialog(TitledPaneExample owner, String message) {
this.owner = owner;
this.parentThreand = Thread.currentThread();
this.instance = this;
this.setExpanded(true);
this.setText("Dialog");
this.setMaxWidth(400);
this.setPrefWidth(300);
this.setMinWidth(200);
Label tx = new Label(message);
tx.setTooltip(new Tooltip(message));
tx.setWrapText(true);
tx.setContentDisplay(ContentDisplay.LEFT);
Button bt = new Button("OK");
bt.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
hide();
}
});
ScrollPane sp = new ScrollPane();
sp.setHbarPolicy(ScrollBarPolicy.AS_NEEDED);
sp.setVbarPolicy(ScrollBarPolicy.AS_NEEDED);
sp.setContent(tx);
HBox hb = new HBox(30);
hb.setPrefHeight(40);
hb.setAlignment(Pos.CENTER_RIGHT);
hb.getChildren().add(bt);
BorderPane bp = new BorderPane();
bp.setCenter(sp);
bp.setBottom(hb);
this.setContent(bp);
}
public void show() {
System.out.println("Dialog show() 1>>>");
owner.showModalMessage(instance);
System.out.println("Dialog show() 2>>>");
}
public void hide() {
System.out.println("Dialog hide() 1>>>");
this.owner.hideModalMessage();
System.out.println("Dialog hide() 2>>>");
}
}
}