button inside column for each row in tableview

回眸只為那壹抹淺笑 提交于 2020-06-28 03:46:37

问题


In my TableView I have column with button for each row for update so I need when click the button to take all the values from the row to a new fxml window

This is my contractor 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;
    }
}

This is my class for 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);
        }
    }

回答1:


It's usually recommended not mixing the view code (Button) with the model code (constractor). Instead you should use a custom TableCell class for the column.

Assuming you know how to pass the data (otherwise take a look here: Passing Parameters JavaFX FXML), all required info should be available via the constractor instance which you should pass to the new scene.

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

You also need to remove the cellValueFactory for the button column.

Note:

  • Sticking to the java naming conventions would make the code easier to read. (Type names should start with an uppercase letter and identifiers should use camelCase instead of underscores assuming they're not for a static final field.)
  • constractor is most likely misspelled. Did you mean contractor? I recommend using the renaming functionality of your IDE to fix this typo...
    (In my code I used the same spelling for the editConstractor method.)


来源:https://stackoverflow.com/questions/51877686/button-inside-column-for-each-row-in-tableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!