簡體   English   中英

如何填充TableView中的我可以在javafx中有單選按鈕

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

美好的一天,我正在嘗試使用javafx tableView,也可以通過下圖顯示單選按鈕 在此處輸入圖片說明

注意:單選按鈕將由用戶選擇,而其他數據將從數據庫中獲取。 下圖是到目前為止我已經能夠實現的 在此處輸入圖片說明

借助以下功能。

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

非常感謝您提供有關完成此操作的任何說明或幫助。 謝謝

為什么不使用gridpane呢? 它有助於使用CheckBoxTreeCell類https://docs.oracle.com/javafx/2/ui_controls/tree-view.htm

無論如何,您可以為單選按鈕創建自己的TreeCellFactory。 這將根據需要顯示一個復選框或單選按鈕。

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

資源:

JavaFX:使用單選按鈕制作樹形視圖

JavaFX:單選按鈕+復選框TreeView

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM