我有一个包含多个列的TableView,一个是ComboBox列,另外五个是CheckBox列。当我尝试用popullate表时,一些CheckBox单元格应该有刻度标记,而带有ComboBox的列应该只显示ObservableList的文本(在这种情况下只有一个字母),但是它显示了内存中的值似乎是什么存储。我添加了它看起来像什么的代码和图像以及我看起来像它的样子。
private final ObservableList<Treatment> data = FXCollections.observableArrayList(
new Treatment("A", true,true,true,true, false),
new Treatment("B", true,true,true,true, false),
new Treatment("C", true,true,true,true, false),
new Treatment("D", true,true,true,true, false),
new Treatment("E", true,true,true,true, false)
);
现在我创建我的列并将CellFactory设置为ComboBox或CheckBox。
instrumental.setCellValueFactory(new PropertyValueFactory<Treatment,String>("treatment"));
instrumental.setCellFactory(ComboBoxTableCell.forTableColumn("A","B", "C", "D"));
instrumental.setOnEditCommit(
new EventHandler<CellEditEvent<Treatment, String>>() {
@Override
public void handle(CellEditEvent<Treatment, String> t) {
((Treatment) t.getTableView().getItems().get(
t.getTablePosition().getRow())
).setTratamiento(t.getNewValue());
}
}
);
c1.setCellValueFactory(new PropertyValueFactory<Treatment,Boolean>("c1"));
c1.setCellFactory(column -> new CheckBoxTableCell());
c2.setCellValueFactory(new PropertyValueFactory<Treatment,Boolean>("c2"));
c2.setCellFactory(column -> new CheckBoxTableCell());
c3.setCellValueFactory(new PropertyValueFactory<Treatment,Boolean>("c3"));
c3.setCellFactory(column -> new CheckBoxTableCell());
c4.setCellFactory(column -> new CheckBoxTableCell());
c4.setCellValueFactory(new PropertyValueFactory<Treatment,Boolean>("c4"));
c5.setCellValueFactory(new PropertyValueFactory<Treatment,Boolean>("c5"));
c5.setCellFactory(column -> new CheckBoxTableCell());
这是我将可观察列表添加到表
的位置table.setItems(data);
这是治疗类
public class Treatment {
public SimpleStringProperty Treatment ;
public BooleanProperty c1;
public SimpleBooleanProperty c2;
public SimpleBooleanProperty c3;
public SimpleBooleanProperty c4;
public SimpleBooleanProperty c5;
public Treatment(String treatmentc, boolean c1c, boolean c2c, boolean c3c, boolean c4c, boolean c5c) {
treatment= new SimpleStringProperty (treatmentc);
c1 = new SimpleBooleanProperty (c1c);
c2 = new SimpleBooleanProperty (c2c);
c3 = new SimpleBooleanProperty (c3c);
c4 = new SimpleBooleanProperty (c4c);
c4 = new SimpleBooleanProperty (c5c);
}
public StringProperty getTreatment() {
return treatment;
}
public void setTreatment(String treatment) {
this.treatment.set(treatmentc);
}
public BooleanProperty getC1() {
return c1;
}
public void setC1(Boolean c1) {
this.c1.set(c1);
}
public BooleanProperty getC2() {
return c2;
}
public void setC2(SimpleBooleanProperty c2) {
this.c2 = c2;
}
public BooleanProperty getC3() {
return c3;
}
public void setC3(SimpleBooleanProperty c3) {
this.c3 = c3;
}
public BooleanProperty getC4() {
return c4;
}
public void setC4(SimpleBooleanProperty c4) {
this.c4 = c4;
}
public BooleanProperty getC5() {
return c5;
}
public void setC5(SimpleBooleanProperty c5) {
this.c5 = c5;
}
}
This is an image of what it looks like originally
这不重复。问题不在于获取值,而在于填充TableView。如果我不使用CellFactory,那么该表会使用ObservableList的String值(&#34; true&#34;,&#34; false&#34;等...)