-1

Ok so the problem is, There is a button in every row. But when I add a new row to the table the button on that row isn't working. How to fix this? Look at the picture, The button of the new added row isn't working but it is working for the default added rows. (https://i.stack.imgur.com/VLBjQ.png)](https://i.stack.imgur.com/dSIEI.png)](https://i.stack.imgur.com/9A6jV.png)](https://i.stack.imgur.com/YS6RR.png)

---MainController Class


package javafxtableviewaddrows;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.collections.transformation.SortedList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.paint.Color;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
/**
 *
 * @author Cool IT Help
 */
public class FXMLDocumentController implements Initializable {
    
    @FXML
    private Label label;
    @FXML private TextField filterField;
    @FXML private TableView<Customer> tableview;
    Button []rbutton=new Button[400];
   
         @FXML  public ObservableList<Customer> dataList = FXCollections.observableArrayList();    
    //observalble list to store data
    
    
    public void buttonAction(ActionEvent event){
        for(int i=0;i<rbutton.length;i++){
            if(event.getSource()==rbutton[i]){
                System.out.println("Button "+i+" is pressed");
            }
        }
    }
    private void calculation(){
        TableColumn id=new TableColumn("ID");
        TableColumn name=new TableColumn("NAME");
        TableColumn address=new TableColumn("ADDRESS");
        TableColumn description=new TableColumn("DESCRIPTION");
        TableColumn quantity=new TableColumn("QUANTITY");
        TableColumn transiction=new TableColumn("TRANSICTION");
        TableColumn payment=new TableColumn("PAYMENT");
        TableColumn button=new TableColumn("PREVIEW");
        
        tableview.getColumns().addAll(id,name,address,description,quantity,transiction,payment,button);
        
         for(int i=0;i<rbutton.length;i++){
            rbutton[i]=new Button();
            rbutton[i].setOnAction(this::buttonAction);
        }
        dataList = FXCollections.observableArrayList(
               new Customer("1", "AMIT", "east west", "batman tshirt", "= 1","/NO","NOT COMPLETED",rbutton[0]),
    new Customer("2", "AMIT", "east west", "batman tshirt", "= 1","/NO","NOT COMPLETED",rbutton[1]),
     new Customer("3", "AMIT", "east west", "batman tshirt", "= 1","/NO","NOT COMPLETED",rbutton[2]),
     new Customer("4", "AMIT", "east west", "batman tshirt", "= 1","/NO","NOT COMPLETED",rbutton[3])
    );  
                               
        id.setCellValueFactory(new PropertyValueFactory("id"));       
        name.setCellValueFactory(new PropertyValueFactory("name"));        
        address.setCellValueFactory(new PropertyValueFactory("address"));        
        description.setCellValueFactory(new PropertyValueFactory("description"));        
        quantity.setCellValueFactory(new PropertyValueFactory("quantity")); 
        transiction.setCellValueFactory(new PropertyValueFactory("transiction")); 
        payment.setCellValueFactory(new PropertyValueFactory("payment")); 
        button.setCellValueFactory(new PropertyValueFactory("button")); 
        
        
        
        // Wrap the ObservableList in a FilteredList (initially display all data).
       FilteredList<Customer> filteredData = new FilteredList<>(dataList, b -> true);
        
        // 2. Set the filter Predicate whenever the filter changes.
        filterField.textProperty().addListener((observable, oldValue, newValue) -> {
            filteredData.setPredicate(employee -> {
                // If filter text is empty, display all persons.
                                
                if (newValue == null || newValue.isEmpty()) {
                    return true;
                }
                
                // Compare first name and last name of every person with filter text.
                String lowerCaseFilter = newValue.toLowerCase();
                
                if (employee.getId().toLowerCase().indexOf(lowerCaseFilter) != -1 ) {
                    return true; // Filter matches first name.
                } else if (employee.getName().toLowerCase().indexOf(lowerCaseFilter) != -1) {
                    return true; // Filter matches last name.
                }
                else if (employee.getPayment().toLowerCase().indexOf(lowerCaseFilter) != -1)
                     return true;
                     else  
                         return false; // Does not match.
            });
        });
        
        // 3. Wrap the FilteredList in a SortedList. 
        SortedList<Customer> sortedData = new SortedList<>(filteredData);
        
        // 4. Bind the SortedList comparator to the TableView comparator.
        //    Otherwise, sorting the TableView would have no effect.
        sortedData.comparatorProperty().bind(tableview.comparatorProperty());
        
        // 5. Add sorted (and filtered) data to the table.
        tableview.setItems(sortedData);
    }
    public void initialize(URL url, ResourceBundle rb) {  
    
             calculation();  
        
    }  
    @FXML private void deleteRow(ActionEvent event) throws IOException{
    dataList.remove(tableview.getSelectionModel().getSelectedItem());
}
    @FXML public void addrows(String id, String name, String address, String description, String quantity, String transiction, String payment) {
    Customer customer = new Customer(id, name, address, description, quantity, transiction, payment,rbutton[4]);
    dataList.add(customer);
    Platform.runLater(() -> {
       
        int row = tableview.getItems().size() - 1;
        tableview.getSelectionModel().select(row);
        tableview.scrollTo(row);
    });
        
    
}
   @FXML private void addRow(ActionEvent event) throws IOException {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("dashbord.fxml"));
        Parent root = loader.load();
        
        // pass the table's list to the dialog controller:
        DashbordController customerDialogController = loader.getController();
        customerDialogController.setCustomerList(dataList,rbutton);
        
        Scene scene = new Scene(root);
        Stage stage = new Stage();
        stage.setScene(scene);
        stage.initOwner(tableview.getScene().getWindow());
        stage.initModality(Modality.WINDOW_MODAL);
        stage.show();
    }
    
}

---CustomerClass


package javafxtableviewaddrows;

import javafx.beans.property.SimpleStringProperty;
import javafx.scene.control.Button;

public class Customer {
    private SimpleStringProperty id;
    private SimpleStringProperty name;
    private SimpleStringProperty address;
    private SimpleStringProperty description;
    private SimpleStringProperty quantity;
    private SimpleStringProperty transiction;
    private SimpleStringProperty payment;
    private Button button;

    public Customer(String id, String name, String address, String description, String quantity, String transiction, String payment, Button button) {
        this.id = new SimpleStringProperty(id);
        this.name = new SimpleStringProperty(name);
        this.address = new SimpleStringProperty(address);
        this.description = new SimpleStringProperty(description);
        this.quantity = new SimpleStringProperty(quantity);
        this.transiction = new SimpleStringProperty(transiction);
        this.payment = new SimpleStringProperty(payment);
        this.button = button;
        this.button.setText("Preview");
    }

    public String getId() {
        return id.get();
    }

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

    public String getAddress() {
        return address.get();
    }

    public String getDescription() {
        return description.get();
    }

    public String getQuantity() {
        return quantity.get();
    }

    public String getTransiction() {
        return transiction.get();
    }

    public String getPayment() {
        return payment.get();
    }

    public Button getButton() {
        return button;
    }

    public void setId(String id) {
        this.id.set(id);
    }

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

    public void setAddress(String address) {
        this.address.set(address);
    }

    public void setDescription(String description) {
        this.description.set(description);
    }

    public void setQuantity(String quantity) {
        this.quantity.set(quantity);
    }

    public void setTransiction(String transiction) {
        this.transiction.set(transiction);
    }

    public void setPayment(String payment) {
        this.payment.set(payment);
    }

    public void setButton(Button button) {
        this.button = button;
    }
    
    
}

---DashbordController Class

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javafxtableviewaddrows;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

/**
 * FXML Controller class
 *
 * @author rajiu
 */
public class DashbordController {

   @FXML
    private TextField id;
    @FXML
    private TextField name;
    @FXML
    private TextField address;
    @FXML
    private TextField description;
    @FXML
    private TextField quantity;
    @FXML
    private TextField transiction;
    @FXML
    private TextField payment;

private ObservableList<Customer> dataList;
private Button []rbutton=new Button[400]; 
    
    public void setCustomerList(ObservableList<Customer> dataList,Button[] rbutton) {
        this.dataList = dataList;
        this.rbutton=rbutton;
    }
    @FXML
    private void addtotable(ActionEvent event) {
        String id1 = id.getText();
        String name1=name.getText();
        String address1=address.getText();
        String description1 =description.getText();
        String quantity1=quantity.getText();
        String transiction1=transiction.getText();
        String payment1=payment.getText();
        if (!id1.isEmpty()) {
            int indx=dataList.size();
           rbutton[indx]=new Button();
           dataList.add(new Customer(id1,name1,address1,description1,quantity1,transiction1,payment1,rbutton[indx]));
        }
        closeWindow();
    }
    
    @FXML
    private void cancel() {
        closeWindow();
    }

    private void closeWindow() {
        id.getScene().getWindow().hide();
        name.getScene().getWindow().hide();
        address.getScene().getWindow().hide();
        description.getScene().getWindow().hide();
        quantity.getScene().getWindow().hide();
        transiction.getScene().getWindow().hide();
        payment.getScene().getWindow().hide();
        id.getScene().getWindow().hide();        
    }
    
    
}

I was expecting that the new added row button will work....

  • 1
    When you add new data to the table, you create a new button, but never set an handler for action events on it. So the new button doesn't do anything. This is just completely the wrong approach anyway. See the linked question for how to have buttons in table cells. – James_D Aug 08 '23 at 03:55
  • 1
    Slightly off-topic, but you seem to have used code here from one of the answers to your [previous question](https://stackoverflow.com/questions/76847264/java-how-to-add-data-to-table-view-when-click-on-button-on-another-stage). If you're going to do that, you should at least have the decency to mark the answer as correct. – James_D Aug 08 '23 at 12:50
  • 1
    The `Customer` class having `private Button button;` defeats the purpose of `VirtualFlow` in the `TableView` in this case. – SedJ601 Aug 08 '23 at 14:21
  • https://stackoverflow.com/questions/29489366/how-to-add-button-in-javafx-table-view – SedJ601 Aug 08 '23 at 14:24

0 Answers0