Olá pessoal,
Tenho uma aplicação em que mostro um JTable que tem algumas colunas com JComboBox e outra que mostra células simples.
O problema é que quando eu clico na tecla TAB a seleção deveria mudar de célula a célula da tabela, mas o que está acontecendo é que só as células simples estão ficando com foco.
Alguém já teve este problema?
Código abaixo:
package teste.jcombo1;
import java.awt.Component;
import java.util.ArrayList;
import java.util.EventObject;
import java.util.List;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.event.CellEditorListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
public class ComboTable {
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new ComboTable();
}
});
}
public ComboTable() {
MyModel model = new MyModel();
JTable table = new JTable(model);
table.setDefaultRenderer(JComboBox.class, new ComboRenderer());
table.setDefaultEditor(JComboBox.class, new ComboEditor());
JScrollPane scroller = new JScrollPane(table);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(scroller);
f.setSize(500, 500);
f.setVisible(true);
}
}
class ComboRenderer implements TableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
return (JComboBox) value;
}
}
class ComboEditor implements TableCellEditor {
private java.util.Vector listeners = new java.util.Vector();
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
table.setRowSelectionInterval(row, row);
table.setColumnSelectionInterval(column, column);
return (JComboBox) value;
}
public boolean shouldSelectCell(EventObject o) {
return true;
}
public void removeCellEditorListener(CellEditorListener l) {
listeners.remove(l);
}
public void addCellEditorListener(CellEditorListener l) {
listeners.add(l);
}
public void cancelCellEditing() {
}
public Object getCellEditorValue() {
return null;
}
public boolean isCellEditable(EventObject o) {
return true;
}
public boolean stopCellEditing() {
return true;
}
}
class MyModel extends AbstractTableModel {
String[] headers = { "Coluna 1", "Coluna 2" ,"Coluna 3"};
Class[] columnTypes = { JComboBox.class, String.class ,JComboBox.class};
Object[] items = {"A", "B", "C", "D"};
List data;
public MyModel() {
data = new ArrayList();
List linha = new ArrayList();
linha.add(new JComboBox(items));
linha.add(new String("XXXXX"));
linha.add(new JComboBox(items));
data.add(linha);
linha = new ArrayList();
linha.add(new JComboBox(items));
linha.add(new String(""));
linha.add(new JComboBox(items));
data.add(linha);
}
public int getRowCount() {
return data.size();
}
public int getColumnCount() {
return headers.length;
}
public Class getColumnClass(int col) {
return columnTypes[col];
}
public String getColumnName(int col) {
return headers[col];
}
public boolean isCellEditable(int row, int col) {
return true;
}
public Object getValueAt(int row, int col) {
java.util.List myRow = (java.util.List) data.get(row);
return myRow.get(col);
}
}
Obrigado