How to create custom component

Hello, I wonder if there is how to create a
custom component type with a textfield
label in a class, and then call this component
custom of another class as parameter passing by the new name of the label to be shown and as I do that?

Hi,

I must say I’m not at all sure what you mean, and what you’re trying to achieve, but here is a starting point…

This is a CustomComponent with a Label (in XHTML mode) and a TextField, the constructor takes a labelText:

public class LabeledTextField extends CustomComponent {

    public LabeledTextField(String labelText) {
        VerticalLayout layout = new VerticalLayout();
        setCompositionRoot(layout);

        Label label = new Label(labelText);
        label.setContentMode(Label.CONTENT_XHTML); // let's make it support HMTL
        layout.addComponent(label);
        layout.addComponent(new TextField());
    }
}

You use it thus:

new LabeledTextField("My <b>bold</b> text")

Does this help?

Best Regards,
Marc

Hello,
I did as you showed, so that when you need the implementations of the class as in this example:

I’ll need to pass this class listenners.

when in fact I would like to be like this:

so that the problem is happening when I run the app, he is not calling any of the attributes that I set point in the case to the button.
So in short I need to create a custom button class that inherits from the more button, with all its methods are already implemented.

Since ja I am thankful for your help my dear friend, here in Brazil and the most difficult things

The rendering of a Button component is handled on the client side. It is not a ComponentContainer, so you cannot put other components in it or change its layout from the server. Furthermore, in your example, you would create a VerticalLayout and then just throw it away as it is not attached anywhere.

What you can do is as Marc suggested, keep the button in a field of the custom component, and add methods to the custom component that register a listener for the button (or even simply add a getter for the Button and register the listeners that way).

Hi,
Could you show me a practical example of how a component with listenner button, label and description for me to understand please

See the
Book on Button
.

Thinking of it now, the example in the Book is rather non-typical, as you typically use an anonymous class to handle button clicks. Here’s a
better on-line example
.