您可以清除选择框的选择,然后将不会在其中选择任何内容。
favBox.getSelectionModel().selectedItemProperty().addListener(
(observable, oldValue, newValue) -> {
if (newValue != null) {
browser.load(newValue);
favBox.getSelectionModel().clearSelection();
}
}
);
请注意,这种行为有点奇怪,因为大多数时候您可能希望所选选项在选择后继续显示。但是,如果您不想要标准操作并希望在选择后立即清除选择,您可以随时使用此处提供的示例代码。
示例应用程序:
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();
}
}