|
zigizig Aleksandar Beograd
Član broj: 42968 Poruke: 113 *.adsl-1.sezampro.yu.
|
Mozda nesto ovako
txtField.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent e) {
if (e.getKeyChar() == '.'
&& Logic.getInstance().twoDotsDisable(
txtField.getText()))
e.consume();
if (e.getKeyChar() != e.VK_BACK_SPACE
&& Logic.getInstance().twoDecimalMax(
txtField.getText()))
e.consume();
if (!('0' <= e.getKeyChar() && e.getKeyChar() <= '9')
&& e.getKeyChar() != e.VK_BACK_SPACE
&& e.getKeyChar() != '.') {
e.consume();
}
}
}
a u Logic klasi
public class Logic {
private static Logic instance = null;
public static Logic getInstance() {
if (instance == null)
instance = new Logic();
return instance;
}
public double roundDouble(double d, int places) {
return Math.round(d * Math.pow(10, (double) places))
/ Math.pow(10, (double) places);
}
public boolean twoDotsDisable(String text) {
if (text.contains("."))
return true;
else
return false;
}
public boolean twoDecimalMax(String text) {
if (text.contains(".")) {
int index = text.lastIndexOf(".");
String ostatak = text.substring(index + 1);
if (ostatak.length() >= 2)
return true;
else
return false;
} else
return false;
}
}
|