下拉单选列表(ChoiceBox)

基本应用

  1. ChoiceBox<String> choiceBox = new ChoiceBox<>();
  2. choiceBox.getItems().addAll("str1","str2","str3");
  3. //也可以直接设置list,在show以后也可以动态修改
  4. //ObservableList<String > list = FXCollections.observableArrayList();
  5. //list.addAll("大名","小刘");
  6. //choiceBox.setItems(list);
  7. choiceBox.getSelectionModel().select(2);//默认选择第3个,可不设置
  8. AnchorPane an = new AnchorPane();
  9. an.getChildren().addAll(choiceBox);
  10. primaryStage.setScene(new Scene(an));
  11. primaryStage.setWidth(300);
  12. primaryStage.setHeight(300);
  13. primaryStage.show();
  14. choiceBox.show();//不用点击就显示下拉选项(如果不调用该方法,则需要点击才显示下拉选择,不调用才比较正常)
  15. //监听选中的选项
  16. choiceBox.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
  17. @Override
  18. public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
  19. }
  20. });

image.png

如果Item不是String类型的处理

  1. import javafx.application.Application;
  2. import javafx.beans.value.ChangeListener;
  3. import javafx.beans.value.ObservableValue;
  4. import javafx.scene.Group;
  5. import javafx.scene.Scene;
  6. import javafx.scene.control.ChoiceBox;
  7. import javafx.scene.layout.AnchorPane;
  8. import javafx.stage.Stage;
  9. import javafx.util.StringConverter;
  10. public class TestApp extends Application {
  11. @Override
  12. public void start(Stage primaryStage) throws Exception {
  13. Student s1 = new Student("小明",12);
  14. Student s2 = new Student("大明",20);
  15. ChoiceBox<Student> choiceBox = new ChoiceBox<>();
  16. choiceBox.getItems().addAll(s1,s2);
  17. choiceBox.setConverter(new StringConverter<Student>() {
  18. @Override
  19. public String toString(Student object) {
  20. String str = object.name + object.year;
  21. //这样选项就会显示为str,而不是object内存地址
  22. return str;
  23. }
  24. @Override
  25. public Student fromString(String string) {
  26. //不用重写
  27. return null;
  28. }
  29. });
  30. AnchorPane an = new AnchorPane();
  31. an.getChildren().addAll(choiceBox);
  32. primaryStage.setScene(new Scene(an));
  33. primaryStage.setWidth(300);
  34. primaryStage.setHeight(300);
  35. primaryStage.show();
  36. }
  37. class Student{
  38. String name;
  39. int year;
  40. public Student(String name, int year) {
  41. this.name = name;
  42. this.year = year;
  43. }
  44. }
  45. public static void main(String[] args) {
  46. launch();
  47. }
  48. }

image.png

数据源排序

  1. @Override
  2. public void start(Stage primaryStage) throws Exception {
  3. ChoiceBox<String> choiceBox = new ChoiceBox<>();
  4. ObservableList<String > list = FXCollections.observableArrayList();
  5. list.addAll("大名子","小刘","西瓜籽儿");
  6. choiceBox.setItems(list);
  7. //没有限制在show之前才能调用,在任何时候都可以进行排序,会直接体现在UI上
  8. list.sort(new Comparator<String>() {
  9. @Override
  10. public int compare(String o1, String o2) {
  11. return o1.length() - o2.length();
  12. }
  13. });
  14. AnchorPane an = new AnchorPane();
  15. an.getChildren().addAll(choiceBox);
  16. primaryStage.setScene(new Scene(an));
  17. primaryStage.setWidth(300);
  18. primaryStage.setHeight(300);
  19. primaryStage.show();
  20. }

image.png

单选项(RadioButton

  1. @Override
  2. public void start(Stage primaryStage) throws Exception {
  3. RadioButton r1 = new RadioButton("r1");
  4. RadioButton r2 = new RadioButton("r2");
  5. RadioButton r3 = new RadioButton("r3");
  6. RadioButton r4 = new RadioButton("r4");
  7. //必须要设置这个,否则无法单选
  8. ToggleGroup group = new ToggleGroup();
  9. group.getToggles().addAll(r1,r2,r3,r4);
  10. // r1.setToggleGroup(group);//作用一致
  11. // r2.setToggleGroup(group);
  12. // r3.setToggleGroup(group);
  13. // r4.setToggleGroup(group);
  14. group.selectToggle(r3);//默认选中r3
  15. group.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
  16. @Override
  17. public void changed(ObservableValue<? extends Toggle> observable, Toggle oldValue, Toggle newValue) {
  18. RadioButton btn = (RadioButton) newValue;
  19. System.out.println(btn.getText());
  20. }
  21. });
  22. HBox box = new HBox();
  23. box.getChildren().addAll(r1,r2,r3,r4);
  24. AnchorPane root = new AnchorPane();
  25. root.getChildren().addAll(box);
  26. primaryStage.setScene(new Scene(root));
  27. primaryStage.setHeight(400);
  28. primaryStage.setWidth(600);
  29. primaryStage.show();
  30. }

image.png