Vous pouvez effacer la sélection d'un ChoiceBox, alors rien ne sera sélectionné en elle.
favBox.getSelectionModel().selectedItemProperty().addListener(
(observable, oldValue, newValue) -> {
if (newValue != null) {
browser.load(newValue);
favBox.getSelectionModel().clearSelection();
}
}
);
Notez que ce comportement est un peu étrange que la plupart du temps que vous voulez sans doute le choix sélectionné pour continuer à afficher après la sélection. Cependant, si vous ne voulez pas l'opération standard et que vous voulez effacer immédiatement le choix après la sélection, vous pouvez toujours utiliser l'exemple de code fourni ici.
exemple d'application:
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.web.*;
import javafx.stage.Stage;
import static javafx.collections.FXCollections.observableArrayList;
public class HiddenChoices extends Application {
@Override
public void start(Stage stage) throws Exception {
WebView webView = new WebView();
WebEngine browser = webView.getEngine();
VBox.setVgrow(webView, Priority.ALWAYS);
ChoiceBox<String> favBox = new ChoiceBox<>(
observableArrayList(
"http://www.google.com",
"http://andrew-hoyer.com/experiments/cloth/",
"http://www.effectgames.com/demos/canvascycle/",
"http://www.zynaps.com/site/experiments/environment.html?mesh=bart.wft"
)
);
favBox.getSelectionModel().selectedItemProperty().addListener(
(observable, oldValue, newValue) -> {
if (newValue != null) {
browser.load(newValue);
favBox.getSelectionModel().clearSelection();
}
}
);
ProgressBar progress = new ProgressBar();
progress.progressProperty().bind(browser.getLoadWorker().progressProperty());
progress.visibleProperty().bind(browser.getLoadWorker().runningProperty());
HBox controls = new HBox(10, favBox, progress);
controls.setMinHeight(HBox.USE_PREF_SIZE);
controls.setAlignment(Pos.CENTER_LEFT);
stage.setScene(
new Scene(
new VBox(10, controls, webView)
)
);
stage.show();
favBox.getSelectionModel().select(0);
}
public static void main(String[] args) {
Application.launch();
}
}
Cela fonctionne parfaitement !! Merci beaucoup pour votre aide!!!!!!! – user3747849