2016-07-28 34 views
0

當所選行中的進程結束時,我想禁用/啓用BorderPane下的按鈕。更新按鈕當TableView中的選定進程結束時,JavaFX

我試試這個

downloadTable.getSelectionModel().getSelectedIndices().addListener(new ListChangeListener<Integer>() { 
     @Override 
     public void onChanged(Change<? extends Integer> c) { 
      int selectedIndex = downloadTable.getSelectionModel().getSelectedIndex(); 
      if (downloadTable.getItems().get(selectedIndex).getStatus() == Download.DOWNLOADING) { 
       cancelButton.setDisable(false); 
      } else { 
       cancelButton.setDisable(true); 
      } 

     } 
    }); 

但如果切換到已結束的項目(下載)它纔會起作用。 我想要做的是在選擇某個項目時啓用/禁用按鈕。 感謝所有

example of ended download with cancelButton that I want to disable

+0

你可以顯示錶格的模型類嗎? –

回答

0

也許這樣的事情可以幫助你:

public class Main { 

    private Button someButton; 
    private TableView<?> downloadTable; 

    private void someMethod() { 
     //somecode 
     Callback<TableView<?>, TableRow<?>> baseFactory = downloadTable.getRowFactory(); 
     downloadTable.setRowFactory(new CustomRowFactory<?>(someButton, baseFactory)); 
     //somecode 
    } 

} 

public class CustomRowFactory<T> implements Callback<TableView<T>, TableRow<T>> { 

    private final Callback<TableView<T>, TableRow<T>> baseFactory; 
    private final Button someButton; 

    public CustromRowFactory(Button someButton, Callback<TableView<T>, TableRow<T>> baseFactory) { 
     this.someButton = somButton; 
     this.baseFactory = baseFactory; 
    } 

    @Override 
    public TableRow<T> call(TableView<T> tableView) { 
     final TableRow<T> row = baseFactory == null ? row = new TableRow<>() : row = baseFactory.call(tableView); 
     someButton.disableProperty().bind(
      row.selectedProperty().and(row.getItem().statusProperty().isNotEquals(Download.DOWNLOADING)) 
     ); 
     return row; 
    } 

} 

或插入你的一些TableCell實現的結合。

相關問題