2013-05-01 3 views
0

변수 값이 null 인 경우 현재 반복을 취소하려는 동작 수신기가 있습니다.ActionListener를 취소시킬 수있는 방법이 있습니까?

public class ValidateListener implements ActionListener { 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     myCalendarKey mCal = Project5.verifyDate(month+"/"+day+"/"+year); //month day and year are defined, just not in this code display. 

     if (mCal == null) e.cancel(); //I need something to cancel the current ActionEvent if this returns true. 

     /* A lot more code down here that only works if mCal is defined */ 
    } 
} 

나는 나는 경우-else 문을 사용하고 mCal != null 경우 모든 것을 할 수 있도록하고, mCal == null 경우 아무것도 할 수도 있겠죠,하지만이 할 수있는 더 좋은 방법은 무엇입니까?

+0

* "이 작업을 수행하는 더 좋은 방법이 있습니까?"* 아무런 문제가 없습니다. 또는, 값이 'null'일 때 컨트롤 또는'Action'을 비활성화하십시오. 그러면 이벤트가 첫 번째 위치에서 실행되지 않습니다! –

+0

@Andrew 위의 접근 방법은 동작을 취소하지 않습니다 (이는 내가 잘못되었을 수도 있음). 이 이벤트 리스너의 처리를 중지합니다. – StuPointerException

+0

@StuPointerException * "내가 틀렸을지라도"* 그렇습니다. & OP가 아닌 사람들의 추측에 특히 관심이 없습니다. –

답변

0

이 시도 :이 이벤트를 소비 할

e.consume() 

:

@Override 
public void actionPerformed(ActionEvent e) { 
    myCalendarKey mCal = Project5.verifyDate(month+"/"+day+"/"+year); 
    if(mCal != null) { 
     /* A lot more code down here that only works if mCal is defined */ 


    } 
} 
+0

이것은 액션 리스너의 로직을 멈추지 만, 이벤트를 취소하지는 않을 것입니다. – StuPointerException

0
나는 그것이 더 나은 언급하지 않았다

,하지만 당신도 이것을 할 수 있습니다.

@Override 
public void actionPerformed(ActionEvent e) { 
    myCalendarKey mCal = Project5.verifyDate(month+"/"+day+"/"+year); 
    if(mCal != null) return; 
    ... 
} 
관련 문제