2013-07-22 75 views
0

我有一個切換按鈕這個簡單的例子:如何更改按鈕標籤在加載內容

public class test extends Application 
{ 

    @Override 
    public void start(Stage primaryStage) 
    { 

     final VBox vbox = new VBox(); 

     String pillButtonCss = DX57DC.class.getResource("PillButton.css").toExternalForm(); 

     // create 3 toggle buttons and a toogle group for them 
     ToggleButton tb1 = new ToggleButton("Left Button"); 
     tb1.setId("pill-left"); 
     ToggleButton tb2 = new ToggleButton("Center Button"); 
     tb2.setId("pill-center"); 
     ToggleButton tb3 = new ToggleButton("Right Button"); 
     tb3.setId("pill-right"); 

     final ToggleGroup group = new ToggleGroup(); 
     tb1.setToggleGroup(group); 
     tb2.setToggleGroup(group); 
     tb3.setToggleGroup(group); 
     // select the first button to start with 
     group.selectToggle(tb1); 

     final Rectangle rect1 = new Rectangle(300, 300); 
     rect1.setFill(Color.DARKOLIVEGREEN); 
     final Rectangle rect2 = new Rectangle(300, 300); 
     rect2.setFill(Color.LAVENDER); 
     final Rectangle rect3 = new Rectangle(300, 300); 
     rect3.setFill(Color.LIGHTSLATEGREY); 

     tb1.setUserData(rect1); 
     tb2.setUserData(rect2); 
     tb3.setUserData(rect3); 

     group.selectedToggleProperty().addListener(new ChangeListener<Toggle>() 
     { 
      @Override 
      public void changed(ObservableValue<? extends Toggle> ov, Toggle toggle, Toggle new_toggle) 
      { 
       if (new_toggle == null) 
       { 
       } 
       else 
       { 
        vbox.getChildren().set(1, (Node) group.getSelectedToggle().getUserData()); 
       } 
      } 
     }); 

     HBox hBox = new HBox(); 
     hBox.getChildren().addAll(tb1, tb2, tb3); 
     hBox.setPadding(new Insets(10, 10, 10, 10)); 
     hBox.getStylesheets().add(pillButtonCss); 

     vbox.getChildren().addAll(hBox, (Node) group.getSelectedToggle().getUserData()); 

     StackPane root = new StackPane(); 
     root.getChildren().add(vbox); 

     Scene scene = new Scene(root, 800, 850); 

     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

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

我注意到,如果我把沉重的業務邏輯轉換的按鈕,而我切換按鈕,看來該應用程序掛起。

當我切換按鈕時是否可以顯示「Loading ..」而不是按鈕標籤?

+0

何時發生「重商業邏輯」?它是按鈕的點擊操作嗎? –

+0

業務邏輯是放置在'tb1.setUserData(rect1);'中的對象。當我在'setUserData()'中切換對象時,應用程序掛起。 –

回答

1

我真的不知道如何掛起,但你可以嘗試在後臺線程中更改工作區。使用Button標籤的Togeher可能如下所示:

public class test 
    extends Application 
{ 

BorderPane workspace = new BorderPane(); 

@Override 
public void start(Stage primaryStage) 
{ 

    final VBox vbox = new VBox(); 

// String pillButtonCss = DX57DC.class.getResource("PillButton.css").toExternalForm(); 

    // create 3 toggle buttons and a toogle group for them 
    ToggleButton tb1 = new ToggleButton("Left Button"); 
    tb1.setId("pill-left"); 
    ToggleButton tb2 = new ToggleButton("Center Button"); 
    tb2.setId("pill-center"); 
    ToggleButton tb3 = new ToggleButton("Right Button"); 
    tb3.setId("pill-right"); 

    final ToggleGroup group = new ToggleGroup(); 
    tb1.setToggleGroup(group); 
    tb2.setToggleGroup(group); 
    tb3.setToggleGroup(group); 

    final Rectangle rect1 = new Rectangle(300, 300); 
    rect1.setFill(Color.DARKOLIVEGREEN); 
    final Rectangle rect2 = new Rectangle(300, 300); 
    rect2.setFill(Color.LAVENDER); 
    final Rectangle rect3 = new Rectangle(300, 300); 
    rect3.setFill(Color.LIGHTSLATEGREY); 

    tb1.setUserData(rect1); 
    tb2.setUserData(rect2); 
    tb3.setUserData(rect3); 

    group.selectedToggleProperty().addListener(new ChangeListener<Toggle>() 
    { 
     @Override 
     public void changed(ObservableValue<? extends Toggle> ov, 
          Toggle toggle, 
          final Toggle new_toggle) 
     { 
      if (new_toggle != null) 
      { 
       final String toggleText = ((ToggleButton)new_toggle).getText(); 
       ((ToggleButton)new_toggle).setText("Loading..."); 

       Thread changeThread = new Thread(new Runnable() 
       { 
        @Override 
        public void run() 
        { 
         try 
         { 
          Thread.sleep(5000); 
         } 
         catch (InterruptedException e) 
         { 
         } 

         Platform.runLater(new Runnable() 
         { 
          @Override 
          public void run() 
          { 
           workspace.setCenter((Node)new_toggle.getUserData()); 
           ((ToggleButton)new_toggle).setText(toggleText); 
          } 
         }); 
        } 
       }); 
       changeThread.setDaemon(true); 
       changeThread.start(); 
      } 
     } 
    }); 

    HBox hBox = new HBox(); 
    hBox.getChildren().addAll(tb1, tb2, tb3); 
    hBox.setPadding(new Insets(10, 10, 10, 10)); 
// hBox.getStylesheets().add(pillButtonCss); 

    vbox.getChildren().addAll(hBox, workspace); 

    Scene scene = new Scene(vbox, 800, 850); 

    primaryStage.setScene(scene); 
    primaryStage.show(); 

    // select the first button to start with 
    group.selectToggle(tb1); 
} 

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

Thread.sleep(5000)模擬切換時的「滯後」。