下拉单选列表(ChoiceBox)
基本应用
ChoiceBox<String> choiceBox = new ChoiceBox<>();
choiceBox.getItems().addAll("str1","str2","str3");
//也可以直接设置list,在show以后也可以动态修改
//ObservableList<String > list = FXCollections.observableArrayList();
//list.addAll("大名","小刘");
//choiceBox.setItems(list);
choiceBox.getSelectionModel().select(2);//默认选择第3个,可不设置
AnchorPane an = new AnchorPane();
an.getChildren().addAll(choiceBox);
primaryStage.setScene(new Scene(an));
primaryStage.setWidth(300);
primaryStage.setHeight(300);
primaryStage.show();
choiceBox.show();//不用点击就显示下拉选项(如果不调用该方法,则需要点击才显示下拉选择,不调用才比较正常)
//监听选中的选项
choiceBox.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
}
});
如果Item不是String类型的处理
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.util.StringConverter;
public class TestApp extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Student s1 = new Student("小明",12);
Student s2 = new Student("大明",20);
ChoiceBox<Student> choiceBox = new ChoiceBox<>();
choiceBox.getItems().addAll(s1,s2);
choiceBox.setConverter(new StringConverter<Student>() {
@Override
public String toString(Student object) {
String str = object.name + object.year;
//这样选项就会显示为str,而不是object内存地址
return str;
}
@Override
public Student fromString(String string) {
//不用重写
return null;
}
});
AnchorPane an = new AnchorPane();
an.getChildren().addAll(choiceBox);
primaryStage.setScene(new Scene(an));
primaryStage.setWidth(300);
primaryStage.setHeight(300);
primaryStage.show();
}
class Student{
String name;
int year;
public Student(String name, int year) {
this.name = name;
this.year = year;
}
}
public static void main(String[] args) {
launch();
}
}
数据源排序
@Override
public void start(Stage primaryStage) throws Exception {
ChoiceBox<String> choiceBox = new ChoiceBox<>();
ObservableList<String > list = FXCollections.observableArrayList();
list.addAll("大名子","小刘","西瓜籽儿");
choiceBox.setItems(list);
//没有限制在show之前才能调用,在任何时候都可以进行排序,会直接体现在UI上
list.sort(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o1.length() - o2.length();
}
});
AnchorPane an = new AnchorPane();
an.getChildren().addAll(choiceBox);
primaryStage.setScene(new Scene(an));
primaryStage.setWidth(300);
primaryStage.setHeight(300);
primaryStage.show();
}
单选项(RadioButton
@Override
public void start(Stage primaryStage) throws Exception {
RadioButton r1 = new RadioButton("r1");
RadioButton r2 = new RadioButton("r2");
RadioButton r3 = new RadioButton("r3");
RadioButton r4 = new RadioButton("r4");
//必须要设置这个,否则无法单选
ToggleGroup group = new ToggleGroup();
group.getToggles().addAll(r1,r2,r3,r4);
// r1.setToggleGroup(group);//作用一致
// r2.setToggleGroup(group);
// r3.setToggleGroup(group);
// r4.setToggleGroup(group);
group.selectToggle(r3);//默认选中r3
group.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
@Override
public void changed(ObservableValue<? extends Toggle> observable, Toggle oldValue, Toggle newValue) {
RadioButton btn = (RadioButton) newValue;
System.out.println(btn.getText());
}
});
HBox box = new HBox();
box.getChildren().addAll(r1,r2,r3,r4);
AnchorPane root = new AnchorPane();
root.getChildren().addAll(box);
primaryStage.setScene(new Scene(root));
primaryStage.setHeight(400);
primaryStage.setWidth(600);
primaryStage.show();
}