2014-07-11 4 views
0

제 작업을위한 프로젝트를 만들고 있습니다. 테이블 뷰와 사용자가 데이터베이스로가는 데이터를 검사 할 필요가 있습니다. 나는이 나를 위해 노력하고 있습니다이 게시물 https://stackoverflow.com/a/7973514/3037869 에서의 TableView에 체크 박스를 추가하지만 내있는 TableView 지금 어떻게이 확인란 스팸을 해결하는 enter image description here왜 TableView의 모든 행에 체크 박스가 있습니까?

을보고하는 방법을 발견했다? 도움을 내 코드

public class ProductPSController extends BorderPane{ 
    @FXML public TableColumn<ProductPS, String> produkt; 
    @FXML public TableColumn<ProductPS, String> symbol; 
    @FXML public TableColumn<ProductPS, String> atrybuty; 
    @FXML public TableColumn<ProductPS, Integer> id; 
    @FXML public TableColumn<ProductPS, Integer> stock; 
    @FXML public TableColumn<ProductPS, Integer> count; 
    @FXML public TableColumn<ProductPS, Integer> price; 
    @FXML public TableColumn<ProductPS, Boolean> checkbox; 
    @FXML public TableView <ProductPS> tab; 
    @FXML public BorderPane produkty; 
    public ObservableList<ProductPS> data = FXCollections.observableArrayList(); 
    public ProductPSController() 
    { 
     FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("product.fxml")); 
     fxmlLoader.setRoot(this); 
     fxmlLoader.setController(this); 

     try { 
      fxmlLoader.load();    
     } catch (IOException exception) { 
      throw new RuntimeException(exception); 
     } 
     id.prefWidthProperty().bind(tab.widthProperty().multiply(0.10)); 
     stock.prefWidthProperty().bind(tab.widthProperty().multiply(0.10)); 
     produkt.prefWidthProperty().bind(tab.widthProperty().multiply(0.20)); 
     symbol.prefWidthProperty().bind(tab.widthProperty().multiply(0.10)); 
     atrybuty.prefWidthProperty().bind(tab.widthProperty().multiply(0.30)); 
     count.prefWidthProperty().bind(tab.widthProperty().multiply(0.10)); 
     price.prefWidthProperty().bind(tab.widthProperty().multiply(0.10)); 
     tab.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY); 
     setProduct(); 
    } 
    public void setProduct() 
    { 
     id.setCellValueFactory(new PropertyValueFactory<ProductPS, Integer>("id")); 
     symbol.setCellValueFactory(new PropertyValueFactory<ProductPS, String>("symbol")); 
     stock.setCellValueFactory(new PropertyValueFactory<ProductPS, Integer>("id_stock")); 
     produkt.setCellValueFactory(new PropertyValueFactory<ProductPS, String>("product_name")); 
     atrybuty.setCellValueFactory(new PropertyValueFactory<ProductPS, String>("attributes")); 
     count.setCellValueFactory(new PropertyValueFactory<ProductPS, Integer>("count")); 
     Callback<TableColumn<ProductPS, Boolean>, TableCell<ProductPS, Boolean>> booleanCellFactory = 
       new Callback<TableColumn<ProductPS, Boolean>, TableCell<ProductPS, Boolean>>() { 
       @Override 
        public TableCell<ProductPS, Boolean> call(TableColumn<ProductPS, Boolean> p) { 
         return new BooleanCell(); 
       } 
      }; 
     price.setCellValueFactory(new PropertyValueFactory<ProductPS, Integer>("price")); 
     checkbox.setCellValueFactory(new PropertyValueFactory<ProductPS, Boolean>("checkbox")); 
     checkbox.setCellFactory(booleanCellFactory); 
     checkbox.setEditable(true); 
     try(Connection c = MysqlConnect.getConnection()) 
     { 
      String SQL = ""; 
      ResultSet rs = c.createStatement().executeQuery(SQL); 
      while(rs.next()){ 
       data.add(new ProductPS(rs.getInt("id_product"),rs.getInt("id_stock_available"),rs.getString("name"),rs.getString("atrybuty"),rs.getInt("quantity"),rs.getFloat("price"),rs.getString("reference"))); 


       } 
      c.close(); 
     } catch (SQLException e) { 
      System.out.println(e.toString()); 
      // TODO Auto-generated catch block 

     } 
     for(int i=0; i<data.size(); i++) { 
      if(i+1<data.size() && data.get(i).getAttributes().length()==0 && data.get(i).getId()==data.get(i+1).getId()){data.remove(i);} 
     } 

     tab.setItems(data); 
    } 
    class BooleanCell extends TableCell<ProductPS, Boolean> { 
     private CheckBox checkBox; 
     public BooleanCell() { 
      checkBox = new CheckBox(); 
      checkBox.setDisable(false); 

      checkBox.selectedProperty().addListener(new ChangeListener<Boolean>() { 
       public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) { 
        if(isEditing()) 
        { 
         commitEdit(newValue == null ? false : newValue); 
        } 
       } 
      }); 
      this.setGraphic(checkBox); 
      this.setContentDisplay(ContentDisplay.GRAPHIC_ONLY); 
      this.setEditable(true); 
     } 
     @Override 
     public void startEdit() { 
      super.startEdit(); 
      if (isEmpty()) { 
       return; 
      } 
      checkBox.setDisable(false); 
      checkBox.requestFocus(); 
     } 
     @Override 
     public void cancelEdit() { 

      super.cancelEdit(); 
      checkBox.setDisable(true); 
     } 
     public void commitEdit(Boolean value) { 
      super.commitEdit(value); 
      checkBox.setDisable(true); 
     } 
     @Override 
     public void updateItem(Boolean item, boolean empty) { 
      super.updateItem(item, empty); 
      if (!isEmpty()) { 
       checkBox.setSelected(item); 
      } 
     } 
    } 
} 

들으와 내가 나쁜 무슨 짓을 말한다.

답변

1

BooleanCell에서 셀이 비어 있지 않으면 그래픽을 checkBox으로 설정하고 비어 있으면 null으로 설정해야합니다.

BooleanCell의 생성자에서 라인

this.setGraphic(checkBox); 

을 제거하려면 updateItem(...) 변경 : 내가 잘못이 무엇인지 어떤 체크 박스가 표시되지

@Override 
public void updateItem(Boolean item, boolean empty) { 
    super.updateItem(item, empty); 
    if (empty) { 
     setGraphic(null); 
    } else { 
     checkBox.setSelected(item); 
     setGraphic(checkBox); 
    } 
} 
+0

에 updateItem 변경 후? – seti

+0

var 항목을 검사하고 모든 행에 대해 empty가 true이고 item이 null입니다. 다른 사람에게 절대로 가지 마라. 오류가 어디 있습니까? – seti

+0

set 및 get 메소드의 잘못된 이름을 찾을 수 없습니다. 체크 박스 체크 박스의 행을 얻는 방법? – seti

관련 문제