fraaanz
fraaanz

Reputation: 117

why listener defined in a subclass seems that aren't defined

I have a class that contains has a private member a combobox and extended CostomComponet as:

class TelefonoWidgetView extends CustomComponent {

private ComboBox comboRecTel;

private VerticalLayout recTelLayout(){

comboRecTel = new ComboBox();
comboRecTel.addValueChangeListener(new ValueChangeListener() {
            private static final long serialVersionUID = 1L;

            @Override
            public void valueChange(ValueChangeEvent event) {
             //do something

     }
}

Now in another class I need to use that class (that I cannot modify) and have a Listener on the combobox; How I can do that?

Let say my new class is WidgetView, and is defined as

WidgetView {

private TelefonoWidgetView tel;

private void metho1(){
    tel.addListener(new com.vaadin.ui.Component.Listener() {

            
                private static final long serialVersionUID = 1L;

                @Override
                public void componentEvent(Event event) {
                    System.out.println( " scatto addListener su tel : ");
                    
                }
            });

    }
  }

when I click on the combobox, I cannot see "scatto addListener su tel" on my console even if it compile correctly, It seems that the Listener has not been defined, while it is there!!

I know that I can create a combobox by myself but in the class TelefonoWidgetView there are other staff that I cannot copied;

I ve also tryed to put addDetachListener(new DetachListener(), addAttachListener(new AttachListener(),addContextClickListener(new ContextClickListener()

but no one is able to intercept the value change in the combobox

thanks a lot

Upvotes: 0

Views: 52

Answers (2)

kscherrer
kscherrer

Reputation: 5766

I am assuming that you can edit the TelefonoWidgetView class.
You can add a public method in your TelefonoWidgetView which accepts a ValueChangeListener and passes it on to the combobox like this:

// I use newer vaadin versions, so please excuse any errors in my syntax
class TelefonoWidgetView extends CustomComponent {

    private ComboBox comboRecTel;

    public TelefonoWidgetView(){
        comboRecTel = new ComboBox();
        ...
        addComponent(comboRecTel);
    }

    public void addTelValueChangeListener(ValueChangeListener valueChangeListener) {
        this.comboRecTel.addValueChangeListener(valueChangeListener);
    }
}

public class WidgetView {

    private TelefonoWidgetView tel;

    public WidgetView() {
        tel = new TelefornoWidgetView();
        addComponent(tel);
        metho1();
    }

    private void metho1(){
        tel.addTelValueChangeListener(new ValueChangeListener() {
            private static final long serialVersionUID = 1L;

            @Override
            public void valueChange(ValueChangeEvent event) {
                System.out.println( " scatto addListener su tel : "+event.getValue());
            }
        });
    }

Upvotes: 1

bekce
bekce

Reputation: 4330

You can use Java reflections API to access comboRecTel even though it is private.

Field privateField = TelefonoWidgetView.class.getDeclaredField("comboRecTel");
privateField.setAccessible(true);
ComboBox comboRecTel = (ComboBox) privateField.get(tel);
// then add your new custom listener here
comboRecTel.addValueChangeListener(...);

Upvotes: 1

Related Questions