I am trying to create a Java windows application using the WindowBuilder plug-in in Eclipse. I have a JTextField that works fine, on entry of the required values. But, when I clear all the contents on the field and try to re-enter other values, the values are disoriented i.e., the values don't appear at all, or seem to appear in the next line.
I'm using a DocumentListener, so yes, this might result in an empty string exception. But, I'm not sure why the contents of the JTextField are inappropriate on re-entry.
PFB my piece-of-code:
void computeTotal(final JTextField jtf){
jtf.getDocument().addDocumentListener(new DocumentListener(){
public void changedUpdate(DocumentEvent e) {
//warn();
getTotal();
}
public void removeUpdate(DocumentEvent e) {
//warn();
getTotal();
}
public void insertUpdate(DocumentEvent e) {
//warn();
getTotal();
}
public void getTotal(){
bt = Float.parseFloat(billTotal.getText());
bd = Float.parseFloat(billDeduction.getText()) ;
ta = Float.parseFloat(taxAmt.getText());
tt = bt - bd+ ta;
totalTotal.setText(tt+"");
}
});
}
What is the problem here?
What is the problem here?
use JFormattedTextField formatted by Number Formatter, then is DocumentListener correct listener
no parsing needed, no issue with nice output, thoushand separator, input of non number chars arenot allowed
use DocumentFilter with Patern, there you can remove non number chars and then you can parse with quite save contens contains numbers, decimal separator and negative sing