मेरे पास JFrame
ऑब्जेक्ट पर दो JFormattedTextField
ऑब्जेक्ट्स हैं। मैं इन JFormattedTextField
ऑब्जेक्ट्स के मानों द्वारा मूलभूत गणित (अतिरिक्त) चाहता हूं। मैं चाहता हूं कि यह तब होता है जब फोकस पहले या दूसरे टेक्स्टफील्ड को खो देता है। लेकिन जब "focusLost()
", ईवेंट को अंतिम मान नहीं मिलता है, तो यह पिछले मान प्राप्त करता है।FocusEvent को JFormattedTextField का अंतिम मान नहीं मिलता है, मैं इसे कैसे प्राप्त कर सकता हूं?
उदाहरण के लिए; tf1
में 0 और tf2
पहले 0 हैं। मैं tf1
करने के लिए 2 लिखते हैं, और जब focusLost()
, परिणाम (tf1+tf2
) अभी भी बन 0. जब मैं उनमें से किसी को बदलने के लिए, परिणाम 2 (पिछले मान)
हो जाता है मैं focusLost पर पिछले मूल्यों कैसे मिलता है?
JFormattedTextField tf1,tf2;
NumberFormat format=NumberFormat.getNumberInstance();
tf1=new JFormattedTextField(format);
tf1.addFocusListener(this);
tf2=new JFormattedTextField(format);
tf2.addFocusListener(this);
और focusLost()
:
यहाँ मेरी कोड है
public void focusLost(FocusEvent e) {
if(tf1.getValue() == null) tf1.setValue(0);
if(tf2.getValue() == null) tf2.setValue(0);
//because if I dont set, it throws nullPointerException for tf.getValue()
BigDecimal no1 = new BigDecimal(tf1.getValue().toString());
BigDecimal no2 = new BigDecimal(tf2.getValue().toString());
System.out.println("total: " + (no1.add(no2)));
}
अच्छा धागा, तीन संभावित श्रोताओं की संपत्ति, दस्तावेज़ और फोकस +1 – mKorbel
+1 मुझे लगता है कि यह बेहतर दृष्टिकोण है। – trashgod