tableview中每一行的列内按钮

时间:2018-08-16 12:59:51

标签: uitableview javafx javafx-8 javafx-2

在我的TableView中,每一行都有带有按钮的列以进行更新,因此当单击按钮时,需要将行中的所有值都移到新的fxml窗口中

enter image description here

这是我的承包商班:

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

这是我的课表:

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

1 个答案:

答案 0 :(得分:0)

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

假设您知道如何传递数据(否则请在此处查看:Passing Parameters JavaFX FXML),所有必需的信息都应该通过constractor实例获得,您应该将其传递到新场景。

MainscreenController

@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命名约定将使代码更易于阅读。 (类型名称应以大写字母开头,标识符应使用camelCase而不是下划线,前提是它们不用于static final字段。)
  • constractor很可能是拼写错误的。您是说contractor吗?我建议使用IDE的重命名功能来解决此错字...
    (在我的代码中,我为editConstractor方法使用了相同的拼写。)