我想基于某些条件更改tableview的颜色,我想出了如何做到这一点,表是根据需要改变颜色但是我得到的唯一问题是表的值没有显示,我试过to system.out.println它返回null的单元格的值,我想知道为什么??
这是java代码
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package gis_map;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.AnchorPane;
import javafx.collections.FXCollections;
import javafx.scene.control.TableCell;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.paint.Color;
/**
* FXML Controller class
*
* @author HP
*/
public class CitizenScheduledController implements Initializable {
@FXML
private AnchorPane root;
@FXML
public TableView<citizen> scheduledCitizenTable;
@FXML
private Button approveBtn;
@FXML
private TableColumn<citizen, Integer> idColumn;
@FXML
private TableColumn<citizen, String> nameColumn;
@FXML
private TableColumn<citizen, String> addressColumn;
@FXML
private TableColumn<citizen, String> arrivalColumn;
@FXML
private TableColumn<citizen, String> departureColumn;
@FXML
private TableColumn<citizen, String> vehicleId;
@FXML
private TableColumn<citizen, Integer> waste;
@FXML
private TableColumn<citizen, String> remarks;
VehicleRoutingProblem vpr = new VehicleRoutingProblem();
public static ObservableList<citizen> List = FXCollections.observableArrayList(
// new citizen(1,"testing","testing","testing","testing")
);
public ObservableList<citizen> getList() {
return List;
}
public void setList(ObservableList<citizen> List) {
this.List = List;
}
@Override
public void initialize(URL url, ResourceBundle rb) {
//System.out.println("List contains"+List.size());
try{
vehicleId.setCellValueFactory(new PropertyValueFactory<citizen,String>("vehicleid"));
idColumn.setCellValueFactory(new PropertyValueFactory<citizen, Integer>("citizenId"));
nameColumn.setCellValueFactory(new PropertyValueFactory<citizen, String>("citizenName"));
addressColumn.setCellValueFactory(new PropertyValueFactory<citizen, String>("citizenAdress"));
arrivalColumn.setCellValueFactory(new PropertyValueFactory<citizen, String>("arrivalTime"));
departureColumn.setCellValueFactory(new PropertyValueFactory<citizen, String>("Departure"));
waste.setCellValueFactory(new PropertyValueFactory<citizen,Integer>("waste"));
remarks.setCellValueFactory(new PropertyValueFactory<citizen, String>("remarks"));
//actionColumn.setCellValueFactory(new PropertyValueFactory<>("citizenId"));;
vehicleId.setCellFactory(column -> {
return new TableCell<citizen, String>() {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setText(getText());
setStyle("");
} else {
if (item.equals("vehicle1")) {
setText(getText());
setStyle("-fx-background-color: yellow");
System.out.println("getting the text for vehicle 1"+getText());
} else {
setText(getText());
setStyle("-fx-background-color: tomato");
}
}
}
};
});
scheduledCitizenTable.setItems(List);}catch(NullPointerException e){};
}
}
班级公民
package gis_map;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
public class citizen {
private SimpleIntegerProperty citizenId;
private SimpleStringProperty citizenName;
private SimpleStringProperty citizenAdress;
private SimpleStringProperty arrivalTime;
private SimpleStringProperty Departure;
private SimpleStringProperty vehicleid;
private SimpleIntegerProperty waste;
private SimpleStringProperty remarks;
public citizen(String vehicleid,int citizenId, String citizenName, String citizenAdress, String arrivalTime, String Departure,int waste,String remarks) {
this.citizenId = new SimpleIntegerProperty (citizenId);
this.waste = new SimpleIntegerProperty (waste);
this.vehicleid = new SimpleStringProperty (vehicleid);
this.citizenName = new SimpleStringProperty(citizenName);
this.citizenAdress =new SimpleStringProperty (citizenAdress);
this.arrivalTime = new SimpleStringProperty(arrivalTime);
this.Departure = new SimpleStringProperty (Departure);
this.remarks = new SimpleStringProperty (remarks);
}
public int getCitizenId() {
return citizenId.get();
}
public String getVehicleid() {
return vehicleid.get();
}
public String getCitizenName() {
return citizenName.get();
}
public String getCitizenAdress() {
return citizenAdress.get();
}
public String getArrivalTime() {
return arrivalTime.get();
}
public String getDeparture() {
return Departure.get();
}
public Integer getWaste() {
return waste.get();
}
public String getRemarks() {
return remarks.get();
}
}