我有一个TableView,当我向下滚动表格时,未选中复选的复选框。包含CheckBox的TableColumn
为col
,定义为:
final TableColumn col = new TableColumn("Select");
col.setCellValueFactory( new Callback<CellDataFeatures<pojo, CheckBox>, ObservableValue<CheckBox>>() {
@Override
public ObservableValue<CheckBox> call(CellDataFeatures<pojo, CheckBox> arg0) {
final CheckBox checkBox = new CheckBox();
checkBox.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
CheckBox checkBox = (CheckBox) event.getSource();
TableCellSkin tcs = (TableCellSkin)checkBox.getParent();
TableCell tc = (TableCell)tcs.getSkinnable();
TableRowSkin trs = (TableRowSkin)tc.getParent();
TableRow tr = trs.getParent()!=null ? (TableRow) trs.getParent(): (TableRow)trs.getSkinnable();
pojo obj = (pojo) tr.getItem();
if(checkBox.isSelected()){
list.add(obj);
}else{
if(list!=null && !list.isEmpty()){
list.remove(obj);
}
}
}
});
return new SimpleObjectProperty(checkBox);
}
});
答案 0 :(得分:0)
以下是如何使用tableview的选择模型添加一个在企业中工作的复选框列的完整示例。选择模式设置为多个,因此您可以通过CTRL + Mouse_Click选择多个项目:
public class TableViewSelectColumnDemo extends Application
{
private final TableView<Person> table = new TableView<>();
private final ObservableList<Person> data
= FXCollections.observableArrayList(
new Person( "Jacob", "Smith" ),
new Person( "Isabella", "Johnson" ),
new Person( "Ethan", "Williams" ),
new Person( "Emma", "Jones" ),
new Person( "Michael", "Brown" )
);
public static void main( String[] args )
{
launch( args );
}
@Override
public void start( Stage stage )
{
Scene scene = new Scene( new Group() );
stage.setWidth( 450 );
stage.setHeight( 500 );
table.getSelectionModel().setSelectionMode( SelectionMode.MULTIPLE );
List list = new ArrayList();
TableColumn selectCol = new TableColumn( "Select" );
selectCol.setStyle( "-fx-alignment: center" );
selectCol.setCellFactory(
new Callback<TableColumn, TableCell>()
{
public TableCell call( TableColumn p )
{
return new TableCell()
{
private final CheckBox checkBox = new CheckBox();
@Override
public void updateItem( Object item, boolean empty )
{
super.updateItem( item, empty );
setText( null );
if ( empty )
{
setGraphic( null );
}
else
{
// select/unselect the item when checkbox is selected/unselected
checkBox.selectedProperty().addListener( new ChangeListener<Boolean>()
{
@Override
public void changed( ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue )
{
getTableView().requestFocus();
if ( newValue )
{
getTableView().getSelectionModel().select( getIndex() );
list.add( item );
}
else
{
getTableView().getSelectionModel().clearSelection( getIndex() );
list.remove( item );
}
}
} );
// select/unselect the checkbox when the tablerow is selected/unselected
if ( getTableRow() != null )
{
getTableRow().selectedProperty().addListener( new ChangeListener<Boolean>()
{
@Override
public void changed( ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue )
{
checkBox.setSelected( newValue );
if ( newValue )
{
list.add( item );
}
else
{
list.remove( item );
}
}
} );
}
setGraphic( checkBox );
}
}
};
}
} );
TableColumn firstNameCol = new TableColumn( "First Name" );
firstNameCol.setMinWidth( 100 );
firstNameCol.setCellValueFactory( new PropertyValueFactory<>( "firstName" ) );
TableColumn lastNameCol = new TableColumn( "Last Name" );
lastNameCol.setMinWidth( 100 );
lastNameCol.setCellValueFactory( new PropertyValueFactory<>( "lastName" ) );
table.setItems( data );
table.getColumns().addAll( selectCol, firstNameCol, lastNameCol );
(( Group ) scene.getRoot()).getChildren().addAll( new VBox( table ) );
stage.setScene( scene );
stage.show();
}
public static class Person
{
private final SimpleStringProperty firstName;
private final SimpleStringProperty lastName;
private Person( String fName, String lName )
{
this.firstName = new SimpleStringProperty( fName );
this.lastName = new SimpleStringProperty( lName );
}
public String getFirstName()
{
return firstName.get();
}
public void setFirstName( String fName )
{
firstName.set( fName );
}
public String getLastName()
{
return lastName.get();
}
public void setLastName( String fName )
{
lastName.set( fName );
}
}
}