Technically speaking, the coins are spendable immediately; they'll just take longer to spend and cost more in transaction fees. But to know when the coins are confirmed, in onCoinsRecieved:
If you want to know when such a transaction receives its first confirmation, register a TransactionConfidence event listener using the object retrieved via Transaction.getConfidence(). It's safe to modify the wallet in this callback, for example, by spending the transaction just received.
(Source.)
Use something like this:
public void onCoinsReceived(Wallet wallet,
Transaction tx,
Coin prevBalance,
Coin newBalance) {
TransactionConfidence confidence = Transaction.getConfidence();
confidence.addEventListener(new TransactionConfidence.Listener() {
public void onConfidenceChanged(Transaction tx,
TransactionConfidence.Listener.ChangeReason reason) {
if(reason == TransactionConfidence.Listener.ChangeReasonDefaultCoinSelector.DEPTHisSelectable(transaction) {
System.out.println("An incoming transaction was just seen in at// leastdo onesomething block.");here
}
}
});
}
Note: code is untested.
EDIT
This also works:
override def onCoinsReceived(
wallet: Wallet,
transaction: Transaction,
prevBalance: Coin,
newBalance: Coin
) {
...
transaction.getConfidence().addEventListener(new TransactionConfidence.Listener() {
def onConfidenceChanged(confidence: TransactionConfidence, reason: TransactionConfidence.Listener.ChangeReason) {
DefaultCoinSelector.isSelectable(transaction) {
// spend here
}
}
}
}