带有自定义项的JavaFx 2 ChoiceBox

Alb*_*sut 1 javafx-2 observablelist

我有一个班级,如:

public class myClass{

int age;
String name;

public String toString(){
return name;

};
}

public static ObservableList<myClass> myClassList;
Run Code Online (Sandbox Code Playgroud)

我想知道是否有可能

ChoiceBox<myClass> choiceChart = new ChoiceBox<>(myClassList);
Run Code Online (Sandbox Code Playgroud)

谢谢

PS我想有类似的情况

使用JavaFX 2渲染POJO

但使用ChoiceBox

编辑:

这是我的情况:我有一个tableView,在其中一个列中我必须使用toString()方法从myClass类型的对象设置一个String.

我试过用这些方法(其中myClass - > 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;
      }
}  
Run Code Online (Sandbox Code Playgroud)

  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;
      }
  });
Run Code Online (Sandbox Code Playgroud)

但是我无法从windowsPlotted列表中显示字符串

更新:我仍在努力解决这个问题,任何帮助或建议真的很感激.

Aar*_*ron 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
  }
}
Run Code Online (Sandbox Code Playgroud)