0
I’m trying to create a "weight" field, but I’m having some problems.
The weight can vary from 2 to 3 decimal places before the point. For example, it could be 80.00 Kg or 100.00 Kg. However, the way I’m doing it, it forces me to put the 3 houses before the point.
I also tried to put one setText("00.00");
for the field not to "start" only with the point, but it did not pick up. How can I solve this?
public class Peso extends JFrame {
public static void main(String[] args) {
Peso t = new Peso();
t.setVisible(true);
}
CampoPeso peso = new CampoPeso();
JPanel painel = new JPanel();
public Peso() {
JLabel label = new JLabel("Peso:");
painel.add(label);
painel.add(peso);
add(painel);
setSize(220, 100);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
class CampoPeso extends JFormattedTextField {
public CampoPeso() {
setColumns(5);
setText("00.00");
try {
MaskFormatter mf = new MaskFormatter("###.##");
mf.install(this);
} catch (Exception e) {
e.printStackTrace();
}
}
//para pegar o valor
public Float getValor() {
return new Float(getText().replace(".", "").replace(",", "."));
}
//setar o valor
public void setValor(Object valor) {
setText(valor.toString());
}
}
It’s unclear what you want to solve. What’s the problem with the code?
– user28595
@The mask always forces me to put the 3 houses
– Javinha
@Articuno is that I do not know how it leaves "dynamic",if I put 2, only goes up to 99kg for example.
– Javinha
If the goal is to limit the maximum size and not set a fixed size, then the answer of this duplicate solves the problem.
– user28595
the more I can use with the mask ? I wanted to create the field in a way that it is separate, entede ? 85.00 for example, in that answer will only limit the amount
– Javinha
@I understand, complicated, I’m still a beginner, I believe I can not make one in the hand yet. I’ll see which one is better then between the options there.
– Javinha
Has a mask in decimal format, maybe it fits. I just do not know if it forces all fields, I need to test here.
– user28595