2015-09-26 14 views
7

Tôi có một Pane với một số điều khiển và một nút. Khi tôi nhấp vào nút, tôi muốn hiển thị ProgressIndicator ở giữa cửa sổ mà không xóa bất kỳ điều khiển nào.Làm thế nào để hiển thị ProgressIndicator ở trung tâm của Pane trong Javafx

Khi tôi thêm ProgressIndicator vào ngăn trong khi onAction của nút, nó sẽ thêm nó bên dưới nút. Tôi muốn nó che phủ trên ngăn.

Hình dưới đây giải thích những gì tôi muốn.

progress indicator

package fx; 

import javafx.application.Application; 
import javafx.event.ActionEvent; 
import javafx.event.EventHandler; 
import javafx.geometry.Pos; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.ProgressIndicator; 
import javafx.scene.control.TextField; 
import javafx.scene.layout.VBox; 
import javafx.stage.Stage; 

public class Main extends Application { 

@Override 
public void start(Stage arg0) throws Exception { 
    final VBox bx = new VBox(); 
    bx.setAlignment(Pos.CENTER); 
    TextField userName = new TextField("User Name"); 
    userName.setMaxWidth(200); 
    TextField email = new TextField("Email"); 
    email.setMaxWidth(200); 
    Button submit = new Button("Submit"); 
    submit.setOnAction(new EventHandler<ActionEvent>() { 

     public void handle(ActionEvent event) { 
      ProgressIndicator pi = new ProgressIndicator(); 
      //adding here but it is adding at below of button 
      //how to do here 
      bx.getChildren().add(pi); 
      //Further process 
      } 
    }); 
    bx.getChildren().addAll(userName, email, submit); 
    Scene c = new Scene(bx); 
    arg0.setScene(c); 
    arg0.setMinWidth(500); 
    arg0.setMinHeight(500); 
    arg0.show(); 
    } 

    public static void main(String[] args) { 
    Main h = new Main(); 
    h.launch(args); 
    } 
} 

Trả lời

12

Bạn cần phải sử dụng một StackPane như bố trí gốc của bạn thay vì sử dụng một VBox. StackPane cho phép bạn xếp chồng các nút lên nhau (thứ tự z).

Trên hành động của nút, bạn có thể tạo ProgressIndicator mới và thêm nó vào StackPane của bạn. Tôi đã giới thiệu một VBox khác làm phụ huynh cho chỉ báo, bởi vì tôi không muốn chỉ báo nắm bắt tất cả không gian có sẵn. Bạn có thể disable VBox đã có để nhận được hiệu ứng greying về thao tác của nút sau khi quá trình được thực hiện, bạn có thể bật lại VBox.

enter image description here


import javafx.application.Application; 
import javafx.event.ActionEvent; 
import javafx.event.EventHandler; 
import javafx.geometry.Pos; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.ProgressIndicator; 
import javafx.scene.control.TextField; 
import javafx.scene.layout.StackPane; 
import javafx.scene.layout.VBox; 
import javafx.stage.Stage; 

public class Main extends Application { 

    @Override 
    public void start(Stage arg0) throws Exception { 
     StackPane root = new StackPane(); 
     VBox bx = new VBox(); 
     bx.setAlignment(Pos.CENTER); 
     TextField userName = new TextField("User Name"); 
     userName.setMaxWidth(200); 
     TextField email = new TextField("Email"); 
     email.setMaxWidth(200); 
     Button submit = new Button("Submit"); 
     submit.setOnAction(new EventHandler<ActionEvent>() { 

      public void handle(ActionEvent event) { 
       ProgressIndicator pi = new ProgressIndicator(); 
       VBox box = new VBox(pi); 
       box.setAlignment(Pos.CENTER); 
       // Grey Background 
       bx.setDisable(true); 
       root.getChildren().add(box); 
      } 
     }); 
     bx.getChildren().addAll(userName, email, submit); 
     root.getChildren().add(bx); 
     Scene c = new Scene(root); 
     arg0.setScene(c); 
     arg0.setMinWidth(500); 
     arg0.setMinHeight(500); 
     arg0.show(); 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 
} 
+0

này hoạt động tốt, nhưng lớp phủ để hiển thị màu xám tôi đã phải thêm vbox trong cách bố trí của tôi bởi vì một trong những bx.setDisable (true); wont đủ để hiển thị màu xám, tôi cũng đã thêm văn bản tải bằng cách sử dụng Text label = new Text ("Đang tải ... Vui lòng đợi"); label.setBoundsType (TextBoundsType.VISUAL); label.setFill (Color.BLACK); –

Các vấn đề liên quan