JavaFx 2 ChoiceBox with custom item

狂风中的少年 提交于 2019-12-11 10:44:31

问题


I have a class such as:

public class myClass{

int age;
String name;

public String toString(){
return name;

};
}

public static ObservableList<myClass> myClassList;

I wonder if is it possible to have

ChoiceBox<myClass> choiceChart = new ChoiceBox<>(myClassList);

Thanks

PS I would like to have a similar situation as

Rendering a POJO with JavaFX 2

but using a ChoiceBox

Edit:

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.

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;
      }
}  

and

  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;
      }
  });

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.


回答1:


You can specify a StringConverter to convert between your myClass instances and the values displayed in the ChoiceBox.

This is done with the setConverter() method.

For example:

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
  }
}



回答2:


Try to see: How do I populate a JavaFX ChoiceBox with data from the Database?

and

https://gist.github.com/jewelsea/1422104



来源:https://stackoverflow.com/questions/19691533/javafx-2-choicebox-with-custom-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!