I am having an issue with negation with Swing, for some reason my Big Decimal negation and addition does not work, my code compiles but the minus and plus calculations are not working, any help is most appreciated.
Code Snippet
//Convert the JLabel to a Double so we can perform negation.
diallerPanelSum = new BigDecimal(balanceAmount.getText());
//Dont allow the Balance to go negative!
if(diallerPanelSum.compareTo(BigDecimal.ZERO)>0)
{
if(e.getSource()==buttonMakeCall)
{
diallerPanelSum.subtract(new BigDecimal("1.0"));
}
if(e.getSource()==buttonSendText)
{
diallerPanelSum.subtract(new BigDecimal("0.10"));
}
if(e.getSource()==buttonTopUp)
{
diallerPanelSum.add(new BigDecimal("10.00"));
}
}
//Convert the Float back to a JLabel
balanceAmount.setText(String.valueOf(diallerPanelSum));
BigDecimals
are immutable. So you must assign the results of the operations like add()
or subtract()
to a BigDecimal
again, as they produce a new BigDecimal
. Try this instead:
if (diallerPanelSum.compareTo(BigDecimal.ZERO) > 0)
{
if (e.getSource() == buttonMakeCall)
{
diallerPanelSum = diallerPanelSum.subtract(BigDecimal.ONE);
}
if (e.getSource() == buttonSendText)
{
diallerPanelSum = diallerPanelSum.subtract(new BigDecimal("0.10"));
}
if (e.getSource() == buttonTopUp)
{
diallerPanelSum = diallerPanelSum.add(BigDecimal.TEN);
}
}