4

I am writing part of my program which read data from a txt file.

I have problem when I want to set value of JSpinner by setValue(object).

My data is double so I need to convert it to object, but how?

LoadData open = new LoadData();
data.setFz(open.giveAwayData());
spinner_1.setValue(data.getFz()); // Fz is double
eric.m
  • 1,577
  • 10
  • 20
Xalion
  • 623
  • 9
  • 27
  • 2
    I've just tried it and I can use a double as the argument, with no casts nor conversions ;) – eric.m Aug 29 '15 at 19:15
  • I restared eclips and everything is ok now. Weird bug. – Xalion Aug 29 '15 at 20:04
  • Unfortunately, it's not as weird a bug as you think. Eclipse has a tendency to lose its mind every now and then, and you have to restart. It happens far too often. You just have to get used to it (like I do), or switch to IntelliJ (which I hear is a little more stable, but I'm not sure). – ajb Aug 29 '15 at 23:41

3 Answers3

3

Try casting it to Double (with capital):

spinner_1.setValue((Double) data.getFz());
  • 1
    If this is an answer, it shouldn't end with a question mark. :) Also, showing the code would be really helpful. I think this is the right answer, but you just need a little more to make it a good answer. – ajb Aug 29 '15 at 19:13
  • Ideally this would be a comment but I can't comment yet so deal with it. But I'll edit it as to your suggestions. – colouredmirrorball Aug 29 '15 at 19:15
  • I dont understand what mean with capital. Give some code, please. – Xalion Aug 29 '15 at 19:18
  • @Xalion "capital" means upper-case. Java distinguishes between `double`, which is a primitive type and not an object, and `Double`, which is an object. `Double` begins with an upper-case (capital) D. Often Java will automatically convert between the two (boxing and unboxing). – ajb Aug 29 '15 at 19:21
  • spinner_1.setValue((Object) data.getFz()); it should look like that, but it doesnt work. – Xalion Aug 29 '15 at 19:51
  • @Xalion Does your IDE specifically say that it's the wrong parameter type? If so, your method doesn't _actually_ have a parameter typed `Object` at all. Otherwise, you must be mistaking which bit of your code is erroneous. – bcsb1001 Aug 29 '15 at 19:58
  • @bcsb1001 The parameter type is `Object`; I looked up `JSpinner` to confirm this. Apparently the IDE had lost its mind. – ajb Aug 29 '15 at 23:39
3

Use Double.valueOf(data.getFz()). An explanation of the differences between primitives and boxed primitives can be found here; basically, primitives have better performance but the boxed classes are there for when you need an Object. A common example is with generics, which cannot be primitives.

However, a double can be passed to something requiring a Double or one of its superclasses. This is because of autoboxing. Yes, I like links a lot.

Community
  • 1
  • 1
bcsb1001
  • 2,834
  • 3
  • 24
  • 35
  • it doesnt work... i need OBJECT, not double. it is setValue(object) – Xalion Aug 29 '15 at 19:52
  • Like I said, autoboxing should solve that problem for you. Also, `Double` is a subclass of `Object`. But you can always use `Object obj = data.getFz();` (autoboxing lets that work). – bcsb1001 Aug 29 '15 at 19:53
  • Never use `new Double`. Always use [Double.valueOf](https://docs.oracle.com/javase/8/docs/api/java/lang/Double.html#valueOf-double-) instead. – VGR Aug 29 '15 at 19:57
  • @VGR, yeah, forgot that was in `Double` too. – bcsb1001 Aug 29 '15 at 19:59
2

You can use for example the Double constructor to convert double primitive type to a Double object.

Double dObj = new Double(d);

or you can cast it to Double like this:

spinner_1.setValue((Double)data.getFz());
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331