Youcef LAIDANI
Youcef LAIDANI

Reputation: 59960

Create a swing component that contain multiple component

I try to create a component(Parent component) that contain 3 child component JLabel for the name of field and JTextField and JLabel for the error of my field like this:

public class YLabel extends JPanel {

    public YLabel() {
        super();
        JLabel name = new JLabel("Some text");
        JTextField field = new JTextField();
        JLabel error = new JLabel("Error text");
        super.add(name);
        super.add(field);
        super.add(error);
    }
}

So when I use this component I can't change the properties of the three component, I can just change the parent component JPanel

MY Component

Any idea how I can use this component and how I can change all the child components?

EDIT MY COMPONENT

Upvotes: 0

Views: 782

Answers (3)

camickr
camickr

Reputation: 324108

Don't know if this will help, but for some properties (I don't know how many?) if the child value is null the parent value will be used

For example:

JLabel label= new JLabel( "hello" );
label.setForeground( null );
label.setFont( null );
JTextField textField = new JTextField(10);
textField.setForeground( null );
textField.setFont( null );

JPanel panel = new JPanel();
panel.setForeground( Color.RED );
panel.setFont( new Font("monospaced", Font.PLAIN, 24) );

panel.add( label );
panel.add( textField );

For properties where this is not supported you would need to override the setXXX(...) method of the panel to update all its child components.

Edit:

So I assume you have a custom component something like this:

public class CustomComponent extends JPanel
{
    private JLabel heading = new JLabel(...);
    private JTextField textField = new JTextField(5);
    private JLabel error = new JLabel(...);

    public CustomComponent()
    {
        add( heading );
        add( textField );
        add( error );

        clearProperties( heading );
        clearProperties( textField );
        clearProperties( error );

    }

    private void clearProperties(JComponent component)
    {
        component.setForeground( null );
        component.setFont( null );
    }
}

Now when you use the component you can do something like:

CustomComponent component = new CustomComponent();
component.setForeground(...);
component.setFont(..);

Using this approach you don't have to override setForeground(...), setFont(...) of the panel to apply the attributes to each child component.

Upvotes: 2

Faiz
Faiz

Reputation: 51

As per your scenario, you can retrieve the child components for the YLabel panel and then modify their properties, but then you need to remember the sequence of your child components. Below code for retrieving the child components

YLabel panel = new YLabel();
for(Component component : panel.getComponents()) {
    System.out.println("component "+component.getClass().getCanonicalName());
    //Change the properties here for your components
}    

Upvotes: 2

Linksx
Linksx

Reputation: 260

You should have a Source and Design button in your project, so when you select Design button, it lets you update properties of all objects. You have it?

Upvotes: 1

Related Questions