Olá amigos!
Preciso saber como eu coloco um JComboBox contendo valores estáticos em mina JTable.
Eu tenho um DocumentosTableModel implementado, um Editor e um Renderer implementado para o meu JComboBox. Contudo eu não tenho muita certeza do que fazer.
Como eu falo para o meu modelo que determinada coluna vai ser um combo? Com uma lista? Com um new JComboBox(lista) ?
E outra duvida xD Como eu coloco cores diferentes para cada item do Combo... sem usar html pois preciso recuperar o item e não o indice. (Estou usando html para mudar a cor, porem eu tenho que fazer varios tratamentos capturando apenas o indice)
Vou postar o código aqui caso ajude.public class MyComboBoxRenderer extends JComboBox implements TableCellRenderer {
public MyComboBoxRenderer(String[] items) {
super(items);
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (isSelected) {
setForeground(table.getSelectionForeground());
super.setBackground(table.getSelectionBackground());
} else {
setForeground(table.getForeground());
setBackground(table.getBackground());
}
// Select the current value
setSelectedItem(value);
return this;
}
}
public class MyComboBoxEditor extends DefaultCellEditor {
public MyComboBoxEditor(String[] items) {
super(new JComboBox(items));
}
}
public class DocumentosTableModel extends AbstractTableModel {
private static final int COL_CODIGO = 0;
private static final int COL_DESCRICAO = 1;
private static final int COL_CONDICAO = 2;
private static final int COL_OBSERVACAO = 3;
private List<DocumentoPermanenteDados> documentos;
public DocumentosTableModel(List<DocumentoPermanenteDados> documentos) {
this.documentos = new ArrayList<DocumentoPermanenteDados>(documentos);
}
public int getRowCount() {
return documentos.size();
}
public int getColumnCount() {
return 4;
}
@Override
public String getColumnName(int col) {
if(col == COL_CODIGO) return "Código";
if(col == COL_DESCRICAO) return "Descrição";
if(col == COL_CONDICAO) return "Condição";
if(col == COL_OBSERVACAO) return "Observação";
return "";
}
public Object getValueAt(int row, int col) {
DocumentoPermanenteDados documento = documentos.get(row);
if(col == COL_CODIGO) return documento.getDocumentoPermanente().getId();
else if(col == COL_DESCRICAO) return documento.getDocumentoPermanente().getDescricao();
// Deve retornar o indice do JComboBox para a tabela Como?
else if(col == COL_CONDICAO) return documento.getCondicao();
else if(col == COL_OBSERVACAO) return documento.getObservacao();
return "";
}
@Override
public void setValueAt(Object aValue, int row, int col) {
DocumentoPermanenteDados documento = documentos.get(row);
if (col == COL_CODIGO) documento.getDocumentoPermanente().setId(Long.parseLong(aValue.toString()));
else if (col == COL_DESCRICAO) documento.getDocumentoPermanente().setDescricao(aValue.toString());
// Deve pegar o indice do combo e adicionar para o objeto Como?
else if (col == COL_CONDICAO) documento.setCondicao(aValue.toString());
else if (col == COL_OBSERVACAO) documento.setObservacao(aValue.toString());
}
@Override
public Class<?> getColumnClass(int col) {
if (col == COL_CODIGO) return Long.class;
else if (col == COL_DESCRICAO) return String.class;
// Devo retornar a classe JComboBox?
else if (col == COL_CONDICAO) return JComboBox.class;
else if (col == COL_OBSERVACAO) return String.class;
return Object.class;
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true;
}
public DocumentoPermanenteDados get(int row) {
return documentos.get(row);
}
}