本文介绍了更优雅地将按钮放入表格列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个SSCCE演示如何将 Button 放入 TableView 列。它有效,但几乎没有不愉快的行为。当使用滚动条通过鼠标向下滚动表格行时,新的渲染按钮不会立即显示。它们仅在释放鼠标然后移动时显示。我认为还有另一种更优雅的方式来显示表格列中的按钮,并在点击时执行与所选行相关的一些内容。有什么建议吗?

Here is a SSCCE demonstrating how to put a Button into the TableView column. It works but with little unpleasant behavior. When the table rows are scrolled down by mouse using the scroll bar, the new rendered buttons are not shown immediately. They are shown only when the mouse is released and then moved. I think there is another more elegant way of displaying the button in the table column and when clicked doing some stuff related to the selected row. Any suggestions?

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.StackPaneBuilder;
import javafx.stage.Stage;
import javafx.util.Callback;

public class ButtonDemo extends Application {

    private TableView<Person> personTable = new TableView<>();
    private TableColumn<Person, String> nameCol = new TableColumn<>("name");
    private TableColumn<Person, Double> salaryCol = new TableColumn<>("salary");
    private TableColumn<Person, String> actionCol = new TableColumn<>("action");

    @Override
    public void start(Stage primaryStage) {
        ObservableList<Person> data = FXCollections.observableArrayList();
        for (int i = 0; i < 40; i++) {
            data.add(new Person("person_" + i, i * 100d));
        }

        nameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("name"));
        salaryCol.setCellValueFactory(new PropertyValueFactory<Person, Double>("salary"));
        actionCol.setCellValueFactory(new PropertyValueFactory<Person, String>("name")); // "name" here is for just to render the column

        Callback<TableColumn<Person, String>, TableCell<Person, String>> printColumnCellFactory = //
                new Callback<TableColumn<Person, String>, TableCell<Person, String>>() {

            @Override
            public TableCell call(final TableColumn param) {
                final TableCell cell = new TableCell() {

                    @Override
                    public void updateItem(Object item, boolean empty) {
                        super.updateItem(item, empty);
                        if (empty) {
                            setText(null);
                            setGraphic(null);
                        } else {
                            final Button btnPrint = new Button("print name");
                            btnPrint.setOnAction(new EventHandler<ActionEvent>() {

                                @Override
                                public void handle(ActionEvent event) {
                                    param.getTableView().getSelectionModel().select(getIndex());
                                    Person item = personTable.getSelectionModel().getSelectedItem();
                                    if (item != null) {
                                        System.out.println(item.getName());
                                    }
                                }
                            });
                            setGraphic(btnPrint);
                            setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
                        }
                    }
                };
                return cell;
            }
        };
        actionCol.setCellFactory(printColumnCellFactory);

        personTable.setItems(data);
        personTable.getColumns().addAll(nameCol, salaryCol, actionCol);
        StackPane root = StackPaneBuilder.create().children(personTable).build();
        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.show();
    }

    public class Person {

        private String name;
        private Double salary;

        public Person(String name, Double salary) {
            this.name = name;
            this.salary = salary;
        }

        public String getName() {
            return name;
        }

        public Double getSalary() {
            return salary;
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}


推荐答案

升级到版本2.1解决了问题。

Upgrading to version 2.1 resolved the problem.

这篇关于更优雅地将按钮放入表格列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 06:32
查看更多