带有复选框行选择的Java Fx表

时间:2015-10-14 18:32:34

标签: checkbox javafx tableview selection

我想使用第一列的复选框创建一个包含多个选择的简单表。

我已经创建了一个自定义“TableColumn”来处理我需要的每个表中的这个功能。我已经实现了除一个之外的所有功能:当我选择或取消选择单行时,必须选择或取消选择第一个列上的相应复选框。我需要帮助才能开发出最后一个功能。

public class ColumnSelectRow extends TableColumn{

private CheckBox headerCheckBox = new CheckBox();
public ColumnSelectRow(String title) {
    super(title);
    setSortable(false); 
    setPrefWidth(100);
    setMaxWidth(100);
    setMinWidth(100);
    setResizable(false);        
    setGraphic(headerCheckBox);
    setCellValueFactory( new Callback<TableColumn.CellDataFeatures<Object, Boolean>,  ObservableValue<Boolean>>() { 
            @Override
            public ObservableValue<Boolean> call(TableColumn.CellDataFeatures<Object, Boolean> p) {
                return new SimpleBooleanProperty(p.getValue() != null);                    
            }
        });               
    setCellFactory(new Callback<javafx.scene.control.TableColumn<Object, Boolean>, TableCell<Object, Boolean>>() { 
        @Override
        public TableCell<Object, Boolean> call(javafx.scene.control.TableColumn<Object, Boolean> p) {                
            return new SelectButtonCell();
        }
    });

    headerCheckBox.setOnAction(t->{
        if(disableEvents) return;
        TableView table = ColumnSelectRow.this.getTableView();                
        if (table == null)  return;
        if(headerCheckBox.isSelected()) table.getSelectionModel().selectAll();
        else                            table.getSelectionModel().clearSelection();
    });
}

public CheckBox getHeaderCheckBox() {
    return headerCheckBox;
}

private boolean disableEvents = false;

private class SelectButtonCell extends TableCell {
    private final CheckBox checkBox = new CheckBox();

    public SelectButtonCell(){             
        setAlignment(Pos.CENTER); 
        checkBox.setOnAction(t -> {
            int row = getTableRow().getIndex();
            TableView table = SelectButtonCell.this.getTableView();                
            if (table == null)  return;      
            if(checkBox.isSelected()) table.getSelectionModel().select(row);
            else                      table.getSelectionModel().clearSelection(row);
            table.getFocusModel().focus(row);

            boolean empty = table.getSelectionModel().isEmpty();
            boolean full  = table.getSelectionModel().getSelectedCells().size()==table.getItems().size();
            disableEvents = true;
            headerCheckBox.setIndeterminate(false);
            if(empty)     headerCheckBox.setSelected(false);
            else if(full) headerCheckBox.setSelected(true);
            else          headerCheckBox.setIndeterminate(true);
            disableEvents = false;
        });                        
    }                 

    public CheckBox getCheckBox() {
        return checkBox;
    }

    @Override
    protected void updateItem(Object item, boolean empty) {
        super.updateItem(item, empty);
        if(!empty){                
            setGraphic(checkBox);
        } 
        int row = getTableRow().getIndex();
        TableView table = SelectButtonCell.this.getTableView();
        checkBox.setSelected(table.getSelectionModel().isSelected(row));
    }

    public boolean isCheckBoxSelected(){
        return checkBox.isSelected();
    }
}

谢谢! 编辑:具有第一列可选择的自定义表

    import javafx.application.Application;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.stage.Stage;


public class MyTable extends Application {

    private ColumnSelectRow columSelect;

    @Override
    public void start(Stage primaryStage) throws Exception {
        columSelect = new ColumnSelectRow(); 
        TableView T = new TableView<>();
        T.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
        ObservableList<myObject> al = FXCollections.observableArrayList();
        al.add(new myObject(1));
        al.add(new myObject(2));
        al.add(new myObject(3));
        T.setItems(al);
        T.getColumns().add(columSelect);
        TableColumn<myObject, Number> number = new TableColumn("Number");        
        number.setCellValueFactory(cellData->cellData.getValue().propetry); 
        T.getColumns().add(number);
        Scene scene = new Scene(T);
        primaryStage.setScene(scene);     
        primaryStage.show(); 
    }

    public static void main(String[] args) {
        launch(args);
    }

    private class myObject{
        public SimpleIntegerProperty propetry = new SimpleIntegerProperty();

        public myObject(int i) {
            propetry.set(i);
        }                
    }

}

0 个答案:

没有答案