Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

So I'm trying to add a ChoiceBox for every variable inserted in the TableView.

Users.java:

 Users(String userName , Integer userAge , ChoiceBox employed)
{
    this.userName = userName ;
    this.userAge = userAge ;
    this.employed= employed;
}
//getters and setters

Main.java:

ObservableList<Users>userList = FXCollections.observableArrayList();
TableView<Users> userTable = new TableView();
TableColumn<Users, String> nameCol = new TableColumn();
TableColumn<Users, Integer> ageCol = new TableColumn();

TableColumn<Users, ChoiceBox> employCol = new TableColumn();

private ChoiceBox createBox(){ 
ChoiceBox box = new ChoiceBox();
box.getItems.addAll("true" , "false");
box.setValue("true"); 
return box;
}


userList.addAll(new User("James" , 47 , createBox()));

Everything compiles and runs just fine, but I have no way of getting a reference to the box that is clicked by the user. I tried getting it via selection model:

userTable.getSelectionModel().getSelectedItem().employed;

but this just gives me a NullPointerException. I need to be able to select the choice box, if the user selects "true" , then it would display in a text field, but that's an easy fix, I'm having trouble actually getting the instance to the choicebox. I've referred to:

Getting selected item from a JavaFX TableView

javafx: attach ChoiceBox to a TableCell on a TableColumn during Edit

ChoiceBox with custom Item in a TableView

But to no avail. I've also tried using multiple variations of

ChoiceBoxTableCell

and

ChoiceBoxTableList

but these won't even compile, I keep getting a callback error.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
128 views
Welcome To Ask or Share your Answers For Others

1 Answer

Callback<TableColumn<Users, String>, TableCell<Users, String>> cellFactory
            = //
            new Callback<TableColumn<Users, String>, TableCell<Users, String>>() {
        @Override
        public TableCell call(final TableColumn<Users, String> param) {
            final TableCell<Users, String> cell = new TableCell<Users, String>() {

                final ChoiceBox createBox(){ 
ChoiceBox box = new ChoiceBox();
box.getItems.addAll("true" , "false");
box.setValue("true"); 

                @Override
                public void updateItem(String item, boolean empty) {
                    super.updateItem(item, empty);
                    if (empty) {
                        setGraphic(null);
                        setText(null);
                    } else {
                        setGraphic(createBox);
                        setText(null);
                    }
                }
            };
            return cell;
        }
    };

    employCol.setCellFactory(cellFactory);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...