【问题标题】:Inserting Hyperlink in JavaFx TableView在 JavaFx TableView 中插入超链接
【发布时间】:2016-07-10 19:30:56
【问题描述】:

我正在尝试在列的所有行中插入带有文本“删除”的超链接。单击按钮时,只会插入 TabelView 行。超链接也被插入,但不是所有行。如果添加了下一行数据,它会自动获取上一行的空白单元格。截屏:

将创建超链接侦听器以在单击时删除选定的行。

当用户点击一个按钮时调用这个方法,这里我正在创建链接:

public void SalesAdd(ActionEvent actionEvent){


    if(quantity.getText().isEmpty()){
        quantity.setStyle("-fx-border-color: red");
        return;
    }
    String name = comboBox.getSelectionModel().getSelectedItem();
    String batch = batchno.getText();
    String exp = expDate.getText();
    int qty = Integer.parseInt(quantity.getText());
    Double mrp1 = Double.valueOf(mrp.getText());
    Double amt = Double.valueOf(amount.getText());
    Double mrpAmount = mrp1*qty;

    PropertyValueFactory<TableData,String> namePro = new PropertyValueFactory<TableData,String>("name");
    PropertyValueFactory<TableData,Integer> qtyPro = new PropertyValueFactory<TableData,Integer>("qty");
    PropertyValueFactory<TableData,String> expPro = new PropertyValueFactory<TableData,String>("exp");
    PropertyValueFactory<TableData,String> batchPro = new PropertyValueFactory<TableData,String>("batch");
    PropertyValueFactory<TableData,Double> mrpPro = new PropertyValueFactory<TableData,Double>("mrp");
    PropertyValueFactory<TableData,Double> amtPro = new PropertyValueFactory<TableData,Double>("amt");
    PropertyValueFactory<TableData,Hyperlink> rmbutton = new PropertyValueFactory<TableData,Hyperlink>("rbutton");

    nameColumn.setCellValueFactory(namePro);
    qtyColumn.setCellValueFactory(qtyPro);
    expColumn.setCellValueFactory(expPro);
    batchColumn.setCellValueFactory(batchPro);
    mrpColumn.setCellValueFactory(mrpPro);
    amtColumn.setCellValueFactory(amtPro);
    removeRowColumn.setCellValueFactory(rmbutton);



    for(TableData data:tableData){
        if(data.getName()==comboBox.getEditor().getText() || data.getName() == comboBox.getSelectionModel().getSelectedItem().toString()){
            common.dialogAlert("Already in Table!","Already in the Table!","Already Exist,Please change the quantity!");
            return;
        }

    }

    tableData.add(new TableData(name,batch,exp,qty,mrp1,mrpAmount, rbutton));
    tableView.setItems(tableData);

    clearInput();
    calctotal();


}

TableData 类:

public class TableData extends ActionEvent {
private final SimpleStringProperty name;
private final SimpleStringProperty batch;
private final SimpleStringProperty exp;
private final SimpleIntegerProperty qty;
private final SimpleDoubleProperty mrp;
private final SimpleDoubleProperty amt;
private final Hyperlink rbutton;

public Hyperlink getRbutton() {
    return rbutton;
}

public TableData(String name, String batch,
                 String exp, int qty, Double mrp, Double amt, Hyperlink rbutton) {
    this.name = new SimpleStringProperty(name);
    this.batch = new SimpleStringProperty(batch);
    this.exp = new SimpleStringProperty(exp);
    this.qty = new SimpleIntegerProperty(qty);
    this.mrp = new SimpleDoubleProperty(mrp);
    this.amt = new SimpleDoubleProperty(amt);
    this.rbutton = rbutton;
    this.amt.bind(this.qty.multiply(this.mrp));


}

public String getName() {
    return name.get();
}

public SimpleStringProperty nameProperty() {
    return name;
}

public void setName(String name) {
    this.name.set(name);
}

public String getBatch() {
    return batch.get();
}

public SimpleStringProperty batchProperty() {
    return batch;
}

public void setBatch(String batch) {
    this.batch.set(batch);
}

public String getExp() {
    return exp.get();
}

public SimpleStringProperty expProperty() {
    return exp;
}

public void setExp(String exp) {
    this.exp.set(exp);
}

public int getQty() {
    return qty.get();
}

public SimpleIntegerProperty qtyProperty() {
    return qty;
}

public void setQty(int qty) {
    this.qty.set(qty);
}

public double getMrp() {
    return mrp.get();
}

public SimpleDoubleProperty mrpProperty() {
    return mrp;
}

public void setMrp(double mrp) {
    this.mrp.set(mrp);
}

public double getAmt() {
    return amt.get();
}

public SimpleDoubleProperty amtProperty() {
    return amt;
}

public void setAmt(double amt) {
    this.amt.set(amt);
}

}

如何为列中的每一行添加相同的超链接?

【问题讨论】:

  • 我猜你对所有行都使用相同的 HyperLink 对象,因为 Node 不能在场景图中添加两次(这就是为什么只有最后添加的行显示超链接)。对于每个 TableData 对象,您应该有一个单独的 HyperLink 对象。我也不明白为什么你的数据模型扩展ActionEvent。在这里你可以找到一篇好文章如何将HyperLinks 插入到TableViewsuperglobals.net/create-hyperlink-cell-in-javafx-tableview
  • 私有超链接 rbutton = new Hyperlink();rbutton=new Hyperlink("Remove");工作,但现在听众不工作?监听器: rbutton.setOnAction(new EventHandler() { @Override public void handle(ActionEvent event) { System.out.println("Clicked"); } });

标签: java javafx


【解决方案1】:

在项目类中包含 UI 元素很少是一个好主意(也似乎没有必要扩展 ActionEvent)。该链接独立于任何项目值工作,因此不应使用一个。而是使用非空时显示链接的单元格:

public class RemoveCell<T> extends TableCell<T, Void> {

    private final Hyperlink link;

    public RemoveCell() {
        link = new Hyperlink("Remove");
        link.setOnAction(evt -> {
            // remove row item from tableview
            getTableView().getItems().remove(getTableRow().getIndex());
        });
    }

    @Override
    protected void updateItem(Void item, boolean empty) {
        super.updateItem(item, empty);

        setGraphic(empty ? null : link);
    }

}

此单元格不获取任何数据。相反,该链接仅针对非空单元格 (setGraphic(empty ? null : link);) 显示。当HyperlinkonAction 事件被触发时,TableCell 中可用的数据用于从包含单元格的TableView 中删除相应的元素。额外的代码可以添加到 lambda 表达式的主体中,以防在删除项目时需要执行额外的操作。


不要将cellValueFactory 用于removeRowColumn(选择Void 作为值类型仅允许null 值),而只需使用cellFactory 创建RemoveCells:

removeRowColumn.setCellFactory(tc -> new RemoveCell<>());

顺便说一句:您似乎在单击按钮插入一个新项目时重新创建 cellValueFactorys。最好对整个表只执行一次,而不是为每个插入的表行执行一次。

【讨论】:

  • 我是 Java 新手。你能解释一下这是如何工作的吗?我从来没有写过这样的代码:(。我还有一个计算另一行的方法。删除一行后如何调用该方法?
  • @rpo:您在评论中询问的内容基本上是一个教程。这超出了答案的范围。比 “这是如何工作的?” 更具体的问题可能是可以回答的,或者至少我可以发布一些链接以供进一步阅读。 The Cell Factories section here 可以阐明Cell/cellFactory 的概念。
猜你喜欢
  • 2017-05-03
  • 2011-10-15
  • 1970-01-01
  • 2016-08-18
  • 2016-06-23
  • 1970-01-01
  • 2023-03-11
  • 2013-01-29
  • 2021-07-26
相关资源
最近更新 更多