This question is answered.


Permlink Replies: 3 - Pages: 1 - Last Post: 20-Oct-2018 19:45 Last Post By: davidekholm
AndreWolff

Posts: 1,841
Registered: 14-Dec-2007
How to display an empty string if the contents of a JNumberField==0?
Posted: 18-Oct-2018 09:29
 
  Click to reply to this thread Reply
I declare a JNumberField in onload.bsh as follows:

JTextField imgThemeWidth = new JnumberField("",4);

and displays it with code

indexTheme.add(ui.imgThemeWidth);

In file SkinModel.java is is declared as:

public int imgThemeWidth=0;
This is required because this variable is used in init.bsh

I use an empty string as indication 'Not used', but if I enter an empty string, it is later displayed as 0 in the ui.imgThemeWidth field.

I don't like that, so I tried to replace the 0 string by an empty string:
if (ui.imgThemeWidth.getText=="0") ui.imgThemeWidth.setText="";
 indexTheme.add(ui.imgThemeWidth);
But that does not work.

What is the correct code to replace the 0 string by an empty string?
davidekholm

Posts: 3,658
Registered: 18-Oct-2002
Re: How to display an empty string if the contents of a JNumberField==0?
Posted: 20-Oct-2018 19:08   in response to: AndreWolff in response to: AndreWolff
Correct
  Click to reply to this thread Reply
You can't compare strings in Java with ==. The == operation is for comparing primitives like ints, chars and floats and for comparing reference identity, not for comparing strings. Use equals() to compare strings.
AndreWolff

Posts: 1,841
Registered: 14-Dec-2007
Re: How to display an empty string if the contents of a JNumberField==0?
Posted: 20-Oct-2018 19:40   in response to: davidekholm in response to: davidekholm
 
  Click to reply to this thread Reply
Thanks for the lesson!
davidekholm

Posts: 3,658
Registered: 18-Oct-2002
Re: How to display an empty string if the contents of a JNumberField==0?
Posted: 20-Oct-2018 19:45   in response to: AndreWolff in response to: AndreWolff
 
  Click to reply to this thread Reply
You're welcome. I admit Java is a bit low-level in this regard. They've made one change for simplicity and that is that you can now compare strings in switch cases.
Legend
Forum admins
Helpful Answer
Correct Answer

Point your RSS reader here for a feed of the latest messages in all forums