JavaFX TableView为表格添加序号

	用JavaFX 做PC客户端时,TableView 是我经常用的一个组件,但它不能设置自动生成序号,没有序号看着又非常不习惯,通过一个简单的类;方便在多个TableView中方便的生成序号;

public class IDCell implements Callback, TableCell> {

@Override
public TableCell call(TableColumn param) {
	
	TableCell cell=new TableCell() {
			@Override
			protected void updateItem(Object item, boolean empty) {
				super.updateItem(item, empty);
                this.setText(null);
                this.setGraphic(null);
                if (!empty) {
                        int rowIndex = this.getIndex() + 1;
                        this.setText(String.valueOf(rowIndex));
                }
			}
			
		};
	return cell;
}

}

我开始是通过给TableColumn通过Lambda表达式传入Callback, TableCell>的,但项目中有多个TableView 是每次都要写一遍,所有还是建一个类方便些,而且代码也简洁好多;只要在TableView中添加一个序号列,然后给这个列设置;
public void loadTable(List employeeList) {
ObservableList employeeData=FXCollections.observableArrayList();
employeeData.addAll(employeeList);
tablePane.setItems(employeeData);
col_NO.setCellFactory(new IDCell<>()); //通过这个类实现自动增长的序号
col_name.setCellValueFactory(new PropertyValueFactory<>(“name”));
col_phone.setCellValueFactory(new PropertyValueFactory<>(“phone”));
col_depart.setCellValueFactory(new PropertyValueFactory<>(“depart”));
col_position.setCellValueFactory(new PropertyValueFactory<>(“position”));
}

你可能感兴趣的:(JavaFX TableView为表格添加序号)