rpo rpo - 1 year ago 671
Java Question

Inserting Hyperlink in JavaFx TableView

I am trying to insert HyperLink with text "Remove" in all rows of a column. Only TabelView row will be inserted when clicked on a button. The hyperlink also get inserted but not for all rows. It automatically get blank cell for previous row if next row data is added. Screenshot:
enter image description here

The hyperlink listener will be created to remove selected row if clicked on it.

This method is called when user click on a button, here i am creating the link:

public void SalesAdd(ActionEvent actionEvent){

quantity.setStyle("-fx-border-color: red");
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");


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!");


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



The TableData Class:

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;


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

public SimpleStringProperty nameProperty() {
return name;

public void setName(String name) {

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

public SimpleStringProperty batchProperty() {
return batch;

public void setBatch(String batch) {

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

public SimpleStringProperty expProperty() {
return exp;

public void setExp(String exp) {

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

public SimpleIntegerProperty qtyProperty() {
return qty;

public void setQty(int qty) {

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

public SimpleDoubleProperty mrpProperty() {
return mrp;

public void setMrp(double mrp) {

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

public SimpleDoubleProperty amtProperty() {
return amt;

public void setAmt(double amt) {


How do i add this same HyperLink for every row in a column?

Answer Source

Including UI elements in the item class is seldom a good idea (also extending ActionEvent seems unnecessary). The link works independent form any item value, therefore it should't use one. Instead use a cell that displays a link when it's non-empty:

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

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

        setGraphic(empty ? null : link);


This cell does not take any data. Instead the link is displayed only for non-empty cells (setGraphic(empty ? null : link);). When the onAction event of the Hyperlink is triggered, the data available from the TableCell is used to remove the corresponding element from the TableView that contains the cell. Additional code could be added to the body of the lambda expression in case additional operations need to be done on the removal of a item.

Do not use a cellValueFactory for the removeRowColumn (choosing Void as value type only allows for null values), instead just use a cellFactory creating RemoveCells:

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

BTW: You seem to be recreating the cellValueFactorys on a button click inserting a single new item. It would be a better idea to do this just once for the whole table instead of once per inserted table row.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download