1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
| package combotest;
import javafx.application.Application;
import javafx.beans.InvalidationListener;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.SplitPane;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
private static final class ComBoxItemPerso {
private final String value;
private final Image icon;
public ComBoxItemPerso(final String value, final String iconUrl) {
this.value = value;
this.icon = new Image(iconUrl);
}
public String getValue() {
return value;
}
public Image getIcon() {
return icon;
}
}
private static final class ComBoxItemPersoListCell extends ListCell<ComBoxItemPerso> {
private final ComboBox<ComBoxItemPerso> parent;
private final Label rightLabel = new Label();
private final ImageView leftImage = new ImageView();
private final SplitPane splitPane = new SplitPane(leftImage, rightLabel);
public ComBoxItemPersoListCell(final ComboBox<ComBoxItemPerso> parent) {
super();
// Oblige de rajouter tout ce qui suit a cause du splitpane.
this.parent = parent;
leftImage.setOnMousePressed(this::selectInCombo);
// rightLabel.setStyle("-fx-border-color: green;");
rightLabel.setMaxWidth(Double.MAX_VALUE);
rightLabel.setOnMousePressed(this::selectInCombo);
}
private ComBoxItemPerso lastItem;
@Override
protected void updateItem(final ComBoxItemPerso item, final boolean empty) {
super.updateItem(item, empty);
lastItem = item;
Node graphic = null;
leftImage.setImage(null);
rightLabel.setText(null);
if (!empty && item != null) {
leftImage.setImage(item.getIcon());
rightLabel.setText(item.getValue());
graphic = splitPane;
}
setGraphic(graphic);
setText(null);
}
private void selectInCombo(final MouseEvent mouseEvent) {
if (parent != null) {
parent.getSelectionModel().select(lastItem);
}
}
}
private final TextField detailsTextField = new TextField();
private final ComboBox<ComBoxItemPerso> myComboBox = new ComboBox<>();
@Override
public void start(Stage primaryStage) {
detailsTextField.setEditable(false);
//comboBox
myComboBox.setPrefWidth(176.5);
final ObservableList<ComBoxItemPerso> options = FXCollections.observableArrayList(new ComBoxItemPerso("1", getClass().getResource("1.png").toExternalForm()),
new ComBoxItemPerso("2", getClass().getResource("2.png").toExternalForm()),
new ComBoxItemPerso("3", getClass().getResource("3.png").toExternalForm()));
myComboBox.setItems(options);
myComboBox.setButtonCell(new ComBoxItemPersoListCell(null));
myComboBox.setCellFactory(listView -> new ComBoxItemPersoListCell(myComboBox));
myComboBox.valueProperty().addListener(valueInvalidationListener);
final StackPane center = new StackPane();
center.getChildren().add(myComboBox);
final BorderPane root = new BorderPane(center, detailsTextField, null, null, null);
final Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Test");
primaryStage.setScene(scene);
primaryStage.show();
}
private final InvalidationListener valueInvalidationListener = observable -> {
final ComBoxItemPerso item = myComboBox.getValue();
final String text = (item == null) ? null : item.getValue();
detailsTextField.setText(text);
};
public static void main(String[] args) {
launch(args);
}
} |
Partager