2017-06-20 6 views
0

良い一日にラジオボタンを持つことができ、それのどのテーブルビューを移入する方法、私はまた enter image description here私はJavaFXの

NOTE下の画像に代表されるラジオボタン持つことができるの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"); 
     } 
    } 

これをどのように行うかに関する説明や援助に感謝します。 ありがとう

答えて

2

代わりにグリッド区画を使用してみませんか? 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: Making a Tree View with Radio Buttons

JavaFX: Radio Button + CheckBox TreeView

関連する問題