Derin S
Derin S

Reputation: 55

How to populate TableView which in it i can have radiobutton in javafx

Good day, I am trying to have a javafx tableView whereby i can also have radio button represented by the image below enter image description here

NOTE: the radio button will be selected by the user while the other data will be fetched from the database. The below image is what i have been able to achieve so far enter image description here

with the aid of the below function.

private ObservableList<ObservableList> data;
    Connection conn = null;
    ResultSet rs = null;
    PreparedStatement pst = null;

    public void buildData() {
        data = FXCollections.observableArrayList();
        try {
            conn = javaconnect.ConnectDb();
            String SQL = "select id, questions from the_questions";
            ResultSet rs = conn.createStatement().executeQuery(SQL);

            for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
                //We are using non property style for making dynamic table
                final int j = i;
                TableColumn col = new TableColumn(rs.getMetaData().getColumnName(i + 1));
                col.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList, String>, ObservableValue<String>>() {
                    public ObservableValue<String> call(TableColumn.CellDataFeatures<ObservableList, String> param) {
                        return new SimpleStringProperty(param.getValue().get(j).toString());
                    }
                });

                questions.getColumns().addAll(col);
                System.out.println("Column [" + i + "] ");
            }
            while (rs.next()) {
                ObservableList<String> row = FXCollections.observableArrayList();
                for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
                    row.add(rs.getString(i));
                }
                System.out.println("Row [1] added " + row);
                data.add(row);

            }

            questions.setItems(data);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Error on Building Data");
        }
    }

Will so much appreciate any illustration or assistance on how to get this done. Thanks

Upvotes: 0

Views: 639

Answers (1)

user4887078
user4887078

Reputation:

Why not use gridpane instead? And it helps use CheckBoxTreeCell class https://docs.oracle.com/javafx/2/ui_controls/tree-view.htm

Anyway you can create your own TreeCellFactory for radiobutton. This will display a checkbox or a radiobutton as needed.

public class TreeCellFactory implements Callback<TreeView<Object>,TreeCell<Object>>
{
    @Override
    public TreeCell call( TreeView param )
    {
        return new TreeCell<Object>()
        {
            private final CheckBox  check = new CheckBox();
            private final RadioButton  radio = new RadioButton();
            private Property<Boolean>  prevRadioProp;
            {
                setContentDisplay( ContentDisplay.GRAPHIC_ONLY );
            }

            @Override
            public void updateItem( Object item, boolean empty )
            {
                if ( prevRadioProp != null )
                {
                    radio.selectedProperty().unbindBidirectional( prevRadioProp );
                    prevRadioProp = null;
                }
                check.selectedProperty().unbind();

                if ( ! empty && item != null )
                {
                    Property<Boolean> selectedProp = ....;

                    if ( getTreeItem().isLeaf() )  // display radio button
                    {
                        radio.setText( ... );
                        radio.selectedProperty().bindBidirectional( selectedProp );
                        prevRadioProp = selectedProp;
                        setGraphic( radio );
                    }
                    else                          // display checkbox
                    {
                        check.setText( ... );
                        check.selectedProperty().bind( selectedProp );
                        setGraphic( check );
                    }
                }
                else
                {
                    setGraphic( null );
                    setText( null );
                }
            }
        };
    }
}

Resources:

JavaFX: Making a Tree View with Radio Buttons

JavaFX: Radio Button + CheckBox TreeView

Upvotes: 2

Related Questions