如何偵測TextField資料變動(應適用其他元件)

/*偵測單價的狀況,當變動時啟動偵測機制來檢查金額差距*/
textPrice.textProperty().addListener((observable, oldValue, newValue) -> {
System.out.println(“textfield changed from ” + oldValue + ” to ” + newValue);

Check_Order_Workers_Price();

});

參考資料:https://stackoverflow.com/questions/30160899/value-change-listener-for-javafxs-textfield

偵測TableView的資料列,當條件符合時改變字體顏色。

/*NOTE 字體變色*/
s03.setCellFactory(column -> {
return new TableCell() {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);

if(item == null || empty) {
setText(null);
setStyle(“”);
} else {
// Format date.
setText(item);
// Style all dates in March with a different color.
if(item.equals(“銷售”)) {
setTextFill(Color.GREEN);
// setStyle(“-fx-background-color: yellow”);
} else {
setTextFill(Color.RED);
}
}
}
};
});

偵測元件上的鍵盤動作
tf.setOnKeyPressed(new EventHandler<KeyEvent>() {

@Override

public void handle(KeyEvent event) {

if(event.getCode() == KeyCode.A){

// do something

}

}

});

tf.setOnKeyPressed(new EventHandler<KeyEvent>() {

@Override

public void handle(KeyEvent event) {

if(event.getCode() == KeyCode.A){

// do something

}

}

});

參考資料:http://www.javafxchina.net/blog/2015/09/javafx-key-event/

發佈留言

Your email address will not be published. Required fields are marked *