0
I’m doing a program that when entering a value it performs a calculation, but I wanted as I typed the value in Jtextfield to be inserted the points that separates the thousands (eg 1000 to turn 1,000)and I am also using a Keylistener to make the calculation as I enter the values.
I’m trying to do this with the DecimalFormat
, but I am having a problem at the time of performing the calculation, it seems that when the first point is inserted it comes to consider that this number is fractionated.
Another problem that’s happening is when I use the Float
, if I try to do the calculation with the variables being Float, after the value reaches 1000 the point is inserted, after that if I try to add one more number it clears the entire Jtextfield field, leaving only the first number typed on display.
I also tried to use the DecimalFormatSymbols
but I don’t know if I’m doing it right, I’m gonna put a piece of the code here.
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class Teste {
private static float valor;
private static float lucrox;
private static void Calcular() {
//Create and set up the window.
JFrame frame = new JFrame("Teste");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextField preco = new JTextField();
JTextField lucro = new JTextField();
preco.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
DecimalFormat df = new DecimalFormat("###,###,###,###,###");
DecimalFormatSymbols dfs = new DecimalFormatSymbols();
dfs.setGroupingSeparator('.');
df.setDecimalFormatSymbols(dfs);
valor = Float.parseFloat(preco.getText());
lucrox = valor - (valor * 10) / 100;
lucro.setText(String.valueOf(df.format(lucrox)));
preco.setText(String.valueOf(df.format(valor)));
}
});
Container c1 = frame.getContentPane();
c1.setLayout(new BorderLayout());
c1.add(preco, BorderLayout.NORTH);
c1.add(lucro, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
Calcular();
}
});
}
}
Without the DecimalFormat
and the DecimalFormatSymbols
works normally, but without this formatting with the stitches I wanted.
Can someone help me?
From now on, thank you!
Provide a [mcve] so that it is possible to test and execute the code.
– user28595
This example is no longer this piece of code that I put?
– jooj
No, it is not possible to execute this code. I suggest you access the link I sent and read it calmly to understand what is a "Minimum, Complete and Verifiable Example "
– user28595
I edited it, that’s how it is?
– jooj
That has already been answered, it is not possible. There is no separation of thousands into double/float.
– user28595