问题描述
我有一个课程,例如:
public class myClass{
int age;
String name;
public String toString(){
return name;
};
}
public static ObservableList<myClass> myClassList;
我想知道是否有可能
ChoiceBox<myClass> choiceChart = new ChoiceBox<>(myClassList);
谢谢
PS,我希望有类似的情况
PS I would like to have a similar situation as
但使用ChoiceBox
but using a ChoiceBox
这是我的情况:我有一个tableView,在其中它必须使用toString()方法在myClass类型的对象中的一列中设置一个String.
this is my situation: I have a tableView where in one of its columns I have to set a String from an object of myClass type, using the toString() method.
我尝试使用这些方法(其中myClass-> CustomInternalWindow类)
I have tried to use these methods (where myClass --> CustomInternalWindow class )
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;
}
});
但是我无法显示Windows绘图列表中的字符串
but I am not able to display strings from windowsPlotted list
更新:我仍在努力解决这个问题,非常感谢您的帮助或建议.
Update: I am still struggling with this issue, any help or suggestion really appreciated.
推荐答案
您可以指定 StringConverter 可以在myClass实例和ChoiceBox中显示的值之间进行转换.
You can specify a StringConverter to convert between your myClass instances and the values displayed in the 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
}
}
这篇关于JavaFx 2 ChoiceBox与自定义项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!