I need to convert from inches to centimeters and vice versa. The idea is to have a single button "convert" and that according to the field that has just lost focus, I will see the conversion in the other JtextField
the result.
Example: I wrote a value in the field centimeters, and it has to appear in the field inches, and the same thing if I work the other way around.
The problem is that I can not do it by asking in the action performed
of Jbutton
the condition if(CampoPulgadas.isFocusOwner)
.
I would appreciate the help.
private void ConvertirBotonActionPerformed(java.awt.event.ActionEvent evt) {
double convertido = 0;
if(!CampoCentimetros.isFocusOwner())
{
String cen = CampoCentimetros.getText();
try{
convertido = Double.parseDouble(cen);
}
catch(NumberFormatException ex){
JOptionPane.showMessageDialog(this, "El valor "+convertido+" no es válido!", "ERROR", JOptionPane.ERROR_MESSAGE);
return;
}
convertido /= 2.54;
cen = String.format("%.4f",convertido);
CampoPulgadas.setText(cen);
return;
}
if(CampoPulgadas.isFocusOwner())
{
} else {
String pulg = CampoPulgadas.getText();
try{
convertido = Double.parseDouble(pulg);
}
catch(NumberFormatException e){
JOptionPane.showMessageDialog(this, "El valor "+convertido+" no es válido!", "ERROR", JOptionPane.ERROR_MESSAGE);
return;
}
convertido *= 2.54;
pulg = String.format("%.4f", convertido);
CampoCentimetros.setText(pulg);
return;
}
}