我有一个类如:
public class myClass{
int age;
String name;
public String toString(){
return name;
};
}
public static ObservableList<myClass> myClassList;
我想知道是否有可能
ChoiceBox<myClass> choiceChart = new ChoiceBox<>(myClassList);
由于
PS我希望有类似的情况
Rendering a POJO with JavaFX 2
但使用ChoiceBox
修改
这是我的情况:我有一个tableView,在其中一个列中我必须使用toString()方法从myClass类型的对象设置一个String。
我试过使用这些方法(其中myClass - &gt; CustomInternalWindow类)
public static class Indicators{
private final SimpleStringProperty tool_col;
private final SimpleStringProperty chart_col;
private final SimpleStringProperty pane_col;
private final SimpleBooleanProperty on_col;
private Indicators(String tl, CustomInternalWindow chrt, String pne, Boolean sel){
this.tool_col = new SimpleStringProperty (tl);
if (chrt == null) {
this.chart_col = null;
}
else {
this.chart_col = new SimpleStringProperty (chrt.toString());
}
this.pane_col = new SimpleStringProperty (pne);
this.on_col = new SimpleBooleanProperty (sel);
}
public String getTool(){
return tool_col.get();
}
public void setTool(String tl){
tool_col.set(tl);
}
...
public SimpleBooleanProperty onProperty() {
return on_col;
}
public SimpleStringProperty toolProperty(){
return tool_col;
}
public SimpleStringProperty chartProperty(){
return chart_col;
}
public SimpleStringProperty paneProperty(){
return pane_col;
}
}
和
tablecolumnFrame.setCellFactory(new Callback<TableColumn<Indicators, CustomInternalWindow>, TableCell<Indicators, CustomInternalWindow>>(){
@Override
public TableCell<Indicators, CustomInternalWindow> call(TableColumn<Indicators, CustomInternalWindow> param){
TableCell<Indicators, CustomInternalWindow> cell = new TableCell<Indicators, CustomInternalWindow>(){
@Override
public void updateItem(CustomInternalWindow item, boolean empty) {
super.updateItem(item, empty);
if (item != null) {
ChoiceBox<CustomInternalWindow> choiceChart = new ChoiceBox<>(newprojectx.NewProjectXController.windowsPlotted);
choiceChart.setConverter(new CustomInternaWindowStringConverter());
choiceChart.getSelectionModel().select(item);
choiceChart.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<CustomInternalWindow>() {
@Override
public void changed(
final ObservableValue<? extends CustomInternalWindow> ov, final CustomInternalWindow oldValue, final CustomInternalWindow newValue) {
if (!isEditing()) {
final TableView table = getTableView();
if (table != null) {
table.edit(getTableRow().getIndex(), getTableColumn());
}
}
commitEdit(newValue);
}
});
setGraphic(choiceChart);
}
}
};
return cell;
}
});
但我无法显示来自windowsPlotted列表的字符串
更新:我仍然在努力解决这个问题,任何帮助或建议都非常感激。
答案 0 :(得分:6)
您可以指定StringConverter以在myClass实例和ChoiceBox中显示的值之间进行转换。
这是通过setConverter()方法完成的。
例如:
ChoiceBox<myClass> choiceChart = new ChoiceBox<>();
choiceChart.setConverter(new MyClassConverter());
choiceChart.setItems(myClassList);
class MyClassConverter extends StringConverter<myClass> {
public myClass fromString(String string) {
// convert from a string to a myClass instance
}
public String toString(myClass myClassinstance) {
// convert a myClass instance to the text displayed in the choice box
}
}
答案 1 :(得分:0)