tableview列或单元格中的单选按钮/复选框 - JavaFX

时间:2016-04-20 00:57:40

标签: javafx tableview

我有一个在JavaFX中填充了一个可观察列表的tableview表,我需要一个列,它将为每个项目保留一个单选按钮或复选框,这样如果选择了多个,它们可以用于下一个GUI屏幕列表。我在这个项目之前从未使用过TableView / JavaFX,虽然我知道如何实现button / checkbox,但我不知道怎么把它放到我的list或tableColumn中。任何帮助深表感谢。

 @FXML private TableView<SummerClass> table;

        @FXML private TableColumn<SummerClass, Integer> id;
        @FXML private TableColumn<SummerClass, String> dept;
        @FXML private TableColumn<SummerClass,Integer> number;
        @FXML private TableColumn<SummerClass, String> title;
        @FXML private TableColumn<SummerClass, String> day;
        @FXML private TableColumn<SummerClass, String> time;

public ObservableList<SummerClass> list1 =        FXCollections.observableArrayList(
        new SummerClass (10001, "ACCT", 1010 , "Intro to Acct (3)", "MWF",   "1:00 - 2:15" ),
        new SummerClass (10002, "ACCT", 2010 , "Acct for Bus. (3)", "MWF", "9:00 - 10:15" ),
        new SummerClass (10003, "ART", 1010 , "Fund. of Art (3)", "TR", "3:00 - 4:15" ),
        new SummerClass (10004, "ART", 1110 , "Art History (3)", "MWF", "1:00 - 2:15" ),

2 个答案:

答案 0 :(得分:1)

正如James_D在评论中所述:

  

如果您仅使用复选框来跟踪一组选定的行,则可以使用表格视图的内置选择功能,而不是使用复选框。

但是,如果内置选择功能不适合您,并且您确实需要复选框功能,那么您可以:

  1. Set a custom cell factory on a column,并覆盖Ruslan's answer
  2. 中的updateItem
  3. 使用内置的CheckBoxTableCell功能。
  4. 在这两个选项中,我建议使用CheckBoxTableCell而不是创建自定义单元工厂和更新处理程序(除非由于某种原因,CheckBoxTableCell不能为你工作)。

    如果您需要更多信息,有各种现有的StackOverflow问题或Oracle论坛问题,询问如何使用CheckBoxTableCell:

    使用CheckBoxTableCell的关键部分是在模型类中定义一个相关的布尔属性,该类支持您的表并将其链接到相关的可编辑表列:

    vegetarianCol.setCellValueFactory(new PropertyValueFactory<>("vegetarian"));
    vegetarianCol.setCellFactory(CheckBoxTableCell.forTableColumn(vegetarianCol));
    vegetarianCol.setEditable(true);
    

    我刚从以下Oracle论坛链接中复制并粘贴了James_D的解决方案:

    import javafx.application.Application;
    import javafx.beans.property.*;
    import javafx.collections.FXCollections;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.control.cell.*;
    import javafx.scene.layout.*;
    import javafx.stage.Stage;
    
    public class CheckBoxTableCellTest extends Application {
      @Override
      public void start(Stage primaryStage) {
        final TableView<Person> tableView = new TableView<>();
        tableView.setItems(FXCollections.observableArrayList(
            new Person("Robert", "Plant"), 
            new Person("Neil", "Young"), 
            new Person("Willie", "Nelson"),
            new Person("Natalie", "Merchant")
        ));
        tableView.getItems().get(3).setVegetarian(true);
        final TableColumn<Person, String> firstNameCol = new TableColumn<>("First Name");
        final TableColumn<Person, String> lastNameCol = new TableColumn<>("Last Name");
        final TableColumn<Person, Boolean> vegetarianCol = new TableColumn<>("Vegetarian");
        tableView.getColumns().addAll(firstNameCol, lastNameCol, vegetarianCol);
        firstNameCol.setCellValueFactory(new PropertyValueFactory<>("firstName"));
        lastNameCol.setCellValueFactory(new PropertyValueFactory<>("lastName"));
        vegetarianCol.setCellValueFactory(new PropertyValueFactory<>("vegetarian"));
        vegetarianCol.setCellFactory(CheckBoxTableCell.forTableColumn(vegetarianCol));
        vegetarianCol.setEditable(true);
        tableView.setEditable(true);
    
        final BorderPane root = new BorderPane();
        root.setCenter(tableView);
    
        final HBox controls = new HBox(5);
        final Button infoButton = new Button("Show details");
        infoButton.setOnAction(event -> {
          for (Person p : tableView.getItems())
            System.out.printf("%s %s (%svegetarian)%n", p.getFirstName(),
                p.getLastName(), p.isVegetarian() ? "" : "not ");
          System.out.println();
        });
        controls.getChildren().add(infoButton);
        root.setBottom(controls);
    
        Scene scene = new Scene(root, 300, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
      }
    
      public static void main(String[] args) {
        launch(args);
      }
    
      public static class Person {
        private StringProperty firstName;
        private StringProperty lastName;
        private BooleanProperty vegetarian;
    
        public Person(String firstName, String lastName) {
          this.firstName = new SimpleStringProperty(firstName);
          this.lastName = new SimpleStringProperty(lastName);
          this.vegetarian = new SimpleBooleanProperty(false);
        }
    
        public String getFirstName() {
          return firstName.get();
        }
    
        public String getLastName() {
          return lastName.get();
        }
    
        public boolean isVegetarian() {
          return vegetarian.get();
        }
    
        public void setFirstName(String firstName) {
          this.firstName.set(firstName);
        }
    
        public void setLastName(String lastName) {
          this.lastName.set(lastName);
        }
    
        public void setVegetarian(boolean vegetarian) {
          this.vegetarian.set(vegetarian);
        }
    
        public StringProperty firstNameProperty() {
          return firstName;
        }
    
        public StringProperty lastNameProperty() {
          return lastName;
        }
    
        public BooleanProperty vegetarianProperty() {
          return vegetarian;
        }
      }
    }
    

答案 1 :(得分:0)

为了成为它,你需要覆盖&#34; updateItem&#34; Cell的方法,你想放置RadioButton或其他东西。像

这样的东西
    @Override
    public void updateItem(Number item, boolean empty) {
        super.updateItem(item, empty);
        if (empty) {
            setGraphic(null);
        } else {
            setGraphic(***YourNode!***);
        }

这是一个带有按钮的示例,添加到TableView,您可以使用它来查看它的工作原理: http://java-buddy.blogspot.de/2013/03/javafx-embed-button-in-tableview.html