0

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.

1 Answers1

0
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);
Elarbi Mohamed Aymen
  • 1,617
  • 2
  • 14
  • 26
  • While I appreciate the SSCCE greatly, I'd much rather know the "why" behind the solution. Another thing is, does this bind a choicebox to every "User" object created in the list? If so, how do we access it? –  Mar 28 '18 at 20:22
  • can you try to explain more, i could not understand what is you question. – Elarbi Mohamed Aymen Mar 28 '18 at 20:45
  • I need to return the value of the selected choicebox in the table cell. `userList.addAll(new User("James" , 47 , createBox())); ` right where createBox() is, how can I get the t/f value of it when it is selected in the table –  Mar 28 '18 at 21:24
  • 1
    dont re-invent the wheel, fx has a ChoiceBoxTableCell ;) – kleopatra Mar 29 '18 at 03:50