Problema de Focus no JComboBox dentro de um JTable

0 respostas
onyaszimba

Olá, amigos.

É o seguinte. Eu tenho uma tela com um JTable em que uma das colunas tem um JComboBox. Até aí nenhum problema, coloquei os combos em cada linha da tabela cada um com um TableCellEditor diferente usando o EachRowEditor, porque para cada combo podem haver valores diferentes. Mais abaixo eu mostro as classes usadas.

O meu problema é que quando eu abro a tela com o JTable preenchido ele exibe o combo e tudo mais. Só que o valor inicial do combo só aparece quando eu clico na célula, apesar dos valores estarem corretos.

Por que isso ocorre? O JTable tem alguma prioridade no focus ou coisa assim? Tem alguma solução para que o combo apareça preenchido visualmente, já que logicamente está tudo certo?

Antes de clicar:

Depois de clicar:

Abaixo as classes usadas:

ComboCellEditor

import java.awt.Component;
import java.util.ArrayList;
import javax.swing.AbstractCellEditor;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.event.CellEditorListener;
import javax.swing.event.ChangeEvent;
import javax.swing.table.TableCellEditor;


public class ComboCellEditor extends AbstractCellEditor implements TableCellEditor {

    
    private JComboBox box;
    private ArrayList<String> listaColuna;
    private int linha; // Linha da tabela onde se encontra este editor
    private int coluna; // coluna da tabela onde se encontra este editor

    public ComboCellEditor(ArrayList<String> lista, int linha, int coluna) {
        try {
            listaColuna = lista;
            box = new JComboBox(listaColuna.toArray());

            this.linha = linha;
            this.coluna = coluna;

        } catch (Exception ex) {
            UTILMsg.msgErroConstrutor(null, ex);
        } // Fim do controle try/catch
    } // Fim do construtor


    @Override
    public Component getTableCellEditorComponent(JTable table, Object value,
            boolean isSelected, int row, int column) {

        return box;

    } // Fim do método getTableCellEditorComponent


    @Override
    public Object getCellEditorValue() {
        return box.getSelectedItem();
    } // Fim do método getCellEditorValue


    @Override
    public void addCellEditorListener(CellEditorListener l) {
       super.addCellEditorListener(new CellEditorListener() {
           @Override
           public void editingStopped(ChangeEvent e) {
                JTable t = (JTable)box.getParent();
                SimpleTableModel model = (SimpleTableModel)t.getModel();
                model.setValueAt(box.getSelectedItem(), linha, coluna);
                t.setModel(model); // */
            } // Fim do evento editingStopped
            @Override
            public void editingCanceled(ChangeEvent e) {
                // Não foi implementado
            } // Fim do evento editingCanceled
        }); // Fim da classe interna CellEditorListener
    } // Fim do método addCellEditorListener


    public void setSelectedComboBox(int row) {
        box.setSelectedIndex(row);
    } // Fim do método setSelectedComboBox


} // Fim da classe ComboCellEditor

EachRowEditor

import java.awt.Component;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.EventObject;
import javax.swing.DefaultCellEditor;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.CellEditorListener;
import javax.swing.table.TableCellEditor;


public class EachRowEditor implements TableCellEditor {

    public ArrayList<TableCellEditor> editors;
    protected TableCellEditor editor,  defaultEditor;
    JTable table;

    /**
     * Constructs a EachRowEditor.
     * create default editor
     *
    @see TableCellEditor
    @see DefaultCellEditor
     */
    public EachRowEditor(JTable table) {
        this.table = table;
        editors = new ArrayList<TableCellEditor>();
        defaultEditor = new DefaultCellEditor(new JTextField());
    }

    /**
    param row    table row
    param editor table cell editor
     */
    public void setEditorAt(int row, TableCellEditor editor) {
        editors.add(row, editor);
    }

    public TableCellEditor getEditorAt(int row) {
        return editors.get(row);
    }

    public void removeEditorAt(int row) {
        editors.remove(row);
    }

    public Component getTableCellEditorComponent(JTable table,
            Object value, boolean isSelected, int row, int column) {
        return editor.getTableCellEditorComponent(table,
                value, isSelected, row, column);
    }

    public Object getCellEditorValue() {
        return editor.getCellEditorValue();
    }

    public boolean stopCellEditing() {
        return editor.stopCellEditing();
    }

    public void cancelCellEditing() {
        editor.cancelCellEditing();
    }

    public boolean isCellEditable(EventObject anEvent) {
        selectEditor((MouseEvent) anEvent);
        return editor.isCellEditable(anEvent);
    }

    public void addCellEditorListener(CellEditorListener l) {
        editor.addCellEditorListener(l);
    }

    public void removeCellEditorListener(CellEditorListener l) {
        editor.removeCellEditorListener(l);
    }

    public boolean shouldSelectCell(EventObject anEvent) {
        selectEditor((MouseEvent) anEvent);
        return editor.shouldSelectCell(anEvent);
    }

    protected void selectEditor(MouseEvent e) {
        int row;
        if (e == null) {
            row = table.getSelectionModel().getAnchorSelectionIndex();
        } else {
            row = table.rowAtPoint(e.getPoint());
        }
        editor = editors.get(row);
        if (editor == null) {
            editor = defaultEditor;
        } // */
    }


} // Fim da classe EachRowEditor

Agradeço a atenção.

Criado 6 de maio de 2011
Respostas 0
Participantes 1