【问题标题】:TableView - How to add a download button inside itTableView - 如何在其中添加下载按钮
【发布时间】:2016-04-13 20:56:53
【问题描述】:

我想知道如何在 JavaFX GUI 中的 tablew 视图列中添加下载按钮,这样我就可以访问每个元素的链接并下载我需要的文件。

比如上图中,对于每一行,在下载栏,我想要一个下载按钮。

【问题讨论】:

    标签: java javafx download tableview


    【解决方案1】:

    您需要为您的TableColumn 提供Cell FactoryCell Value Factory。 cellFactory 负责渲染它从 cellValueFactory 获得的数据。

    TableColumn<YourDataModel, String> yourColumn = new TableColumn<>();
    yourColumn.setCellFactory(tableColumn -> new DownloadCell());
    yourColumn.setCellValueFactory(cellData -> cellData.getValue().downloadProperty());
    
    public class YourDataModel {
    
        private StringProperty download = new SimpleStringProperty();
        // additional fields
    
        public StringProperty downloadProperty() {
            return download;
        }
    
        public String getDownload() {
            return download.get();
        }
    
        public void setDownload(String value) {
            download.set(value);
        }
    
    }
    
    public class DownloadCell extends TableCell<YourDataModel, String> {
    
        private Hyperlink downloadLink;
    
        public DownloadCell() {
            downloadLink = new Hyperlink();
            downloadLink.setOnAction(evt -> {
                try {
                    Desktop.getDesktop().browse(new URI(downloadLink.getText()));
                } catch (Exception e) {
                    // exception handling
                }
            });
        }
    
        @Override
        protected void updateItem(String link, boolean empty) {
            super.updateItem(link, empty);
    
            if (link == null || empty) {
                setGraphic(null);
            } else {
                downloadLink.setText(link);
                setGraphic(downloadLink);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-19
      • 1970-01-01
      • 1970-01-01
      • 2018-10-06
      • 2013-05-10
      相关资源
      最近更新 更多