JavaFX Slider compile time error

H

Hiram Hunt

Hello,
I have some code that gets compile time errors in
some places but not others. I don't understand why
one situation is apparently legal and the other is not.
Here is the copy-and-paste of a minimal example:

/*
* Program: Minimal
* A minimal case of some code that is causing
* compile time errors. Compile with Java 7u3 and
* use JavaFX 2.0 library (jfxrt.jar) on 64 bit
* Windows 7.
*/

import javafx.geometry.Orientation;
import javafx.scene.control.Slider;
import javafx.scene.control.SliderBuilder;

class Minimal {

// No compile error on okslider1.

Slider okslider1 = SliderBuilder
.create()
.prefWidth(100.0)
.orientation(Orientation.HORIZONTAL)
.build();

// Swap width and orientation calls; still no compile error.

Slider okslider2 = SliderBuilder
.create()
.orientation(Orientation.HORIZONTAL)
.prefWidth(100.0)
.build();

// Create a SliderBuilder with the setting of several
// properties omitted for the sake of a minimal example.

SliderBuilder sliderbuilder = SliderBuilder
.create();

// Use the SliderBuilder I just made to make a Slider.
// Compile error for badslider1: cannot find method
// .orientation(Orientation) in class ControlBuilder.

Slider badslider1 = sliderbuilder
.prefWidth(100.0)
.orientation(Orientation.HORIZONTAL)
.build();

// Swap width and orientation calls.
// Compile error for badslider2: cannot find method
// .build() in class ControlBuilder. Thus, comparing
// with badslider1 case, the problem seems to be with
// using the result of .prefWidth(), but why doesn't
// the same problem appear when I make okslider1
// and okslider2?

Slider badslider2 = sliderbuilder
.orientation(Orientation.HORIZONTAL)
.prefWidth(100.0)
.build();
}

(End copy-and-paste.)
Can anyone explain this behavior? In particular, I don't just
want to know why the errors occur, but why they don't also occur
in the cases of okslider1 and okslider2. Thanks.

-- Hiram Hunt
 
M

markspace

.prefWidth(100.0)
.build();


::prefWidth() is declared in the BuildControl<B> class, and has as Lief
pointed out a generic return type. Its ::create() factory method does
return a type of SliderBuilder<?>, so that might be your best best for
your variable sliderBuilder.

If that doesn't work, try SliderBuilder<SliderBuilder> as the type of
sliderBulder. It seems to be to fit more closely with the declared
intent (bound) of the type parameter.
 
H

Hiram Hunt

Leif Roar Moldskred said:
I don't have a Java development environment at hand, but
it looks like a problem with generics.


Try "SliderBuilder<?> sliderbuilder =" instead.

Thanks Leif and Markspace. Using <?> worked both in the minimal
example and in the program I trimmed down to make the example.
Using SliderBuilder<SliderBuilder> didn't, but thanks also.
I understand some of how generic work, but not all. I assume
that some type information that is available in the expressions
that initialize okslider1 and okslider2 is lost by not including
the <?> in "SliderBuilder<?> sliderbuilder".

-- Hiram Hunt
 
L

Lew

Thanks Leif and Markspace. Using<?> worked both in the minimal
example and in the program I trimmed down to make the example.
Using SliderBuilder<SliderBuilder> didn't, but thanks also.
I understand some of how generic work, but not all. I assume
that some type information that is available in the expressions
that initialize okslider1 and okslider2 is lost by not including
the<?> in "SliderBuilder<?> sliderbuilder".

Don't assume, research.

Generics without generic parameters (the angle-bracket part) are known as
"raw" types, and exist only for backward compatibility. They are an error,
shown as a warning by compilers in lenient mode.

You should regard generic parameters as mandatory in generic types.

The wildcard '?' is a dodge to get around not knowing the type at compile
time. If you know what the base (generic) type should be, you should specify it.

It would be ridiculous for the base type of 'SlicerBuilder' to be
'SliderBuilder', wouldn't it?

Admit it, you were just throwing shit at the wall to see what would stick,
weren't you?

Read the angle brackets as "of":

'SliderBuilder<Foo>' is "'SliderBuilder' of 'Foo'".

What is 'SliderBuilder', anyway?

Show a link to the Javadocs for it (which *surely* you've studied - right?),
please.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top