2016-07-28 33 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实现的结合。

相关问题