JavaFX Slider compile time error

Discussion in 'Java' started by Hiram Hunt, May 5, 2012.

  1. Hiram Hunt

    Hiram Hunt Guest

    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
     
    Hiram Hunt, May 5, 2012
    #1
    1. Advertising

  2. Hiram Hunt

    markspace Guest

    On 5/5/2012 1:19 PM, Hiram Hunt wrote:

    > .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.
     
    markspace, May 5, 2012
    #2
    1. Advertising

  3. Hiram Hunt

    Hiram Hunt Guest

    "Leif Roar Moldskred" <> wrote in message
    news:...
    > Hiram Hunt <> wrote:
    >> 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:

    >
    > I don't have a Java development environment at hand, but
    > it looks like a problem with generics.
    >
    >> // Create a SliderBuilder with the setting of several
    >> // properties omitted for the sake of a minimal example.
    >>
    >> SliderBuilder sliderbuilder = SliderBuilder
    >> .create();

    >
    > 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
     
    Hiram Hunt, May 5, 2012
    #3
  4. Hiram Hunt

    Lew Guest

    On 05/05/2012 02:53 PM, Hiram Hunt wrote:
    > "Leif Roar Moldskred"<> wrote in message
    > news:...
    >> Hiram Hunt<> wrote:
    >>> 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:

    >>
    >> I don't have a Java development environment at hand, but
    >> it looks like a problem with generics.
    >>
    >>> // Create a SliderBuilder with the setting of several
    >>> // properties omitted for the sake of a minimal example.
    >>>
    >>> SliderBuilder sliderbuilder = SliderBuilder
    >>> .create();

    >>
    >> 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".


    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.

    --
    Lew
    Honi soit qui mal y pense.
    http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg
     
    Lew, May 6, 2012
    #4
  5. Hiram Hunt

    Lew Guest

    On 05/05/2012 09:03 PM, Lew wrote:
    > On 05/05/2012 02:53 PM, Hiram Hunt wrote:
    >> "Leif Roar Moldskred"<> wrote in message
    >> news:...
    >>> Hiram Hunt<> wrote:
    >>>> 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:
    >>>
    >>> I don't have a Java development environment at hand, but
    >>> it looks like a problem with generics.
    >>>
    >>>> // Create a SliderBuilder with the setting of several
    >>>> // properties omitted for the sake of a minimal example.
    >>>>
    >>>> SliderBuilder sliderbuilder = SliderBuilder
    >>>> .create();
    >>>
    >>> Try "SliderBuilder<?> sliderbuilder =" instead.


    OK, I finally checked out
    <http://docs.oracle.com/javafx/2/api/javafx/scene/control/SliderBuilder.html>
    myself. I was wrong on several counts.

    Although I was right about the value of the Javadocs.

    It looks like a similar pattern to 'Enum' or 'Comparable', where the 'B' is
    the subtype of the 'Builder'. As others have said, the '<?>' is the way to go.

    --
    Lew
    Honi soit qui mal y pense.
    http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg
     
    Lew, May 6, 2012
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nagaraj
    Replies:
    1
    Views:
    890
    Lionel B
    Mar 1, 2007
  2. Eisa Ayed

    JavaFX , what its mean ???

    Eisa Ayed, May 10, 2007, in forum: Java
    Replies:
    3
    Views:
    556
    tahir akhtar
    May 10, 2007
  3. Replies:
    0
    Views:
    360
  4. Carter
    Replies:
    2
    Views:
    518
    Carter
    Mar 4, 2009
  5. bux

    google time slider control

    bux, Oct 6, 2006, in forum: Javascript
    Replies:
    1
    Views:
    124
    Dr John Stockton
    Oct 7, 2006
Loading...

Share This Page