Java- Start general properties for the components

2

Good afternoon. My question is how to generate several properties for a component (for example a JLabel) and be able to assign it to all the JLabel instants.

Whenever we installed a JLabel we made the following steps.

JLabel ejemploLb = new JLabel();
    ejemploLb.setText("LISTADO DE MIEMBROS");
    ejemploLb.setFont(new Font("Arial", 1, 18));
    ejemploLb.setBackground(Color.red);

And so on every time we start a JLabel, with this I want to ask, is there the possibility of being able to take the properties of that component (JLabel) and apply it to another JLabel without having to write the same lines again?

An idea has occurred to me but I would like to find another one simpler. This is my example.

/*Instanciamos los componentes*/
     titleLb = new JLabel("LISTADO DE CLIENTES");
     titleL2b = new JLabel("LISTADO DE PROVEEDORES");

/*Llamamos al método y mandamos el JLabel*/
     initJLabel(titleLb);
     initJLabel(titleLb2);

/*En el método introducimos las propiedades que queremos para los JLabel*/
private void initJLabel(JLabel label){    
    label.setFont(new Font("Arial", 1, 18));
    label.setBackground(Color.red);
}

Do you know a simpler way? Thank you very much for everything.

    
asked by Arkhan6 19.10.2016 в 20:09
source

1 answer

0

I recommend that you do your own extended class of JLabel and set those default values by doing @Override of the methods, so your objects will be of your particular class with all the properties and events that JLabel has.

Answering your question

  

Is there the possibility of being able to take the properties of that component (JLabel) and apply it to another JLabel without having to write the same lines again?

The answer is no, remember that each object that you create JLabel is unique and has its defined properties.

Another way, is to have an arrangement of JLabels and go through them one by one and set these properties ...

JLabel[] labels = new JLabel[3];

for (int i = 0 i < cur.length; i++){
    labels[i] = new JLabel( cur[i] );
    labels[i].setText("LISTADO DE MIEMBROS");
    ....
}
    
answered by 19.10.2016 / 20:22
source