What is the difference between getText () and getSelection () SWT Spinner?
For control Spinner
in SWT, what is the difference between getText()
and getSelection()
? The only thing I can make out is the return type. However, I cannot enter any text, only numbers are allowed.
GetText (): Returns a string containing a copy of the content of the recipient's text box, or an empty string if there is no content.
GetSelection (): Returns a selection that is the position of the receiver.
So why do we have two different getters, and which one should I use?
source to share
Since it Spinner
is a numeric selection, you have to go with getSelection()
, since it returns exactly what was selected.
I don't know why it getText()
exists, but it will return a representation of the String
selected value, eg. if it getSelection()
returns int
7
, it getText()
will return String
"7"
.
source to share
As stated in the documentation, getText()
returns the string as if it were currently displayed, including possible non-numeric values or values outside the min / max range. getSelection()
returns the current or last valid integer value.
You usually want to use getSelection()
. Does this answer your question?
source to share
-
getText
in particular says that it can be empty (I'm guessing it's platform dependent if that happens). It then distinguishes this case from the value 0. -
You can use
getText
when usedSpinner
with decimal digits (see http://help.eclipse.org/luna/topic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/swt/widgets/Spinner.html #setDigits (int) ), sincegetSelection
in this case it is further from the value of the value. But note that the decimal separator depends on the current locale.
source to share