zoukankan      html  css  js  c++  java
  • javafx ComboBox Event and change cell color

    public class EffectTest extends Application {
    public static void main(String[] args) {
        launch(args);
      }
    
      @Override
      public void start(Stage stage) {
        stage.setTitle("ComboBoxSample");
        Scene scene = new Scene(new Group(), 450, 250);
    
        ComboBox emailComboBox = new ComboBox();
        emailComboBox.getItems().addAll("A","B","C","D","E");
        
            emailComboBox.setPromptText("Email address");
        emailComboBox.setEditable(true);        
        emailComboBox.valueProperty().addListener(new ChangeListener<String>() {
            @Override public void changed(ObservableValue ov, String t, String t1) {
              System.out.println(ov);
                System.out.println(t);
                System.out.println(t1);
            }    
        });
        
        emailComboBox.setCellFactory(
            new Callback<ListView<String>, ListCell<String>>() {
                @Override public ListCell<String> call(ListView<String> param) {
                    final ListCell<String> cell = new ListCell<String>() {
                        {
                            super.setPrefWidth(100);
                        }    
                        @Override public void updateItem(String item, 
                            boolean empty) {
                                super.updateItem(item, empty);
                                if (item != null) {
                                    setText(item);    
                                    if (item.contains("A")) {                                    
                                        setTextFill(Color.RED);
                                    }
                                    else if (item.contains("B")){
                                        setTextFill(Color.GREEN);
                                    }
                                    else {
                                        setTextFill(Color.BLACK);
                                    }
                                }
                                else {
                                    setText(null);
                                }
                            }
                };
                return cell;
            }
                        
        });
        
        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();
    
      }
      
    }
  • 相关阅读:
    Sublime安装package control的操作
    Sublime的简单操作
    C# 泛型方法
    C# 数组的讲解(ArrayList 与List的区别)
    免费的天气API
    bootstrapValidator的验证
    sqlServer 多行合并为一行
    bootstrap的安装注意
    SQL数据库中把一个表中的数据复制到另一个表中
    JavaScript的误区
  • 原文地址:https://www.cnblogs.com/rojas/p/4720840.html
Copyright © 2011-2022 走看看