javaFX:带有单选按钮的listview
我有一个列表,其中的项目应带有带有列表项目的javaFX:带有单选按钮的listview,java,javafx,scenebuilder,Java,Javafx,Scenebuilder,我有一个列表,其中的项目应带有带有列表项目的单选按钮 ListView是一个可观察的ArrayList,其中包含我想为列表视图中的每个项目添加单选按钮的数据。创建一个自定义列表,并将列表单元格的图形设置为单选按钮。如果需要,您可以在updateItem()中添加更多功能 输出 完整示例 import javafx.application.Application; import javafx.collections.FXCollections; import javafx.collections
单选按钮
ListView
是一个可观察的ArrayList
,其中包含我想为列表视图中的每个项目添加单选按钮的数据。创建一个自定义列表,并将列表单元格的图形设置为单选按钮。如果需要,您可以在updateItem()中添加更多功能
输出
完整示例
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class RadioButtonListView extends Application {
public static final ObservableList names =
FXCollections.observableArrayList();
private ToggleGroup group = new ToggleGroup();
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("List View Sample");
final ListView listView = new ListView();
listView.setPrefSize(200, 250);
listView.setEditable(true);
names.addAll(
"Adam", "Alex", "Alfred", "Albert",
"Brenda", "Connie", "Derek", "Donny",
"Lynne", "Myrtle", "Rose", "Rudolph",
"Tony", "Trudy", "Williams", "Zach"
);
listView.setItems(names);
listView.setCellFactory(param -> new RadioListCell());
StackPane root = new StackPane();
root.getChildren().add(listView);
primaryStage.setScene(new Scene(root, 200, 250));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
private class RadioListCell extends ListCell<String> {
@Override
public void updateItem(String obj, boolean empty) {
super.updateItem(obj, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
RadioButton radioButton = new RadioButton(obj);
radioButton.setToggleGroup(group);
// Add Listeners if any
setGraphic(radioButton);
}
}
}
}
导入javafx.application.application;
导入javafx.collections.FXCollections;
导入javafx.collections.ObservableList;
导入javafx.scene.scene;
导入javafx.scene.control.ListCell;
导入javafx.scene.control.ListView;
导入javafx.scene.control.RadioButton;
导入javafx.scene.control.ToggleGroup;
导入javafx.scene.layout.StackPane;
导入javafx.stage.stage;
公共类RadioButtonListView扩展了应用程序{
公共静态最终可观察列表名称=
FXCollections.observableArrayList();
private-ToggleGroup-group=new-ToggleGroup();
@凌驾
公共无效开始(阶段primaryStage){
setTitle(“列表视图示例”);
最终ListView ListView=新建ListView();
setPrefSize(200250);
listView.setEditable(true);
name.addAll(
“亚当”、“亚历克斯”、“阿尔弗雷德”、“阿尔伯特”,
“布伦达”,“康妮”,“德里克”,“唐尼”,
“林恩”、“桃金娘”、“玫瑰”、“鲁道夫”,
“托尼”、“特鲁迪”、“威廉姆斯”、“扎克”
);
setItems(名称);
setCellFactory(参数->新建RadioListCell());
StackPane root=新的StackPane();
root.getChildren().add(listView);
原始阶段。设置场景(新场景(根,200250));
primaryStage.show();
}
公共静态void main(字符串[]args){
发射(args);
}
私有类RadioListCell扩展了ListCell{
@凌驾
public void updateItem(字符串obj,布尔空){
super.updateItem(对象,空);
if(空){
setText(空);
设置图形(空);
}否则{
RadioButton RadioButton=新的RadioButton(obj);
radioButton.setToggleGroup(组);
//添加侦听器(如果有)
设置图形(无线按钮);
}
}
}
}
创建自定义列表单元格,并将其图形设置为单选按钮
。如果需要,您可以在updateItem()中添加更多功能
输出
完整示例
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class RadioButtonListView extends Application {
public static final ObservableList names =
FXCollections.observableArrayList();
private ToggleGroup group = new ToggleGroup();
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("List View Sample");
final ListView listView = new ListView();
listView.setPrefSize(200, 250);
listView.setEditable(true);
names.addAll(
"Adam", "Alex", "Alfred", "Albert",
"Brenda", "Connie", "Derek", "Donny",
"Lynne", "Myrtle", "Rose", "Rudolph",
"Tony", "Trudy", "Williams", "Zach"
);
listView.setItems(names);
listView.setCellFactory(param -> new RadioListCell());
StackPane root = new StackPane();
root.getChildren().add(listView);
primaryStage.setScene(new Scene(root, 200, 250));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
private class RadioListCell extends ListCell<String> {
@Override
public void updateItem(String obj, boolean empty) {
super.updateItem(obj, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
RadioButton radioButton = new RadioButton(obj);
radioButton.setToggleGroup(group);
// Add Listeners if any
setGraphic(radioButton);
}
}
}
}
导入javafx.application.application;
导入javafx.collections.FXCollections;
导入javafx.collections.ObservableList;
导入javafx.scene.scene;
导入javafx.scene.control.ListCell;
导入javafx.scene.control.ListView;
导入javafx.scene.control.RadioButton;
导入javafx.scene.control.ToggleGroup;
导入javafx.scene.layout.StackPane;
导入javafx.stage.stage;
公共类RadioButtonListView扩展了应用程序{
公共静态最终可观察列表名称=
FXCollections.observableArrayList();
private-ToggleGroup-group=new-ToggleGroup();
@凌驾
公共无效开始(阶段primaryStage){
setTitle(“列表视图示例”);
最终ListView ListView=新建ListView();
setPrefSize(200250);
listView.setEditable(true);
name.addAll(
“亚当”、“亚历克斯”、“阿尔弗雷德”、“阿尔伯特”,
“布伦达”,“康妮”,“德里克”,“唐尼”,
“林恩”、“桃金娘”、“玫瑰”、“鲁道夫”,
“托尼”、“特鲁迪”、“威廉姆斯”、“扎克”
);
setItems(名称);
setCellFactory(参数->新建RadioListCell());
StackPane root=新的StackPane();
root.getChildren().add(listView);
原始阶段。设置场景(新场景(根,200250));
primaryStage.show();
}
公共静态void main(字符串[]args){
发射(args);
}
私有类RadioListCell扩展了ListCell{
@凌驾
public void updateItem(字符串obj,布尔空){
super.updateItem(对象,空);
if(空){
setText(空);
设置图形(空);
}否则{
RadioButton RadioButton=新的RadioButton(obj);
radioButton.setToggleGroup(组);
//添加侦听器(如果有)
设置图形(无线按钮);
}
}
}
}
创建自定义列表单元格,并将其图形设置为单选按钮
。如果需要,您可以在updateItem()中添加更多功能
输出
完整示例
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class RadioButtonListView extends Application {
public static final ObservableList names =
FXCollections.observableArrayList();
private ToggleGroup group = new ToggleGroup();
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("List View Sample");
final ListView listView = new ListView();
listView.setPrefSize(200, 250);
listView.setEditable(true);
names.addAll(
"Adam", "Alex", "Alfred", "Albert",
"Brenda", "Connie", "Derek", "Donny",
"Lynne", "Myrtle", "Rose", "Rudolph",
"Tony", "Trudy", "Williams", "Zach"
);
listView.setItems(names);
listView.setCellFactory(param -> new RadioListCell());
StackPane root = new StackPane();
root.getChildren().add(listView);
primaryStage.setScene(new Scene(root, 200, 250));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
private class RadioListCell extends ListCell<String> {
@Override
public void updateItem(String obj, boolean empty) {
super.updateItem(obj, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
RadioButton radioButton = new RadioButton(obj);
radioButton.setToggleGroup(group);
// Add Listeners if any
setGraphic(radioButton);
}
}
}
}
导入javafx.application.application;
导入javafx.collections.FXCollections;
导入javafx.collections.ObservableList;
导入javafx.scene.scene;
导入javafx.scene.control.ListCell;
导入javafx.scene.control.ListView;
导入javafx.scene.control.RadioButton;
导入javafx.scene.control.ToggleGroup;
导入javafx.scene.layout.StackPane;
导入javafx.stage.stage;
公共类RadioButtonListView扩展了应用程序{
公共静态最终可观察列表名称=
FXCollections.observableArrayList();
private-ToggleGroup-group=new-ToggleGroup();
@凌驾
公共无效开始(阶段primaryStage){
setTitle(“列表视图示例”);
最终ListView ListView=新建ListView();
setPrefSize(200250);
listView.setEditable(true);
name.addAll(
“亚当”、“亚历克斯”、“阿尔弗雷德”、“阿尔伯特”,
“布伦达”,“康妮”,“德里克”,“唐尼”,
“林恩”、“桃金娘”、“玫瑰”、“鲁道夫”,
“托尼”、“特鲁迪”、“威廉姆斯”、“扎克”
);
setItems(名称);
listView.setCellFactory(参数->新半径