我有一门课,比如:
public class myClass{
int age;
String name;
public String toString(){
return name;
};
}
public static ObservableList<myClass> myClassList;
我想知道是否有可能
ChoiceBox<myClass> choiceChart = new ChoiceBox<>(myClassList);
谢谢
附言:我希望有类似的情况
使用JavaFX 2渲染POJO
但是使用选择盒
编辑:
这是我的情况:我有一个 tableView,在其中的一列中,我必须使用 toString() 方法从 myClass 类型的对象设置一个字符串。
我已经尝试使用这些方法(其中myClass--
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列表的字符串
更新:我仍在努力解决这个问题,任何帮助或建议都非常感谢。
您可以指定要在 myClass 实例和 ChoiceBox 中显示的值之间进行转换的 StringConverter。
这是通过 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
}
}
请看:如何用数据库中的数据填充JavaFX ChoiceBox?
和
https://gist.github.com/jewelsea/1422104