如何在JavaFX表格视图中添加按钮
如何在JavaFX表格视图中添加按钮
我在Google和Stackoverflow上搜索了这个问题,但我对给出的例子一点都不了解。有人能解释一下吗?
我想在表格视图的最后一列中添加一个按钮,当点击按钮时,它应该触发一个监听器并传递按钮所在行的对象。我对gist.github.com上的示例不太理解。
这是我的完整代码:
public class SchermdeelWerkplaats extends BorderPane{
//ATD moeder klasse met alle collecties etc.
private ATD $;
TableView tabel = new TableView();
Button nieuwTaak = new Button("Nieuwe taak inboeken");
final ObservableList
public SchermdeelWerkplaats(ATD a) {
$ = a;
data.addAll($.agenda);
tabel.setEditable(false);
tabel.setPlaceholder(new Label("Geen taken"));
TableColumn c1 = new TableColumn("datum");
c1.setMinWidth(200);
TableColumn c2 = new TableColumn("type");
c2.setMinWidth(100);
TableColumn c3 = new TableColumn("uren");
c3.setMinWidth(100);
TableColumn c4 = new TableColumn("klaar");
c4.setMinWidth(200);
TableColumn c5 = new TableColumn("Werknemer");
c5.setMinWidth(100);
TableColumn c6= new TableColumn("Auto");
c6.setMinWidth(400);
TableColumn c7= new TableColumn("Actie");
c7.setMinWidth(400);
TableColumn col_action = new TableColumn<>("Action");
col_action.setCellValueFactory(
new Callback
ObservableValue
@Override
public ObservableValue
return new SimpleBooleanProperty(p.getValue() != null);
}
});
col_action.setCellFactory(
new Callback
@Override
public TableCell
return new ButtonCell();
}
}
);
c1.setCellValueFactory(
new PropertyValueFactory
);
c2.setCellValueFactory(
new PropertyValueFactory
);
c3.setCellValueFactory(
new PropertyValueFactory
);
c4.setCellValueFactory(
new PropertyValueFactory
);
c5.setCellValueFactory(
new PropertyValueFactory
);
c6.setCellValueFactory(
new PropertyValueFactory
);
tabel.getColumns().addAll(c1, c2, c3, c4, c5, c6, c7);
tabel.setItems(data);
setCenter(tabel);
setBottom(nieuwTaak);
}
//letterlijk van internet geplukt en datatype aangepast
private class ButtonCell extends TableCell
private Button cellButton;
ButtonCell(){
cellButton = new Button("jjhjhjh");
cellButton.setOnAction(new EventHandler
@Override
public void handle(ActionEvent t) {
// do something when button clicked
Task record = getItem();
// do something with record....
}
});
}
//Display button if the row is not empty
@Override
protected void updateItem(Task record, boolean empty) {
super.updateItem(record, empty);
if(!empty){
cellButton.setText("Something with "+record);
setGraphic(cellButton);
} else {
setGraphic(null);
}
}
}
}
现在我需要创建一个ButtonCell extends TableCell
,这部分是可以理解的。但是如何将其分配给列呢?
我理解这个:
c1.setCellValueFactory( new PropertyValueFactory("date") );
但是这个我不理解:
TableColumn col_action = new TableColumn<>("Action"); col_action.setCellValueFactory( new Callback, ObservableValue >() { @Override public ObservableValue call(TableColumn.CellDataFeatures p) { return new SimpleBooleanProperty(p.getValue() != null); } }); col_action.setCellFactory( new Callback , TableCell >() { @Override public TableCell call(TableColumn p) { return new ButtonCell(); } } );