了JavaFx 2 ChoiceBox与自定义项目

问题描述:

我有一个类,如:了JavaFx 2 ChoiceBox与自定义项目

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凡在其列之一,我必须从MyClass的类型的对象设置一个字符串,通过toString () 方法。

我曾尝试使用这些方法(其中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; 
     } 
} 

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列表中显示的字符串

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

您可以指定一个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 
    } 
} 
+0

我已经编辑我的问题有更详细的代码,我仍然有问题,即使您的回复后, –