gpt4 book ai didi

java - 在 JavaFx TableView 中插入超链接

转载 作者:行者123 更新时间:2023-11-30 07:16:33 26 4
gpt4 key购买 nike

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

将创建超链接监听器,以在单击选定行时删除该行。

当用户单击按钮时调用此方法,这里我创建链接:

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);
}

}

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

最佳答案

在项目类中包含 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<>());
<小时/>

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

关于java - 在 JavaFx TableView 中插入超链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38290286/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com