0

I am currently working on a calculator app that mimics the windows calculator as a side project and I've run into something I can't figure out.

I have a JTextField that has ComponentOrientation set to RIGHT_TO_LEFT, as so:

    bottomText.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

and the way I've been adding text is as follows:

    bottomText.setText(bottomText.getText() + key.getNumber());

for keys with numbers (0 through 9). The issue is when I try to add a deicmal, like so:

    bottomText.setText(bottomText.getText() + ".");

it shows up like ".123" until I press another key, then it switches to "123.4". What is strange is if i do:

    bottomText.setText(bottomText.getText() + "f");

replacing the decimal with an "f", it shows how it should normally, i.e. "123f", then "123f4". My decimal representation of the number is just fine (stored in a double variable), but it displays strangely in the JTextField. Does anyone know what is happening here?

joshua0823
  • 370
  • 1
  • 10
  • See also this [calculator example](http://stackoverflow.com/a/7441804/418556). It uses `ScriptEngine` to evaluate the expression in the text field. – Andrew Thompson Jun 06 '16 at 18:46

1 Answers1

0

It looks like I should have used

    bottomText.setHorizontalAlignment(SwingConstants.TRAILING);

instead of

    bottomText.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

Thank you Andrew Thompson for your example.

joshua0823
  • 370
  • 1
  • 10