如何在JavaFX表视图中添加按钮
我已经在Google和Stackoverflow上搜索过了,但是我没有得到给出的例子。谁能给我解释一下吗 我想在表视图的最后一列添加一个按钮,当它被单击时,它应该触发一个侦听器并传递buttons行的对象。我只是没有从中得到以下示例: 这是我当前的完整代码:如何在JavaFX表视图中添加按钮,java,button,javafx,tableview,Java,Button,Javafx,Tableview,我已经在Google和Stackoverflow上搜索过了,但是我没有得到给出的例子。谁能给我解释一下吗 我想在表视图的最后一列添加一个按钮,当它被单击时,它应该触发一个侦听器并传递buttons行的对象。我只是没有从中得到以下示例: 这是我当前的完整代码: public class SchermdeelWerkplaats extends BorderPane{ //ATD moeder klasse met alle collecties etc. private ATD
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<Task> data = FXCollections.observableArrayList();
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<TableColumn.CellDataFeatures<Task, Boolean>,
ObservableValue<Boolean>>() {
@Override
public ObservableValue<Boolean> call(TableColumn.CellDataFeatures<Task, Boolean> p) {
return new SimpleBooleanProperty(p.getValue() != null);
}
});
col_action.setCellFactory(
new Callback<TableColumn<Task, Task>, TableCell<Task, Task>>() {
@Override
public TableCell<Task, Task> call(TableColumn<Task, Task> p) {
return new ButtonCell();
}
}
);
c1.setCellValueFactory(
new PropertyValueFactory<Task,Date>("date")
);
c2.setCellValueFactory(
new PropertyValueFactory<Task,Task.TaskType>("type")
);
c3.setCellValueFactory(
new PropertyValueFactory<Task,Double>("hours")
);
c4.setCellValueFactory(
new PropertyValueFactory<Task,Boolean>("done")
);
c5.setCellValueFactory(
new PropertyValueFactory<Task,Employee>("employee")
);
c6.setCellValueFactory(
new PropertyValueFactory<Task,Car>("car")
);
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<Task, Task> {
private Button cellButton;
ButtonCell(){
cellButton = new Button("jjhjhjh");
cellButton.setOnAction(new EventHandler<ActionEvent>(){
@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);
}
}
}
}
但不是这个:
TableColumn col_action = new TableColumn<>("Action");
col_action.setCellValueFactory(
new Callback<TableColumn.CellDataFeatures<Task, Boolean>,
ObservableValue<Boolean>>() {
@Override
public ObservableValue<Boolean> call(TableColumn.CellDataFeatures<Task, Boolean> p) {
return new SimpleBooleanProperty(p.getValue() != null);
}
});
col_action.setCellFactory(
new Callback<TableColumn<Task, Task>, TableCell<Task, Task>>() {
@Override
public TableCell<Task, Task> call(TableColumn<Task, Task> p) {
return new ButtonCell();
}
}
);
TableColumn col_action=新的TableColumn(“action”);
col_action.setCellValueFactory(
新回调函数(){
@凌驾
公共observeValue调用(TableColumn.celldatap){
返回新的SimpleBoleAnProperty(p.getValue()!=null);
}
});
col_action.setcell工厂(
新回调函数(){
@凌驾
公共TableCell调用(TableP列){
返回新的ButtonCell();
}
}
);
要能够呈现列,TableColumn
需要cellValueFactory。但底层数据模型中不存在“action”列。在本例中,我只给cellValueFactory一些虚拟值,然后继续:
public class JustDoIt 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) {
stage.setWidth(450);
stage.setHeight(500);
TableColumn firstNameCol = new TableColumn("First Name");
firstNameCol.setCellValueFactory(new PropertyValueFactory<>("firstName"));
TableColumn lastNameCol = new TableColumn("Last Name");
lastNameCol.setCellValueFactory(new PropertyValueFactory<>("lastName"));
TableColumn actionCol = new TableColumn("Action");
actionCol.setCellValueFactory(new PropertyValueFactory<>("DUMMY"));
Callback<TableColumn<Person, String>, TableCell<Person, String>> cellFactory
= //
new Callback<TableColumn<Person, String>, TableCell<Person, String>>() {
@Override
public TableCell call(final TableColumn<Person, String> param) {
final TableCell<Person, String> cell = new TableCell<Person, String>() {
final Button btn = new Button("Just Do It");
@Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setGraphic(null);
setText(null);
} else {
btn.setOnAction(event -> {
Person person = getTableView().getItems().get(getIndex());
System.out.println(person.getFirstName()
+ " " + person.getLastName());
});
setGraphic(btn);
setText(null);
}
}
};
return cell;
}
};
actionCol.setCellFactory(cellFactory);
table.setItems(data);
table.getColumns().addAll(firstNameCol, lastNameCol, actionCol);
Scene scene = new Scene(new Group());
((Group) scene.getRoot()).getChildren().addAll(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);
}
}
}
public类JustDoIt扩展了应用程序{
private final TableView table=new TableView();
私有最终可观测列表数据
=FXCollections.observableArrayList(
新人(“雅各布”、“史密斯”),
新人(“伊莎贝拉”、“约翰逊”),
新人(“伊桑”、“威廉姆斯”),
新人(“艾玛”、“琼斯”),
新人(“迈克尔”、“布朗”)
);
公共静态void main(字符串[]args){
发射(args);
}
@凌驾
公众假期开始(阶段){
舞台布景宽度(450);
舞台设置高度(500);
TableColumn firstNameCol=新的TableColumn(“名字”);
firstNameCol.setCellValueFactory(新属性ValueFactory(“firstName”));
TableColumn lastNameCol=新的TableColumn(“姓氏”);
lastNameCol.setCellValueFactory(新属性ValueFactory(“lastName”));
TableColumn actionCol=新的TableColumn(“操作”);
actionCol.setCellValueFactory(新属性ValueFactory(“虚拟”);
回叫手机工厂
= //
新回调函数(){
@凌驾
公共TableCell调用(最终TableColumn参数){
最终TableCell单元格=新TableCell(){
final Button btn=新按钮(“只管做”);
@凌驾
public void updateItem(字符串项,布尔值为空){
super.updateItem(项,空);
if(空){
设置图形(空);
setText(空);
}否则{
btn.设置动作(事件->{
Person=getTableView().getItems().get(getIndex());
System.out.println(person.getFirstName()
+“”+person.getLastName());
});
设置图形(btn);
setText(空);
}
}
};
返回单元;
}
};
actionCol.setCellFactory(cellFactory);
表2.设置项目(数据);
table.getColumns().addAll(firstNameCol、lastNameCol、actionCol);
场景=新场景(新组());
((组)scene.getRoot()).getChildren().addAll(表);
舞台场景;
stage.show();
}
公共静态类人员{
私有最终SimpleStringProperty名字;
私有最终SimpleStringProperty姓氏;
私人(字符串fName,字符串lName){
this.firstName=新的SimpleStringProperty(fName);
this.lastName=新的SimpleStringProperty(lName);
}
公共字符串getFirstName(){
返回firstName.get();
}
public void setFirstName(字符串fName){
firstName.set(fName);
}
公共字符串getLastName(){
返回lastName.get();
}
public void setLastName(字符串fName){
lastName.set(fName);
}
}
}
下面是我使用出色的Java 8功能和扩展TableCell类的示例
让我快速解释一下我在做什么:
我创建了一个ActionButtonTableCell类,它扩展了TableCell。然后可以使用Java8LAMDA函数为按钮创建一个动作
import java.util.function.Function;
import javafx.event.ActionEvent;
import javafx.scene.control.Button;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.util.Callback;
public class ActionButtonTableCell<S> extends TableCell<S, Button> {
private final Button actionButton;
public ActionButtonTableCell(String label, Function< S, S> function) {
this.getStyleClass().add("action-button-table-cell");
this.actionButton = new Button(label);
this.actionButton.setOnAction((ActionEvent e) -> {
function.apply(getCurrentItem());
});
this.actionButton.setMaxWidth(Double.MAX_VALUE);
}
public S getCurrentItem() {
return (S) getTableView().getItems().get(getIndex());
}
public static <S> Callback<TableColumn<S, Button>, TableCell<S, Button>> forTableColumn(String label, Function< S, S> function) {
return param -> new ActionButtonTableCell<>(label, function);
}
@Override
public void updateItem(Button item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setGraphic(null);
} else {
setGraphic(actionButton);
}
}
}
import java.util.function.function;
导入javafx.event.ActionEvent;
导入javafx.scene.control.Button;
导入javafx.scene.control.TableCell;
导入javafx.scene.control.TableColumn;
导入javafx.util.Callback;
公共类ActionButtonTableCell扩展了TableCell{
私人最终按钮操作按钮;
公共操作按钮TableCell(字符串标签,函数函数){
this.getStyleClass().add(“操作按钮表单元格”);
this.actionButton=新按钮(标签);
this.actionButton.setOnAction((ActionEvent e)->{
apply(getCurrentItem());
});
this.actionButton.setMaxWidth(Double.MAX_值);
}
公共的getCurrentItem(){
返回getTableView().getItems().get(getIndex());
}
tableColumn的公共静态回调(字符串标签,函数函数){
返回参数->新建ActionButtonTableCell(标签、函数);
}
@凌驾
public void updateItem(按钮项,布尔值为空){
super.updateItem(项,空);
if(空){
设置图形(空);
}否则{
设置图形(操作按钮);
}
import java.util.function.Function;
import javafx.event.ActionEvent;
import javafx.scene.control.Button;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.util.Callback;
public class ActionButtonTableCell<S> extends TableCell<S, Button> {
private final Button actionButton;
public ActionButtonTableCell(String label, Function< S, S> function) {
this.getStyleClass().add("action-button-table-cell");
this.actionButton = new Button(label);
this.actionButton.setOnAction((ActionEvent e) -> {
function.apply(getCurrentItem());
});
this.actionButton.setMaxWidth(Double.MAX_VALUE);
}
public S getCurrentItem() {
return (S) getTableView().getItems().get(getIndex());
}
public static <S> Callback<TableColumn<S, Button>, TableCell<S, Button>> forTableColumn(String label, Function< S, S> function) {
return param -> new ActionButtonTableCell<>(label, function);
}
@Override
public void updateItem(Button item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setGraphic(null);
} else {
setGraphic(actionButton);
}
}
}
column.setCellFactory(ActionButtonTableCell.<Person>forTableColumn("Remove", (Person p) -> {
table.getItems().remove(p);
return p;
}));