Skip to content

Instantly share code, notes, and snippets.

@nlowe
Last active July 11, 2018 16:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nlowe/e18395381d451a6ae0fb to your computer and use it in GitHub Desktop.
Save nlowe/e18395381d451a6ae0fb to your computer and use it in GitHub Desktop.
JavaFX Table Cell Progress Bar in CSS
import javafx.fxml.Initializable;
import javafx.scene.control.Slider;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable {
public TableView view;
public Slider slider;
private DataContainer model = new DataContainer();
// <ModelType,ColumnType>
public TableColumn<DataObject, Double> actionCell;
@Override
public void initialize(URL location, ResourceBundle resources) {
model.stream().forEach(obj -> {
obj.setName("asdf");
obj.valueProperty().bind(slider.valueProperty());
});
view.setItems(model.getValue());
//Whatever the "percent done" property is
actionCell.setCellValueFactory(cell -> cell.getValue().valueProperty().asObject());
actionCell.setCellFactory(column -> new TableCell<DataObject, Double>(){
@Override
protected void updateItem(Double item, boolean empty) {
super.updateItem(item, empty);
if(item != null && !empty){
setText(item.toString());
double percent = item * 100;
String color = "169f16";
if(percent > 90){
color = "ff0303";
}else if(percent > 75){
color = "ffd742";
}
setStyle("-fx-background-color: linear-gradient(from 0% 100% to " + (percent) +"% 100%, #" + color + ", #" + color + " 99.99%, transparent);");
}
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment