Ver Mensaje Individual
  #7 (permalink)  
Antiguo 27/04/2019, 21:59
Avatar de detective_jd
detective_jd
 
Fecha de Ingreso: abril-2011
Ubicación: Salto
Mensajes: 437
Antigüedad: 13 años
Puntos: 6
Respuesta: Personalizando JList

Hola, gracias CalgaryCorpus, verás al resto de los jlist que tienen headers y celdas todos funcionan bien además de estar implementados de la misma manera que este, de hecho simplifique más la superclase:

Código Java:
Ver original
  1. package app.vista.util.celdas;
  2. import app.vista.util.columnas.HeaderPanel;
  3. import java.awt.*;
  4. import javax.swing.*;
  5. public abstract class CeldaPerformer extends JPanel implements ListCellRenderer {
  6.     protected HeaderPanel header;
  7.     protected JTextArea[] columns;
  8.     public CeldaPerformer(HeaderPanel xheader) {
  9.         this.header = xheader;
  10.         setLayout(new FlowLayout(FlowLayout.LEFT, 2, 2));
  11.         setBackground(Color.WHITE);
  12.         setOpaque(true);
  13.         setFont(new Font("DejaVu Sans",Font.BOLD, 12));
  14.         prepareColumns();
  15.     }
  16.     protected void customRenderer(JTextArea ta,JLabel xheader) {
  17.         ta.setOpaque(true);
  18.         ta.setFont(new Font("DejaVu Sans",Font.BOLD, 12));
  19.         ta.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
  20.         ta.setForeground(Color.BLACK);
  21.         ta.setPreferredSize(xheader.getPreferredSize());
  22.         ta.setBorder(BorderFactory.createEmptyBorder(0, 20, 0, 20));        
  23.     }
  24.     protected void colourColumn(JTextArea ta,Color xc){
  25.         ta.setBackground(xc);        
  26.         ta.setForeground(Color.BLACK);
  27.     }    
  28.     protected void prepareColumns(){
  29.         columns = new JTextArea[header.getHeaders().length];
  30.         for(int i = 0; i < header.getHeaders().length; i++){
  31.             columns[i] = new JTextArea();
  32.             customRenderer(columns[i], header.getHeaders()[i]);
  33.             add(columns[i]);
  34.         }
  35.     }
  36.     @Override
  37.     public Component getListCellRendererComponent(JList list, Object value,
  38.         int index, boolean isSelected, boolean cellHasFocus) {
  39.         displayColumns(value);
  40.         if (index % 2 == 0) {
  41.             for (JTextArea column : columns) {
  42.                 colourColumn(column, Color.LIGHT_GRAY);
  43.             }
  44.         } else {
  45.             for (JTextArea column : columns) {
  46.                 colourColumn(column, Color.WHITE);
  47.             }
  48.         }        
  49.         if(isSelected){
  50.             for (JTextArea column : columns) {
  51.                 colourColumn(column, Color.PINK);
  52.             }
  53.         }
  54.         return this;
  55.     }
  56.     public abstract void displayColumns(Object value);
  57. }

y la subclase:

Código Java:
Ver original
  1. package app.vista.util.celdas;
  2. import app.contr.clases.*;
  3. import app.contr.util.Conversiones;
  4. import app.contr.util.FacadeSystem;
  5. import app.vista.util.columnas.*;
  6. import java.awt.*;
  7. import javax.swing.*;
  8. public class CeldaRegistro extends CeldaPerformer {
  9.     public CeldaRegistro(HeaderPanel xheader) {
  10.         super(xheader);
  11.     }
  12.     @Override
  13.     protected void customRenderer(JTextArea ta,JLabel xheader) {        
  14.         ta.setOpaque(true);
  15.         ta.setFont(new Font("DejaVu Sans",Font.BOLD, 12));
  16.         ta.setComponentOrientation(
  17.             ComponentOrientation.LEFT_TO_RIGHT);
  18.         ta.setForeground(Color.BLACK);            
  19.         int bottom = getBottom();
  20.         if(bottom == 0){
  21.             bottom = xheader.getPreferredSize().height;
  22.         }
  23.         ta.setPreferredSize(new Dimension(
  24.             xheader.getPreferredSize().width, bottom));
  25.         ta.setBorder(BorderFactory.createEmptyBorder(
  26.             0, 20, bottom, 20));
  27.     }
  28.     private int getBottom(){
  29.         int maxlength = 1;
  30.         int maxBottom = 0;
  31.         for(Paciente p : FacadeSystem.getInstance().manejadorDePacientes().findAll(null)){
  32.             for(Registro r : p.getRegistros().getChildren()){
  33.                 String[] descrip = r.getDescripcion().split("\n");
  34.                 if(descrip.length > maxlength){
  35.                     maxlength = descrip.length;
  36.                 }
  37.                 String[] tratat = r.getTratamientos().split("\n");
  38.                 if(tratat.length > maxlength){
  39.                     maxlength = tratat.length;
  40.                 }
  41.             }
  42.         }
  43.         if(maxlength > 1){
  44.             for(int i = 0; i < maxlength; i++){
  45.                 maxBottom += 25;
  46.             }
  47.         } else {
  48.             maxBottom = 25;
  49.         }
  50.         return maxBottom;
  51.     }
  52.     @Override
  53.     public void displayColumns(Object value) {
  54.         Registro data = (Registro)value;
  55.         Consulta c = data.getConsulta();
  56.         columns[0].setText(Conversiones.CStr(data.getId()));
  57.         columns[1].setText(Conversiones.MostrarFechaYHora(c.getFecha()));
  58.         columns[2].setText(c.getMedico().getNombre() + " " + c.getMedico().getApellido());
  59.         columns[3].setText(c.getPaciente().getNombre() + " " + c.getPaciente().getApellido());
  60.         columns[4].setText(data.getDescripcion());
  61.         columns[5].setText(data.getTratamientos());
  62.     }
  63. }

Cita:
Si no tienes ningun objeto de tipo JList en tu codigo, o bien no le estas aplicando el renderer, entonces ya tienes la respuesta de porque esto no funciona.
El único es el parámetro de la función getListCellRendererComponent, probé con el setFont pero no hace nada.
Raro porque todos los demás funcionan menos este.

Espero sus respuestas y saludos.
__________________
Si te interesa, visita mi perfil de Linkedin. Gracias