tableview 中每一行的列内按钮

button inside column for each row in tableview

在我的 TableView 中,我的每一行都有用于更新的按钮的列,因此我需要在单击按钮时将行中的所有值获取到新的 fxml window

这是我的承包商class:

public class constractor {

    private String co_id;
    private String co_name;
    private String co_address;
    private String co_create_date;
    private String co_description;
    private String co_mobile;
    private String co_type_compile;
    private String co_status;
    private String co_type_model;
    private Button button;


    public constractor(String co_id, String co_name, String co_type_compile, String co_description, String co_create_date, String co_status, String co_address, String co_mobile, String co_type_model, String button) {
        this.co_id = co_id;
        this.co_name = co_name;
        this.co_type_compile = co_type_compile;
        this.co_description = co_description;
        this.co_create_date = co_create_date;
        this.co_status = co_status;
        this.co_address = co_address;
        this.co_mobile = co_mobile;
        this.co_type_model = co_type_model;
        this.button = new Button("edit");

    }

    public String getCo_id() {
        return co_id;
    }

    public void setCo_id(String co_id) {
        this.co_id = co_id;
    }

    public String getCo_name() {
        return co_name;
    }

    public void setCo_name(String co_name) {
        this.co_name = co_name;
    }

    public String getCo_address() {
        return co_address;
    }

    public void setCo_address(String co_address) {
        this.co_address = co_address;
    }

    public String getCo_create_date() {
        return co_create_date;
    }

    public void setCo_create_date(String co_create_date) {
        this.co_create_date = co_create_date;
    }

    public String getCo_description() {
        return co_description;
    }

    public void setCo_description(String co_description) {
        this.co_description = co_description;
    }

    public String getCo_mobile() {
        return co_mobile;
    }

    public void setCo_mobile(String co_mobile) {
        this.co_mobile = co_mobile;
    }

    public String getCo_type_compile() {
        return co_type_compile;
    }

    public void setCo_type_compile(String co_type_compile) {
        this.co_type_compile = co_type_compile;
    }

    public String getCo_status() {
        return co_status;
    }

    public void setCo_status(String co_status) {
        this.co_status = co_status;
    }

    public String getCo_type_model() {
        return co_type_model;
    }

    public void setCo_type_model(String co_type_model) {
        this.co_type_model = co_type_model;
    }


    public Button getButton() {
        return button;
    }

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

这是我的 class table:

public class MainscreenController implements Initializable {

    @FXML
    private TableView<constractor> co_tableview;
    @FXML
    private TableColumn<constractor, String> col_id;
    @FXML
    private TableColumn<constractor, String> col_name;
    @FXML
    private TableColumn<constractor, String> col_compaile_type;
    @FXML
    private TableColumn<constractor, String> col_description;
    @FXML
    private TableColumn<constractor, String> col_ceartedat;
    @FXML
    public TableColumn<constractor, String> col_status;
    @FXML
    private TableColumn<constractor, String> col_mobile;
    @FXML
    private TableColumn<constractor, String> col_type_model;
    @FXML
    private TextField search;
    @FXML
    private TableColumn<constractor, Button> col_button;

    int indexorder = -1;
    ObservableList<constractor> orderdata = FXCollections.observableArrayList();
    @FXML
    public void ordertables() {
        Connection con = DB.getConnection();

        orderdata.clear();
        try {
            try (ResultSet rs = con.createStatement().executeQuery("select * from mr_order")) {
                while (rs.next()) {
                    orderdata.add(new constractor(
                        rs.getString("co_id"),
                        rs.getString("co_name"),
                        rs.getString("co_type_model"),
                        rs.getString("co_description"),
                        rs.getString("co_create_date"),
                        rs.getString("co_status"),
                        rs.getString("co_mobile"),
                        rs.getString("co_address"),
                        rs.getString("co_type_compile"),
                        rs.getString("co_user_id")
                    ));
                }
                countneworder();
            }
        } catch (SQLException ex) {
            Logger.getLogger(MainscreenController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public int tablesandsearchorder() {
        ////tableview Itemsinserting 
        col_id.setCellValueFactory(new PropertyValueFactory<>("co_id"));
        col_name.setCellValueFactory(new PropertyValueFactory<>("co_name"));
        col_compaile_type.setCellValueFactory(new PropertyValueFactory<> 
        ("co_type_compile"));
        col_description.setCellValueFactory(new PropertyValueFactory<> 
        ("co_description"));
        col_ceartedat.setCellValueFactory(new PropertyValueFactory<> 
        ("co_create_date"));
        col_status.setCellValueFactory(new PropertyValueFactory<>("co_status"));
        col_mobile.setCellValueFactory(new PropertyValueFactory<>("co_mobile"));
        col_type_model.setCellValueFactory(new PropertyValueFactory<> 
        ("co_type_model"));
        col_button.setCellValueFactory(new PropertyValueFactory<>("button"));
        co_tableview.setItems(orderdata);
        co_tableview.getItems().setAll(orderdata);

        co_tableview.itemsProperty().addListener((observable, oldItems, newItems)
            -> {
            countorder.textProperty().bind(
                Bindings.size(newItems).asString());
        });

        // 2. Set the filter Predicate whenever the filter changes.
        search.textProperty().addListener((ObservableValue<? extends String> observable, String oldValue, String newValue) -> {
            filteredData.setPredicate(constractor -> {
                // 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 
(constractor.getCo_name().toLowerCase().contains(lowerCaseFilter)) {
                    return true; // Filter matches first name.
                } else if    (constractor.getCo_id().toLowerCase().contains(lowerCaseFilter)) {
                    return true; // Filter matches last name.
                } else if 
(constractor.getCo_description().toLowerCase().contains(lowerCaseFilter)) {
                    return true; // Filter matches last name.
                }
                return false; // Does not match.
            });
        });

        // 3. Wrap the FilteredList in a SortedList. 
        SortedList<constractor> sortedData = new SortedList<>(filteredData);

        // 4. Bind the SortedList comparator to the TableView comparator.
        sortedData.comparatorProperty().bind(co_tableview.comparatorProperty());

        // 5. Add sorted (and filtered) data to the table.
        co_tableview.setItems(sortedData);
        return 0;

    }

      @FXML
      public void openinsert() {
        try {
//in this fxml i create the new order and also i need for update the status the order from this fxml when i click the button inside the tableview 

            FXMLLoader fxmlLoader = new FXMLLoader();
            fxmlLoader.setLocation(getClass().getResource("createorder.fxml"));
            Scene scene = new Scene(fxmlLoader.load());
            Stage stage = new Stage();
            stage.setTitle("neworder");
            stage.setScene(scene);
            stage.setFullScreen(false);
            stage.setResizable(false);
            stage.setMinHeight(400);
            stage.setMinWidth(600);
            stage.show();

        } catch (IOException e) {
            Logger logger = Logger.getLogger(getClass().getName());
            logger.log(Level.SEVERE, "Failed to create new Window.", e);
        }
    }

通常建议不要将视图代码 (Button) 与模型代码 (constractor) 混合使用。相反,您应该为列使用自定义 TableCell class。

假设您知道如何传递数据(否则请看这里:Passing Parameters JavaFX FXML),所有必需的信息都应该可以通过您应该传递给新场景的 constractor 实例获得。

主屏幕控制器

@FXML
private TableColumn<constractor, Void> col_button;

...

private void editConstractor(constractor constractor) {
    // TODO: implement
}

@FXML
private void initialize() {
    col_button.setCellFactory(col -> new TableCell<constractor, Void>() {

        private final Button button;

        {
            button = new Button("edit");
            button.setOnAction(evt -> {
                constractor item = getTableRow().getItem();
                editConstractor(item);
            });
        }

        @Override
        protected void updateItem(Void item, boolean empty) {
            super.updateItem(item, empty);
            setGraphic(empty ? null : button);
        }
    });
}

您还需要删除按钮列的 cellValueFactory

注:

  • 坚持 java 命名约定会使代码更易于阅读。 (类型名称应以大写字母开头,标识符应使用驼峰式而不是下划线,假设它们不用于 static final 字段。)
  • constractor 很可能拼错了。您是说 contractor 吗?我建议使用 IDE 的重命名功能来修复此拼写错误...
    (在我的代码中,我对 editConstractor 方法使用了相同的拼写。)