-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountrySelector.java
More file actions
81 lines (70 loc) · 3.03 KB
/
CountrySelector.java
File metadata and controls
81 lines (70 loc) · 3.03 KB
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
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.ScrollBar;
import javafx.scene.control.Slider;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class CountrySelector extends Application {
private ObservableList<String> countries = FXCollections.observableArrayList(
"USA", "Canada", "UK", "Australia", "India", "China", "Russia", "Brazil", "South Africa", "Germany"
);
@Override
public void start(Stage primaryStage) {
VBox root = new VBox(10);
root.setPadding(new Insets(10));
Label lblSelectionMode = new Label("Selection Mode:");
ComboBox<String> cbSelectionMode = new ComboBox<>();
cbSelectionMode.getItems().addAll("Single", "Multiple");
cbSelectionMode.setValue("Single");
ListView<String> listView = new ListView<>(countries);
listView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
System.out.println("Selected: " + newValue);
}
});
ScrollBar scrollBar = new ScrollBar();
scrollBar.setMin(0);
scrollBar.setMax(countries.size() - 1);
scrollBar.valueProperty().addListener((obs, oldVal, newVal) -> {
listView.scrollTo(newVal.intValue());
});
Slider slider = new Slider();
slider.setMin(0);
slider.setMax(countries.size() - 1);
slider.setSnapToTicks(true);
slider.setMajorTickUnit(1);
slider.setMinorTickCount(0);
slider.setShowTickLabels(true);
slider.setShowTickMarks(true);
slider.valueProperty().addListener((obs, oldVal, newVal) -> {
listView.scrollTo(newVal.intValue());
});
cbSelectionMode.valueProperty().addListener((obs, oldVal, newVal) -> {
if ("Single".equals(newVal)) {
listView.getSelectionModel().setSelectionMode(javafx.scene.control.SelectionMode.SINGLE);
} else if ("Multiple".equals(newVal)) {
listView.getSelectionModel().setSelectionMode(javafx.scene.control.SelectionMode.MULTIPLE);
}
});
HBox hbox = new HBox(10, lblSelectionMode, cbSelectionMode);
VBox vboxControls = new VBox(10, hbox, listView, scrollBar, slider);
root.getChildren().add(vboxControls);
Scene scene = new Scene(root, 300, 400);
primaryStage.setScene(scene);
primaryStage.setTitle("Country Selector");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}