2015-05-03 75 views
-1

我有组合框,我想用它来配置服务延迟时间:设置自定义值,组合框

import javafx.application.Application; 
import static javafx.application.Application.launch; 
import javafx.beans.value.ChangeListener; 
import javafx.beans.value.ObservableValue; 
import javafx.geometry.Insets; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.control.ComboBox; 
import javafx.scene.control.Label; 
import javafx.scene.layout.GridPane; 
import javafx.stage.Stage; 
import javafx.util.Duration; 

public class MainApp extends Application 
{ 
    public static void main(String[] args) 
    { 
     launch(args); 
    } 

    private MyService myService = new MyService(); 

    @Override 
    public void start(Stage stage) 
    { 
     myService.setDelay(new Duration(300)); 
     myService.setPeriod(new Duration(1000)); 

     myService.start(); 
     stage.setTitle("ComboBoxSample"); 
     Scene scene = new Scene(new Group(), 450, 250); 

     ComboBox emailComboBox = new ComboBox(); 
     emailComboBox.getItems().addAll("Stop", "1 Second", "5 Seconds", "10 Seconds", "15 Seconds"); 
     emailComboBox.setPromptText("Email address"); 
     emailComboBox.valueProperty().addListener(new ChangeListener<String>() 
     { 
      @Override 
      public void changed(ObservableValue ov, String t, String t1) 
      { 
       if (t1.equals("Stop")) 
       { 
        myService.cancel(); 
       } 
       if (t1.equals("1 Second")) 
       { 
        myService.setPeriod(new Duration(1000)); 
       } 
       if (t1.equals("5 Second")) 
       { 
        myService.setPeriod(new Duration(5000)); 
       } 
       if (t1.equals("10 Second")) 
       { 
        myService.setPeriod(new Duration(10000)); 
       } 
       if (t1.equals("15 Second")) 
       { 
        myService.setPeriod(new Duration(15000)); 
       } 
      } 
     }); 

     GridPane grid = new GridPane(); 
     grid.setVgap(4); 
     grid.setHgap(10); 
     grid.setPadding(new Insets(5, 5, 5, 5)); 
     grid.add(new Label("To: "), 0, 0); 
     grid.add(emailComboBox, 1, 0); 

     Group root = (Group) scene.getRoot(); 
     root.getChildren().add(grid); 
     stage.setScene(scene); 
     stage.show(); 
    } 
} 

有没有减少,我用它来设置服务太多,如果()开关的情况下任何取巧的方法延迟期。 我想添加emailComboBox.setEditable(true);并基于我的自定义输入我想设置服务延迟期。

+1

如果格式将是'XX秒'然后你可以简单地在空间上使用String Tokenize来获得第一个数字,使用'Integer.parseInt()'把它作为'int'获得,然后乘以1000毫秒。如果它无法解析为具有异常的int,则假定它是'停止'或输入错误并调用'cancel()'。 –

+0

@AaronD你能告诉我一些代码示例吗? –

+1

似乎真的不应该在'ComboBox'中首先使用'String's ...... –

回答

1

由于用户有效选择Duration,因此ComboBox的数据类型应为Duration,而不是String。安装电池工厂,配置Duration对象是如何显示在组合框中:

ComboBox<Duration> combo = new ComboBox<>(
      FXCollections.observableArrayList(
       Duration.UNKNOWN, 
       Duration.seconds(1), 
       Duration.seconds(5), 
       Duration.seconds(10), 
       Duration.seconds(15))); 

    combo.setCellFactory(lv -> createListCell()); 
    combo.setButtonCell(createListCell()); 

    combo.valueProperty().addListener((obs, oldValue, newValue) -> { 
     if (newValue == null || newValue == Duration.UNKNOWN) { 
      myService.cancel(); 
     } else { 
      myService.setPeriod(newValue); 
     } 
    }); 

通过自定义单元实现看起来像

private ListCell<Duration> createListCell() { 
    return new ListCell<Duration>() { 
     @Override 
     public void updateItem(Duration item, boolean empty) { 
      super.updateItem(item, empty); 
      if (empty) { 
       setText(null); 
      } else { 
       if (item == Duration.UNKNOWN) { 
        setText("Stop"); 
       } else { 
        setText(String.format("%.0f Second", item.toSeconds())); 
       } 
      } 
     } 
    }; 
} 
+0

我在myService.setPeriod(newValue)之前加了''myService.restart();''服务没有重新启动后,我选择停止。它是否正确? –

+0

是的。设置期限不会导致停止的服务启动。 –