Currently I populate a normal combobox this way:
this.CadperfilLinha.removeAllItems();
try {
Class.forName(Auxiliar.AcessoBanco.getDriver());
Connection con = DriverManager.getConnection(Auxiliar.AcessoBanco.getUrl(), Auxiliar.AcessoBanco.getUser(), Auxiliar.AcessoBanco.getPass());;
Statement Sent = con.createStatement();
ResultSet rs = Sent.executeQuery("Select * from Linha");
CadperfilLinha.addItem("Selecione...");
while (rs.next()) {
this.CadperfilLinha.addItem(rs.getString("LINHA"));
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
But now I'm needing a popular one inside the jtable, but I'm not getting it. I have it mounted here by just pulling values typed inside the code and wanted it to use the database. Can someone give me a hand?
String[] values = new String[]{"Ativo", "Desativado"};
TableColumn col = jTable1.getColumnModel().getColumn(2);
col.setCellEditor(new MyComboBoxEditor(values));
col.setCellRenderer(new MyComboBoxRenderer(values));
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());
}
setSelectedItem(value);
return this;
}
}
class MyComboBoxEditor extends DefaultCellEditor {
public MyComboBoxEditor(String[] items) {
super(new JComboBox(items));
}
}