2

I am having a super hard time with this app I am making. The app consists of numbers but it look likes Javafx Text() and Label() only supports strings.

My Problem

        int amount =23;
        Text text = new Text(amount);//ERROR cant not convert in to string.
        Text text2 = new Text(amount.toString();//ERROR int cannot be dereferenced

My Question:

How do I print the number 23 as an actual number not as string; Remember the number needs to be printed as a number int javafx not in the console;

Why

Because latter in the app I will be getting a number from the user and Ineed to add it to: int amount = 23; and I cant add to string together. lol.

Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
Doc Type
  • 95
  • 2
  • 14

3 Answers3

2

UI components like Text handle strings only. You need to convert your int to a String, like

Text text = new Text(Integer.toString(amount));

If you want to add your user input to the original value and update the text to the new value, you should store the original int value, add the value from the user and then update the Text node with the new text.

Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
  • That is great, but how would i modify the int amount =23; latter on. Without using text.setText(); – Doc Type May 04 '16 at 06:06
  • If you want to reflect the modified value in your UI, you will not have an alternative to `setText()` (besides removing the Node and re-adding a new one). What is the problem with `setText()`? – Andreas Fester May 04 '16 at 06:10
1

Different ways to get a String from int...

    Text text = new Text(new Integer(amount).toString());
Text text = new Text(Integer.toString(amount)); // static way

Or use String.valueOf(amount);

or use a StringBuilder

StringBuilder sb = New StringBuilder();
sb.append(amount);
Text text = new Text(sb.toString());
Stefan
  • 2,603
  • 2
  • 33
  • 62
  • Can I change it latter, lets say the user gives me a number 28. I would have to add that value to int amount =23; And I would like to do that without using text.setText(); – Doc Type May 04 '16 at 06:10
  • Of course - all my describe methods show you a dynamic way of creating a String Object from an integer. you just need to pass the calculated amount.. – Stefan May 04 '16 at 06:11
  • May I ask how. I am trying to get the amount from a TextField – Doc Type May 04 '16 at 06:19
  • @DocType I don't know anything about TextField or JavaFX but I do know how to get a String Object of a primitive int. For your own question you should check the javadoc for any methods that may get you the value of Text Object but this has nothing to do with this question here. – Stefan May 04 '16 at 06:27
  • thanks for taking time to answer. Well all the answers work but I love that you gave me multiple options. – Doc Type May 04 '16 at 06:44
1

You can try to use setText() like this:

setText("" +amount);
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • whilst this works the string concatenation will create more String objects temporarily than you need (String is immutable). So this is never a good practice to use the + operator if there are better ways. – Stefan May 04 '16 at 05:57
  • Rahul Stefan is right your answer also works..but there are better ways of doing it. Thank you for taking you time to answer. – Doc Type May 04 '16 at 06:25