我正在尝试在列的所有行中插入带有文本“删除”的HyperLink。单击按钮时,只会插入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?
答案 0 :(得分: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);
)显示链接。当触发onAction
的{{1}}事件时,Hyperlink
提供的数据用于从包含该单元格的TableCell
中删除相应的元素。如果需要在删除项目时执行其他操作,可以将附加代码添加到lambda表达式的主体中。
不要对TableView
使用cellValueFactory
(选择removeRowColumn
作为值类型仅允许Void
值),而只需使用null
创建cellFactory
S:
RemoveCell
BTW:您似乎正在重新创建按钮上的removeRowColumn.setCellFactory(tc -> new RemoveCell<>());
点击插入一个新项目。对于整个表而言,这样做一次是一个更好的主意,而不是每个插入的表行一次。