JavaFX按数据库值更改行颜色

时间:2016-05-19 19:57:10

标签: java javafx colors tableview row

有人帮帮我吗?我想根据值更改TableView中的行颜色。 例如:当列colType中的值等于"良好"时,我想将行颜色更改为绿色。 这是我的方法,我用数据库填充TableView:

  public void setCustomers() {

    List<Customer> list = customer.getTable();  //getting results from database
    data = FXCollections.observableArrayList();
    Integer count = 1;
    for (Customer customer1 : list) {
        data.add(new CustomerObj(count++, customer1.getId(), customer1.getName(),
                form.formatDate(customer1.getBorn().toString()), customer1.getStreet(),
                customer1.getCity(), customer1.getIdCustomerType().getPhrase()));

    }

    tblCustomer.setItems(data);
    tblCustomer.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
    colID.setCellValueFactory(cell -> cell.getValue().getPropertyId());
    colName.setCellValueFactory(cell -> cell.getValue().getPropertyName());
    colBorn.setCellValueFactory(cell -> cell.getValue().getPropertyBorn());
    colAddr.setCellValueFactory(cell -> cell.getValue().getPropertyAddr());
    colCity.setCellValueFactory(cell -> cell.getValue().getPropertyCity());
    colType.setCellValueFactory(cell -> cell.getValue().getPropertyType());
    rowOptions.setCellValueFactory(new PropertyValueFactory<>("Button"));
    editCust(); //just filling TableCell with Button
    rowOptions1.setCellValueFactory(new PropertyValueFactory<>("Button"));
    deleteCust(); //just filling TableCell with Button
}

这是CustomerObj类:

package sk.evka.fp.obj;

import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;


public class CustomerObj {

private SimpleIntegerProperty id;
private SimpleIntegerProperty idDB;
private SimpleStringProperty name;
private SimpleStringProperty born;
private SimpleStringProperty addr;
private SimpleStringProperty city;
private SimpleStringProperty type;

public CustomerObj(Integer i, Integer id, String n, String b, String a, String c, String t) {
    this.id = new SimpleIntegerProperty(i);
    this.idDB = new SimpleIntegerProperty(id);
    this.name = new SimpleStringProperty(n);
    this.born = new SimpleStringProperty(b);
    this.addr = new SimpleStringProperty(a);
    this.city = new SimpleStringProperty(c);
    this.type = new SimpleStringProperty(t);
}

public Integer getId() {
    return id.get();
}

public Integer getIdDB() {
    return idDB.get();
}

public String getName() {
    return name.get();
}

public String getBorn() {
    return born.get();
}

public String getAddr() {
    return addr.get();
}

public String getCity() {
    return city.get();
}

public String getType() {
    return type.get();
}

public void setId(Integer i) {
    this.id = new SimpleIntegerProperty(i);
}

public void setIdDB(Integer i) {
    this.idDB = new SimpleIntegerProperty(i);
}

public void setName(String n) {
    name = new SimpleStringProperty(n);
}

public void setBorn(String b) {
    born = new SimpleStringProperty(b);
}

public void setAddr(String a) {
    addr = new SimpleStringProperty(a);
}

public void setCity(String c) {
    city = new SimpleStringProperty(c);
}

public void setType(String t) {
    type = new SimpleStringProperty(t);
}

public SimpleIntegerProperty getPropertyId() {
    return id;
}

public SimpleIntegerProperty getPropertyIdDB() {
    return idDB;
}

public SimpleStringProperty getPropertyName() {
    return name;
}

public SimpleStringProperty getPropertyBorn() {
    return born;
}

public SimpleStringProperty getPropertyAddr() {
    return addr;
}

public SimpleStringProperty getPropertyCity() {
    return city;
}

public SimpleStringProperty getPropertyType() {
    return type;
}

public void setPropertyId(SimpleIntegerProperty i) {
    this.id = i;
}

public void setPropertyIdDB(SimpleIntegerProperty i) {
    this.idDB = i;
}

public void setPropertyName(SimpleStringProperty n) {
    name = n;
}

public void setPropertyBorn(SimpleStringProperty b) {
    born = b;
}

public void setPropertyAddr(SimpleStringProperty a) {
    addr = a;
}

public void setPropertyCity(SimpleStringProperty c) {
    city = c;
}

public void setPropertyType(SimpleStringProperty t) {
    type = t;
}

}

我还没有找到答案。

2 个答案:

答案 0 :(得分:2)

如果要为整行着色,请使用rowFactory根据项属性更改其创建的TableRows的颜色。

此外,您最好适当地使用JavaFX属性:

属性并不意味着要替换它们自己。属性getter应该始终返回相同的属性实例,或者至少返回在公共数据结构中存储侦听器的实例;否则,可以修改该值,而不会被通知该事实(用包含新值的属性替换该属性)。

public static class Item {

    // property only assigned once
    private final StringProperty type;

    public Item(String type) {
        this.type = new SimpleStringProperty(type);
    }

    // getter for value wrapped in property
    public final String getType() {
        return this.type.get();
    }

    // setter for value wrapped in property
    public final void setType(String value) {
        this.type.set(value);
    }

    // property getter
    public final StringProperty typeProperty() {
        return this.type;
    }
}

public static Color typeToColor(String type) {
    if (type == null) {
        return Color.WHITESMOKE;
    }
    switch (type) {
        case "bad":
            return Color.RED;
        case "good":
            return Color.LIME;
        default:
            return Color.WHITESMOKE;
    }
}
TableView<Item> table = new TableView<>(FXCollections.observableArrayList(
        new Item("ok"),
        new Item("bad"),
        new Item("good")));
TableColumn<Item, String> typeColumn = new TableColumn<>("type");
typeColumn.setCellValueFactory(new PropertyValueFactory<>("type"));

table.setRowFactory(tv -> {
    TableRow<Item> row = new TableRow<>();
    StringBinding typeBinding = Bindings.selectString(row.itemProperty(), "type");

    row.backgroundProperty().bind(Bindings.createObjectBinding(()
            -> new Background(new BackgroundFill(typeToColor(typeBinding.get()), CornerRadii.EMPTY, Insets.EMPTY)), typeBinding));
    return row;
});

table.getColumns().add(typeColumn);

答案 1 :(得分:0)

最后我做了这件事:

  Callback<TableColumn<CustomerObj, String>, TableCell<CustomerObj, String>> cellFactory
                    = new Callback<TableColumn<CustomerObj, String>, TableCell<CustomerObj, String>>() {
            public TableCell call(TableColumn p) {
                TableCell cell = new TableCell<CustomerObj, String>() {
                    @Override
                    public void updateItem(String item, boolean empty) {
                        super.updateItem(item, empty);
                        if (!empty) {
                            CustomerObj obj = this.getTableView().getItems().get(getIndex());    

                            setText(obj.getType());                        
                            switch (obj.getType()) {
                                case "good":
                                    setStyle("-fx-background-color: rgba(71, 209, 71, .7)");
                                    break;
                                case "problem":
                                    setStyle("-fx-background-color: rgba(255, 51, 51, .7)");
                                    break;
                                case "ViP":
                                    setStyle("-fx-background-color: rgba(255, 219, 25 .7)");
                                    break;
                                default:  
                                    break;
                            }


                        } else {
                            setText(null);
                        }
                    }

                    private String getString() {
                        return getItem() == null ? "" : getItem().toString();
                    }
                };

                return cell;
            }
        };
        colType.setCellFactory(cellFactory);

工作得很好,看起来好多了,但是谢谢它让我更接近解决方案。