下面列出了怎么用javafx.scene.control.SingleSelectionModel的API类实例代码及写法,或者点击链接到github查看源代码。
private void initLanguageChoicebox() {
languageChoicebox.getItems().addAll(getSupportedLanguages().sorted().collect(Collectors.toList()));
languageChoicebox.setConverter(DesignerUtil.stringConverter(Language::getName, AuxLanguageRegistry::findLanguageByNameOrDefault));
SingleSelectionModel<Language> langSelector = languageChoicebox.getSelectionModel();
@NonNull Language restored = globalLanguage.getOrElse(defaultLanguage());
globalLanguage.bind(langSelector.selectedItemProperty());
langSelector.select(restored);
Platform.runLater(() -> {
langSelector.clearSelection();
langSelector.select(restored); // trigger listener
});
}
/**
* Update a list of available path sizes.
*/
@FxThread
private void updatePathSizeValues() {
final ComboBox<Integer> pathSizeComboBox = getPatchSizeComboBox();
final SingleSelectionModel<Integer> selectionModel = pathSizeComboBox.getSelectionModel();
final Integer current = selectionModel.getSelectedItem();
final ObservableList<Integer> items = pathSizeComboBox.getItems();
items.clear();
final ComboBox<Integer> totalSizeComboBox = getTotalSizeComboBox();
final Integer naxValue = totalSizeComboBox.getSelectionModel().getSelectedItem();
for (final Integer value : PATCH_SIZE_VARIANTS) {
if (value >= naxValue) break;
items.add(value);
}
if (items.contains(current)) {
selectionModel.select(current);
} else {
selectionModel.select(items.get(items.size() - 1));
}
}
/**
* Here is the matching tabs set to the right index
* The index of the tabs are as following:
* 0: automatic matching tab
* 1: manual matching tab
* 2: potential matching tab
*
* @param tab index of the wanted matching tab
* @void - No direct output
*/
public void setMatchingTab(int tab){
//check the index (tab) given
if (tab > 3 || tab < 0){
tab = AUTO_MATCHING_TAB_INDEX; //set tab to automatich matching
}
//get selection of matching tabs
SingleSelectionModel<Tab> matchingSelectionTabs = matchingTabs.getSelectionModel();
//set the right tab
matchingSelectionTabs.select(tab);
}
@Override
protected void mouseClicked(MouseEvent me) {
TabPane tp = (TabPane) node;
SingleSelectionModel<Tab> selectionModel = tp.getSelectionModel();
Tab selectedTab = selectionModel.getSelectedItem();
if (selectedTab != null && prevSelection != selectionModel.getSelectedIndex()) {
recorder.recordSelect(this, getTextForTab(tp, selectedTab));
}
prevSelection = selectionModel.getSelectedIndex();
}
@Override
@FxThread
protected void createContent(@NotNull final GridPane root) {
super.createContent(root);
final Label algorithmTypeLabel = new Label(Messages.GENERATE_TANGENTS_DIALOG_ALGORITHM_LABEL + ":");
algorithmTypeLabel.prefWidthProperty().bind(root.widthProperty().multiply(DEFAULT_LABEL_W_PERCENT3));
algorithmTypeComboBox = new ComboBox<>(GenerateTangentsDialog.ALGORITHM_TYPES);
algorithmTypeComboBox.prefWidthProperty().bind(root.widthProperty().multiply(DEFAULT_FIELD_W_PERCENT3));
final SingleSelectionModel<AlgorithmType> selectionModel = algorithmTypeComboBox.getSelectionModel();
selectionModel.select(AlgorithmType.MIKKTSPACE);
final Label splitMirroredLabel = new Label(Messages.GENERATE_TANGENTS_DIALOG_SPLIT_MIRRORED + ":");
splitMirroredLabel.prefWidthProperty().bind(root.widthProperty().multiply(DEFAULT_LABEL_W_PERCENT3));
splitMirroredCheckBox = new CheckBox();
splitMirroredCheckBox.disableProperty().bind(selectionModel.selectedItemProperty().isNotEqualTo(AlgorithmType.STANDARD));
splitMirroredCheckBox.prefWidthProperty().bind(root.widthProperty().multiply(DEFAULT_FIELD_W_PERCENT3));
root.add(algorithmTypeLabel, 0, 0);
root.add(algorithmTypeComboBox, 1, 0);
root.add(splitMirroredLabel, 0, 1);
root.add(splitMirroredCheckBox, 1, 1);
FXUtils.addClassTo(algorithmTypeLabel, splitMirroredLabel, CssClasses.DIALOG_DYNAMIC_LABEL);
FXUtils.addClassTo(algorithmTypeComboBox, splitMirroredCheckBox, CssClasses.DIALOG_FIELD);
}
@SuppressWarnings("unchecked")
private SingleSelectionModel<String> createSelectionModelMock(
final ComboBox<String> comboBox, final int index) {
final SingleSelectionModel<String> mock = mock(SingleSelectionModel.class);
when(comboBox.getSelectionModel()).thenReturn(mock);
when(mock.getSelectedIndex()).thenReturn(index);
return mock;
}
public SingleSelectionModel<Tab> getSelectionModel() {
return tabPane.getSelectionModel();
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Tabs");
JFXTabPane tabPane = new JFXTabPane();
Tab tab = new Tab();
tab.setText(msg);
tab.setContent(new Label(TAB_0));
tabPane.getTabs().add(tab);
tabPane.setPrefSize(300, 200);
Tab tab1 = new Tab();
tab1.setText(TAB_01);
tab1.setContent(new Label(TAB_01));
tabPane.getTabs().add(tab1);
SingleSelectionModel<Tab> selectionModel = tabPane.getSelectionModel();
selectionModel.select(1);
JFXButton button = new JFXButton("New Tab");
button.setOnMouseClicked((o) -> {
Tab temp = new Tab();
int count = tabPane.getTabs().size();
temp.setText(msg + count);
temp.setContent(new Label(TAB_0 + count));
tabPane.getTabs().add(temp);
});
tabPane.setMaxWidth(500);
HBox hbox = new HBox();
hbox.getChildren().addAll(button, tabPane);
hbox.setSpacing(50);
hbox.setAlignment(Pos.CENTER);
hbox.setStyle("-fx-padding:20");
Group root = new Group();
Scene scene = new Scene(root, 700, 250);
root.getChildren().addAll(hbox);
scene.getStylesheets().add(TabsDemo.class.getResource("/css/jfoenix-components.css").toExternalForm());
primaryStage.setTitle("JFX Tabs Demo");
primaryStage.setScene(scene);
primaryStage.show();
}
private void addAndSelectNewTab(ObservableList<Tab> tabs, Tab tab2, SingleSelectionModel<Tab> selectedTab, SingleSelectionModel<Tab> fxSelectedTab
,int selectedTabIndex) {
// TODO Auto-generated method stub
Platform.runLater(new Runnable() {
@Override
public void run() {
for(int a=0; a<tabs.size();a++){
String openTabName = tabs.get(a).getText();
if(openTabName.equals("History") ||
openTabName.equals("Bookmarks") ||
openTabName.equals("Downloads") ||
openTabName.equals("Setting"))
{
// fxSelectedTab.select(selectedTabIndex);
System.out.println("Tab index:"+ selectedTabIndex);
selectedTab.select(a);
return;
}
}
fxSelectedTab.select(selectedTabIndex);
tabs.add(tabs.size() - 1, tab);
selectedTab.select(tab);
getBookMarkView();
}
});
}
public void creatNewTab(TabPane tabpane, Tab addNewTab) {
Tab tab = new Tab("New tab");
try {
tab.setContent(FXMLLoader.load(getClass().getResource(Main.FXMLS+"Tab.fxml")));
// tab.setText(TabController.getWebEngine().getTitle());
} catch (IOException e) {
System.out.println("Exception: New tab click but not working in TabPaneView Class");
}
tab.getStyleClass().addAll("tab-pane");
ObservableList<Tab> tabs = tabpane.getTabs();
Platform.runLater(new Runnable() {
@Override
public void run() {
tabs.add(tabs.size() - 1, tab);
SingleSelectionModel<Tab> selectedTab = tabpane.getSelectionModel();
selectedTab.select(tab);
}
});
}