1
votes

I have to use a combobox to associate a list of values (key, Value). Key is the value to be stored in the database, Value is the description to be displayed in the combobox.

For example:

Key / value  
C1    Code one  
C2    Choice two  
C3    Choice three  
...

Retrieving, for example, the selected value 'Choice two' I would like to receive the code C2.

For storing the elements in items I defined the ComboVal class.

Defining my combobox, I am stuck on the definition of the setConverter function. The compiler gives me the following error:

Error: (1093, 49) java: constructor FormatStringConverter in class javafx.util.converter.FormatStringConverter cannot be applied to given types; required: java.text.Format; found: no arguments

reason: actual and formal argument lists differ in length

Code:

class ComboVal {

        String[] dato = {null, null};

        ComboVal (String Key, String Value)
        {
            setValue(Key, Value);
        }

        ComboVal ()
        {
            setValue(null, null);
        }

        String getValue ()
        {
            return dato[1];
        }

        String getKey ()
        {
            return dato[0];
        }

        void setValue (String Key, String Value)
        {
            dato[0] = Key;
            dato[1] = Value;
        }
}

classe myclass {
....

/*
   Parameter ctrl is a List containing information for dynamic creation of combobox
*/
void mothod (List ctrl)
{
   VBox box = new VBox();

   box.getChildren().add(new Label(ctrl. label));

   ObservableList items = FXCollections.observableArrayList();

   ComboBox<ComboVal> cb = new ComboBox<>();
   cb.setId(ctrl.name);
   cb.setItems(items);

   //----->>> compiler report error in the next row <<<<<<<<
   cb.setConverter(new FormatStringConverter<ComboVal>() {
     @Override
     public String toString (ComboVal object)
     {
        return (object.getValue());
     }

     @Override
     public ComboVal fromString (String string)
     {
        return null;
     }
   });

   ctrl.options.forEach((k, v) -> {items.add(new ComboVal(k, v));});

   cb.setCellFactory(new Callback<ListView<ComboVal>, ListCell<ComboVal>>() {
      @Override
      public ListCell<ComboVal> call (ListView<ComboVal> p)
      {
         return new ListCell<ComboVal>() {
         @Override
         protected void updateItem (ComboVal item, boolean empty)
         {
            super.updateItem(item, empty);
            if (item == null || empty)
            {
               setGraphic(null);
            }
            else
            {
               setGraphic(new Text(item.getValue()));
            }
         }
      };
    }});

   box.getChildren().add(cb);
}
1

1 Answers

0
votes

The FormatStringConverter class needs to be constructed with a Format parameter. You however constructed it with no parameters.

Supply a Format, like this for example:

Format format = new MessageFormat("Bla bla");

cb.setConverter(new FormatStringConverter<ComboVal>(format);

The FormatStringConverter defines its own toString and fromString methods already, and will use the given format to parse and display the values. I doubt this is what you want as this is pretty limited.

So I think you'd be better of just using a regular StringConverter and provide the custom implementations for toString and fromString.