使用JavaFX,想要使Tableview中的某一列 TableColumn 是按钮button ,该如何实现 ???
急求,谢谢!!!
使用JavaFX,想要使Tableview中的某一列 TableColumn 是按钮button ,该如何实现 ???
急求,谢谢!!!
serverInfoButtonColumn.setCellFactory(tableColumn -> new AddServerInfoCell(serverInfoView));
package entity;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.text.TextAlignment;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
/**
@description:
/
public class AddServerInfoCell extends TableCell {
/
/**
/**
/** places an add button in the row only if the row is not empty. */
@Override
protected void updateItem(Boolean item, boolean empty) {
super.updateItem(item, empty);
if (!empty) {
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
HBox pane = new HBox(startButton,delButton);
pane.setAlignment(Pos.CENTER);
setGraphic(pane);
} else {
setGraphic(null);
}
}
/**
@param y the y position of the top left corner of the dialog.
*/
private void showAddPersonDialog(final TableView table, double y) {
// initialize the dialog.
final Stage dialog = new Stage();
dialog.setTitle("New Person");
dialog.initModality(Modality.WINDOW_MODAL); //必须关闭对话框后才能操作其他的
dialog.initStyle(StageStyle.UTILITY); //对话框-只保留关闭按钮
dialog.setY(y);
// create a grid for the data entry.
GridPane grid = new GridPane();
final TextField firstNameField = new TextField();
final TextField lastNameField = new TextField();
grid.addRow(0, new Label("First Name"), firstNameField);
grid.addRow(1, new Label("Last Name"), lastNameField);
grid.setHgap(10);
grid.setVgap(10);
GridPane.setHgrow(firstNameField, Priority.ALWAYS);
GridPane.setHgrow(lastNameField, Priority.ALWAYS);
// create action buttons for the dialog.
Button ok = new Button("OK");
ok.setDefaultButton(true);
Button cancel = new Button("Cancel");
cancel.setCancelButton(true);
// only enable the ok button when there has been some text entered.
ok.disableProperty().bind(firstNameField.textProperty().isEqualTo("").or(lastNameField.textProperty().isEqualTo("")));
// add action handlers for the dialog buttons.
ok.setOnAction(actionEvent -> {
int nextIndex = table.getSelectionModel().getSelectedIndex() + 1;
table.getSelectionModel().select(nextIndex);
dialog.close();
});
cancel.setOnAction(actionEvent -> dialog.close());
// layout the dialog.
HBox buttons = HBoxBuilder.create().spacing(10).children(ok, cancel).alignment(Pos.CENTER_RIGHT).build();
VBox layout = new VBox(10);
layout.getChildren().addAll(grid, buttons);
layout.setPadding(new Insets(5));
dialog.setScene(new Scene(layout));
dialog.show();
}
}