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