Reputation: 59960
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
Any idea how I can use this component and how I can change all the child components?
Upvotes: 0
Views: 782
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
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
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