Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Method reference does not work with the Decorator pattern

Denis.MakarskiyMar 28 2014 — edited Apr 8 2014

Hello all!

I faced with a problem, when I use method reference as a function in Collectors.groupingBy I get an error: "no suitable method found for collect". But! if I replaced method reference with a lambda expression everything works fine.

Here is the code sample:

interface Iface{

    public int getPropertyOfClassA();

    public void setPropertyOfClassA(int propertyOfClassA);

}

class A implements Iface{

    private int propertyOfClassA;

    @Override

    public int getPropertyOfClassA() {

        return propertyOfClassA;

    }

    @Override

    public void setPropertyOfClassA(int propertyOfClassA) {

        this.propertyOfClassA = propertyOfClassA;

    }

}

class B{

    private A objectA;

    public int getPropertyOfClassA() {return objectA.getPropertyOfClassA();}

}

class C extends B {

    int propertyOfClassC;

    public int getPropertyOfClassC() {

        return propertyOfClassC;

    }

    public void setPropertyOfClassC(int propertyOfClassC) {

        this.propertyOfClassC = propertyOfClassC;

    }

}

public class example {

    public static void main(String[] ars){

        List<B> listOfB = new ArrayList<>();

        Map<Integer, List<B>> groupedByPropertyOfClassA = listOfB

                .stream()

                .collect(Collectors.groupingBy(C::getPropertyOfClassA));

    }

}


Change "C::getPropertyOfClassA" with "objC -> objC.getPropertyOfClassA()" and it works!

Any ideas?

This post has been answered by James_D on Mar 28 2014
Jump to Answer

Comments

MoC

Can you increase the font size instead of scaling the Text?

-fx-font-size: 2.0em;

This would fix the issue, I think.

user8929955

The StackPane will center everything you put into it. Use a Group instead.

NikolayArtamonov

Yes, it will fix the issue, but I need proportional scaling of text relative to size of stack pane. By the way, size of stack pane isn't fixed and may change on some conditions. It's hard to do with font size, that's why I use scaleX/Y properties.

MoC
Answer

You could try wrapping the Text in a Group. I tested in Scene Builder and it seemed to work ok with different StackPane alignments:

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>

<?import java.util.*?>

<?import javafx.scene.*?>

<?import javafx.scene.layout.*?>

<?import javafx.scene.paint.*?>

<?import javafx.scene.text.*?>

<StackPane alignment="TOP_LEFT" prefHeight="230.0" prefWidth="335.0" style="" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2">

  <children>

    <Group id="Group">

      <children>

        <Text layoutX="-29.0" layoutY="-11.0" scaleX="2.0" scaleY="2.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Text" />

      </children>

    </Group>

  </children>

</StackPane>

Marked as Answer by NikolayArtamonov · Sep 27 2020
shakir.gusaroff

Hi. Try the following:

text.setTextAlignment(TextAlignment.LEFT);

StackPane.setAlignment(Pos.CENTER);

NikolayArtamonov

Thank you very much, MoC! Wrapping text to group gracefully solved the problem! )

1 - 6

Post Details

Added on Mar 28 2014
2 comments
1,525 views