Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8116680

ChoiceBox: setConverter does not work

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P3
    • 7u6
    • fx2.1
    • javafx
    • b16

    Description

      Values are not converted.

      import javafx.application.Application;
      import javafx.event.ActionEvent;
      import javafx.event.EventHandler;
      import javafx.scene.Scene;
      import javafx.scene.control.Button;
      import javafx.scene.control.ChoiceBox;
      import javafx.scene.layout.VBox;
      import javafx.stage.Stage;
      import javafx.util.StringConverter;

      public class Main extends Application {

          VBox root;

          @Override
          public void start(Stage stage) {
              stage.setTitle(this.getClass().getSimpleName());
              stage.setScene(new MainScene());
              stage.show();
          }

          public static void main(String[] args) {
              launch(Main.class, args);
          }

          public final class MainScene extends Scene {
              ChoiceBox cb;

              public MainScene() {
                  super(root = new VBox(10), 300, 300);

                  cb = new ChoiceBox();

                  cb.setConverter(new StringConverter() {
                              @Override
                              public String toString(Object t) {
                                  return "Converted " + t.toString();
                              }
                              @Override
                              public Object fromString(String string) {
                                  return Integer.valueOf(string);
                              }
                          });


                  cb.getItems().clear();
                  for (int i = 0; i < 10; i++) {
                      cb.getItems().add(i);
                  }

                  Button setConverter = new Button("Set converter");
                  setConverter.setOnAction(new EventHandler<ActionEvent>() {
                      public void handle(ActionEvent t) {
                          cb.setConverter(new StringConverter() {
                              @Override
                              public String toString(Object t) {
                                  return "Converted " + t.toString();
                              }
                              @Override
                              public Object fromString(String string) {
                                  return Integer.valueOf(string);
                              }
                          });
                      }
                  });

                  root.getChildren().add(setConverter);
                  root.getChildren().add(cb);
              }

              protected void reset() {
                  cb.getItems().clear();
                  for (int i = 0; i < 10; i++) {
                      cb.getItems().add(i);
                  }
              }
          }
      }

      Attachments

        Activity

          People

            psomashe Parvathi Somashekar (Inactive)
            ogb Oleg Barbashov (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              Imported: