美好的一天,我正在尝试使用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");
}
}
非常感谢有关如何完成此任务的任何说明或帮助。 感谢
答案 0 :(得分:2)
为什么不使用gridpane呢?它有助于使用CheckBoxTreeCell类https://docs.oracle.com/javafx/2/ui_controls/tree-view.htm
无论如何,你可以为radiobutton创建自己的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 );
}
}
};
}
}
资源: