Ajuda com Jtable CellRenderer

Prezados bom dia,

estou implementado um jtable usando AbstractTableModel implements TableModelListener que ate aprendi usando os exemplos que achei aqui no guj, inclusise um exemplo do vinigodoy :

precisei criar um modelo de tabela que contenha na coluna 0 um jcheckbox e na coluna 1 um jcombobox ate ai tudo bem consegui criando um JCheckboxCellRenderer extends DefaultTableCellRenderer etc … etc…

mas quando rodo a aplicação nao consigo usar os controles dentro do jtable fica como se tivesse travado, mas ai implementei o:

//Você deve implementar o setValueAt se sua tabela for editável.  
    //Nesse exemplo, vou deixar alterar só a quantidade.  
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        try {
            if (columnIndex == DIA) {
                JCheckBox chk = (JCheckBox) aValue;
                listadeDias.get(rowIndex).setMarcado(chk.isSelected());
            }
        } catch (Exception e) {
            //Em caso de exceção, deixo o produto inalterado. Isso fará com que os valores antigos sejam desenhados.  
            //Pode ocorrer caso o usuário digite uma letra na quantidade, por exemplo.  
        }

        fireTableCellUpdated(rowIndex, columnIndex);
    }

 @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        //return super.isCellEditable(rowIndex, columnIndex);
        if (columnIndex == 0 || columnIndex == 1) {
            return true;
        } else {
            return false;
        }
    }

mas também nao deu certo como faria para abilitar estes controles seria de outra forma?

alguem pode me ajudar com isso?

segue anexo imagem do app rodando…: http://img94.imageshack.us/img94/7782/capturadetelade20130115.png

Dá uma olhada aqui:

http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#combobox

rogeriopaguilar deu certo com a juda do seu link obrigado valeu !

[code]
import javax.swing.AbstractCellEditor;
import javax.swing.table.TableCellEditor;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JTable;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JCheckBox;

public class JCheckboxEditor extends AbstractCellEditor implements TableCellEditor, MouseListener {
boolean currentValor;
JCheckBox chkDia;
protected static final String EDIT = “edit”;

public JCheckboxEditor() {      
    chkDia = new JCheckBox("01/01/2012");     
}

/**
 * Handles events from the editor button and from
 * the dialog's OK button.
 */
/*public void actionPerformed(ActionEvent e) {
    if (EDIT.equals(e.getActionCommand())) {
        //The user has clicked the cell, so
        //bring up the dialog.
        ///button.setBackground(currentColor);
        ///colorChooser.setColor(currentColor);
        ///dialog.setVisible(true);

        //Make the renderer reappear.
        fireEditingStopped();

    } else { //User pressed dialog's "OK" button.
       /// currentColor = colorChooser.getColor();
    }
}*/

@Override
public void mouseClicked(MouseEvent e) {
    chkDia.setSelected(false);
    fireEditingStopped();
    System.out.println("clicou !!!");
}

//Implement the one CellEditor method that AbstractCellEditor doesn't.
public Object getCellEditorValue() {
    ///return currentColor;
    return chkDia;
}

//Implement the one method defined by TableCellEditor.
public Component getTableCellEditorComponent(JTable table,
                                             Object value,
                                             boolean isSelected,
                                             int row,
                                             int column) {
    chkDia = (JCheckBox) value;
    //fireEditingStopped();
    return chkDia;
}

@Override
public void mousePressed(MouseEvent e) {
    throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public void mouseReleased(MouseEvent e) {
    throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public void mouseEntered(MouseEvent e) {
    throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public void mouseExited(MouseEvent e) {
    throw new UnsupportedOperationException("Not supported yet.");
}

}[/code]

Volta o cão arrependido…
contei vitoria antes do tempo…
galera empaquei novamente agora quando eu mudo por exemplo: seleciono o checkbox ou jcombo… ele muda mas nao salva as alteracoes so quando eu tiro o foco da celular e assim mesmo o funcionamento? se eu marquei 4 items no check quando eu dou recupero os elementos somente tres estao (isSelected=true)

no exemplo da oracle funciona ja logo quando clico… apesar de ele mostrar uma janela de colochoose… e depois ele atualiza… tentei adaptar para meu cenário e fazer o mesmo abrir uma janela e ja logo fechar(gambi…) mas nao deu! tb…

ou e o meu codigo?

codigo do tablemodel…


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kawakami.tables;

import com.kawakami.controller.EscalaController;
import com.kawakami.vo.Dias;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Alicia
 */
public class DiasTM extends AbstractTableModel implements TableModelListener {

    private static final int DIA = 0;
    private static final int TIPO_FOLGA = 1;
    private List<Dias> listadeDias; //A lista de dias de trabalho dos colaboradores que pretendemos exibir
    JTable jtableInstance;
    EscalaController escalaController = EscalaController.getInstance();

    //Esse é um construtor, que recebe a nossa lista de funções  
    public DiasTM(Collection<Dias> listadeDias) {
        this.listadeDias = new ArrayList<Dias>(listadeDias);
    }

    @Override
    public int getRowCount() {
        //Quantas linhas tem sua tabela? Uma para cada item da lista.
        //throw new UnsupportedOperationException("Not supported yet.");
        return listadeDias.size();
    }

    @Override
    public int getColumnCount() {
        //Quantas colunas tem a tabela? Na sua tabela, 5. 
        //throw new UnsupportedOperationException("Not supported yet.");
        return 2;
    }

    @Override
    public String getColumnName(int columnIndex) {
        //Qual é o nome das nossas colunas? Isso aparecerá no cabeçalho  
        String label = "";
        if (columnIndex == DIA) {
            label = "DIA";
        }
        if (columnIndex == TIPO_FOLGA) {
            label = "TIPO DE FOLGA";
        }

        return label;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        //Precisamos retornar o valor da coluna column e da linha row.  
        //O JTable chama esse método quando precisa desenhar uma célula. E só ele deve chama-lo.  
        //Você usará um método muito mais conveniente, chamado getfuncao()  
        Dias dia = listadeDias.get(rowIndex);
        Object obj = null;
        JComboBox jcmbTipo_folga = new JComboBox();
        jcmbTipo_folga.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Folga Normal", "Folga Feriado", "Folga Domingo", "Folga Férias", "Folga Afastado", "Folga Demitido", "Folga Licença" }));


        if (columnIndex == DIA) {
            JCheckBox chkdia = new JCheckBox(dia.getDia().toString());
            //chkdia.setEnabled(true);
            chkdia.setSelected(dia.isMarcado());
            obj = chkdia;
        }
        if (columnIndex == TIPO_FOLGA) {
            obj = jcmbTipo_folga;
        }

        return obj;
    }

    //Você deve implementar o setValueAt se sua tabela for editável.  
    //Nesse exemplo, vou deixar alterar só a quantidade.  
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        try {
            if (columnIndex == DIA) {
                JCheckBox chk = (JCheckBox) aValue;
                listadeDias.get(rowIndex).setMarcado(chk.isSelected());
            }
        } catch (Exception e) {
            //Em caso de exceção, deixo o produto inalterado. Isso fará com que os valores antigos sejam desenhados.  
            //Pode ocorrer caso o usuário digite uma letra na quantidade, por exemplo.  
        }
        
        fireTableCellUpdated(rowIndex, columnIndex);
        System.out.println("setValueAt...");
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        //return super.isCellEditable(rowIndex, columnIndex);
        if (columnIndex == 0 || columnIndex == 1) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public Class getColumnClass(int columnIndex) {
        //Qual a classe das nossas colunas?   
        if (columnIndex == DIA) {
            return JCheckBox.class;
        }
        if (columnIndex == TIPO_FOLGA) {
            return JComboBox.class;
        }

        //As demais são String  
        return String.class;
    }

    /*public boolean isCellEditable(int rowIndex, int columnIndex) {  
     //Indicamos se a célula da rowIndex e da columnIndex é editável. Somente a quantidade é editavel nesse exemplo  
     //return columnIndex == COL_QUANTIDADE;  
     }*/
    //Já que esse tableModel é de Notas, vamos fazer um get que retorne uma Nota inteira.  
    //Isso elimina a necessidade de chamar o getValueAt() nas telas.   
    public Dias get(int row) {
        return listadeDias.get(row);
    }

    // --- Já respondemos ao Swing o que ele queria saber. Agora é hora de adicionar métodos para nós mesmo. :)   
    //Esse método permite adicionar uma funcao  
    public void add(Dias dias) {
        //Adicionamos o produto  
        listadeDias.add(dias);
        //Avisamos a table, para que redesenhe a linha  
        fireTableRowsInserted(listadeDias.size() - 1, listadeDias.size() - 1);
    }

    public List<Dias> getDias() {
        return Collections.unmodifiableList(listadeDias);
    }

    @Override
    public void tableChanged(TableModelEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void setHeaderRenderer(JTable jtableInstance) {
        this.jtableInstance = jtableInstance;
        this.jtableInstance.getColumnModel().getColumn(0).setMaxWidth(50);
        this.jtableInstance.getColumnModel().getColumn(1).setMaxWidth(60);
    }
}

codigo dos meus renderer…

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kawakami.tables;

import java.awt.Component;
import javax.swing.JCheckBox;
import javax.swing.JProgressBar;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;

/**
 *
 * @author Alicia
 */
public class JCheckboxCellRenderer implements TableCellRenderer {

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        //return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        JCheckBox checkDia = (JCheckBox)  value;
        return checkDia;
    }
    
    
}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kawakami.tables;

import java.awt.Component;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JProgressBar;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

/**
 *
 * @author Alicia
 */
public class JComboboxCellRenderer extends DefaultTableCellRenderer {

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        //return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        JComboBox jcomboTipo_Folga = (JComboBox)  value;
        return jcomboTipo_Folga;
    }
}

codigo dos meus celleditor…


/*
 * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   - Neither the name of Oracle or the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.kawakami.tables;

/* 
 * ColorEditor.java (compiles with releases 1.3 and 1.4) is used by 
 * TableDialogEditDemo.java.
 */
import com.kawakami.view.JDDiaedit;
import java.awt.Color;
import javax.swing.AbstractCellEditor;
import javax.swing.table.TableCellEditor;
import javax.swing.JTable;
import java.awt.Component;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JCheckBox;

public class JCheckboxEditor extends AbstractCellEditor implements TableCellEditor, MouseListener {

    boolean currentValor;
    JCheckBox chkDiaEdit;
    JCheckBox chkDiaTable;
    DiasTM diasTM;
    int row;
    int cow;
    protected static final String EDIT = "edit";

    public JCheckboxEditor() {
        chkDiaEdit = new JCheckBox("01/01/2012");
        //chkDia.setActionCommand(EDIT);
        chkDiaEdit.addMouseListener(this);
        chkDiaEdit.setOpaque(true);
        
        System.out.println("construtor !!!");
    }

    /**
     * Handles events from the editor button and from the dialog's OK button.
     */
    @Override
    public void mouseClicked(MouseEvent e) {
        System.out.println("mouseClicked !!!");
        chkDiaEdit.setBackground(Color.YELLOW);
        JDDiaedit jdedit = new JDDiaedit(null, true);
        jdedit.setVisible(true);
        //chkDiaTable = chkDiaEdit;
        fireEditingStopped();
    }

    //Implement the one CellEditor method that AbstractCellEditor doesn't.
    public Object getCellEditorValue() {
        ///return currentColor;
        return chkDiaTable;
    }

    //Implement the one method defined by TableCellEditor.
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        chkDiaTable = (JCheckBox) value;
        chkDiaTable.addMouseListener(this);
        chkDiaTable.setOpaque(true);
        diasTM = (DiasTM) table.getModel();
        this.row = row;
        this.cow = column;

        System.out.println("getTableCellEditorComponent...");
        return chkDiaEdit;
    }

    @Override
    public void mousePressed(MouseEvent e) {
        //System.out.println("mousePressed...");
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        //System.out.println("mouseReleased...");
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void mouseExited(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }
}

/*
 * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   - Neither the name of Oracle or the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.kawakami.tables;

/* 
 * ColorEditor.java (compiles with releases 1.3 and 1.4) is used by 
 * TableDialogEditDemo.java.
 */
import com.kawakami.view.JDDiaedit;
import java.awt.Color;
import javax.swing.AbstractCellEditor;
import javax.swing.table.TableCellEditor;
import javax.swing.JTable;
import java.awt.Component;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;

public class JComboboxEditor extends AbstractCellEditor implements TableCellEditor, MouseListener {

    boolean currentValor;
    JComboBox cmbTipoFolgaEditor;
    JComboBox cmbTipoFolgaEditorTable;
    DiasTM diasTM;
    int row;
    int cow;
    protected static final String EDIT = "edit";

    public JComboboxEditor() {
        cmbTipoFolgaEditor = new JComboBox();
        //chkDia.setActionCommand(EDIT);
       // chkDiaEdit.addMouseListener(this);
        //chkDiaEdit.setOpaque(true);
        
        System.out.println("construtor !!!");
    }

    /**
     * Handles events from the editor button and from the dialog's OK button.
     */
    @Override
    public void mouseClicked(MouseEvent e) {
        System.out.println("mouseClicked !!!");
        //chkDiaEdit.setBackground(Color.YELLOW);
        //JDDiaedit jdedit = new JDDiaedit(null, true);
        //jdedit.setVisible(true);
        //chkDiaTable = chkDiaEdit;
        fireEditingStopped();
    }

    //Implement the one CellEditor method that AbstractCellEditor doesn't.
    public Object getCellEditorValue() {
        ///return currentColor;
        return cmbTipoFolgaEditor;
    }

    //Implement the one method defined by TableCellEditor.
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        cmbTipoFolgaEditorTable = (JComboBox) value;
        //chkDiaTable.addMouseListener(this);
        //chkDiaTable.setOpaque(true);
        //diasTM = (DiasTM) table.getModel();
        this.row = row;
        this.cow = column;

        System.out.println("getTableCellEditorComponent...");
        return cmbTipoFolgaEditorTable;
    }

    @Override
    public void mousePressed(MouseEvent e) {
        //System.out.println("mousePressed...");
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        //System.out.println("mouseReleased...");
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void mouseExited(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }
}

perdi a manha inteira só nisso ! e pelo que to vendo vai ser o dia inteiro,

nossa ache que seria mais facil…

segui o exemplo que rogeriopaguilar passow mas… nada !!!
não ta se comportando do jeito que queria…

inicializo meu jtable assim…

private void geraDiasMes(){
        diasMes = new LinkedList<Dias>();
        
        for (int x = 0; x <= 30; x++) {
            Dias dia = new Dias();
            dia.setDia(""+x);
            diasMes.add(dia);
        }
        mostraDiasMes(diasMes);
// na verdade esse medoto depois vou mudar para api jodatime para pegar as datas ... aki e so para testar mesmo....
    }
    
    private void imprimeDiasMes(){
              
        for (int x = 0; x <= 30; x++) {
            Dias dia = diasMes.get(x);
            System.out.println(dia.getDia() + ":" + dia.isMarcado());
        }
        //mostraDiasMes(diasMes);
    }
    
    private void mostraDiasMes(List<Dias> diasMes){
        DiasTM diasTM = new DiasTM(diasMes);
        lstDiasMes.setModel(diasTM);
        lstDiasMes.getColumnModel().getColumn(0).setCellRenderer(new JCheckboxCellRenderer());
        lstDiasMes.getColumnModel().getColumn(0).setCellEditor(new JCheckboxEditor());
        lstDiasMes.getColumnModel().getColumn(1).setCellRenderer(new JComboboxCellRenderer());
        lstDiasMes.getColumnModel().getColumn(1).setCellEditor(new JComboboxEditor());
        lstColaboradores.setGridColor(Color.GRAY);
        lstColaboradores.setShowGrid(true);
        lstColaboradores.setRowHeight(25);
        
    }

dia e um bean com getes e setters…

public class Dias {
    private String dia;
    private boolean marcado;
    private String tipo_folga;

    public String getDia() {
        return dia;
    }

    public void setDia(String dia) {
        this.dia = dia;
    }

    public boolean isMarcado() {
        return marcado;
    }

    public void setMarcado(boolean marcado) {
        this.marcado = marcado;
    }

    public String getTipo_folga() {
        return tipo_folga;
    }

    public void setTipo_folga(String tipo_folga) {
        this.tipo_folga = tipo_folga;
    }
    
    
}