What rule of thumb applies to Swing component expansion?

When you dedicate a class to a specific Swing component, is it better to extenduse that particular component or build it inside and provide a link?

public class Foo extends JComponent{

}

OR

public class Foo{
    public JComponent getComponent(){
    }
}

EDIT

This is what I mean by initiation.

public class Foo{
    private static Foo INSTANCE;

    public static Foo getInstance(){
        if(INSTANCE == null){
            INSTANCE = new Foo();
        }
    }

    public void createAndShowComponent(){
        //do stuff
    }
}

Inside, createAndShowComponent()I create JComponentwith all its components and their corresponding listeners without , displaying the internal components of the component just created.

+3
source share
6 answers

+1 for composition over extension. This makes the API much cleaner as you only discover which methods are important to your new component.

+7
source

jzd, .

, , , , , , JComponent. .

. , , , . .

, , , . , .

+3

, . , . . .

. .

+2

, . , .

+2

, . JPanel, , . , .

+1

I would say none of them. Swing components are very (very) rich and can be configured to render (L & F) and behavior (events) in any way. Another point is to create a group of various components and put them in JPanel.

+1
source

All Articles