I know that using DefaultTableModel
is not the best way to do the tables, but I have not yet mastered AbstractTableModel
, I have a table that is edited as JTextArea, but when I type it does not come in the variable of cell of the table, I would like to know how I get what was typed ... CellRenderer is doing what I want, now I want it to get everything I type in textarea
of jTable
, follow code:
public void preenche_evolucao(){ //Pega do banco de dados
DefaultTableModel modelo = (DefaultTableModel) table_1.getModel();
modelo.setNumRows(0);
table_1.getColumnModel().getColumn(2).setCellRenderer(new CustomRenderer());
table_1.getColumnModel().getColumn(2).setCellEditor(new CustomEditor());
table_1.setRowHeight(50);
try {
Connection con = Conexao.getConexao();
Statement stmt = con.createStatement();
ResultSet RS = stmt.executeQuery("SELECT id, Data, Evolucao FROM tab2_perfil where IdPac='" + id + "'");
while (RS.next()) {
String dat = RS.getString("Data");
String tab = RS.getString("Evolucao");
String id = RS.getString("id");
modelo.addRow(new Object[] { id, dat, tab });
}
} catch (Exception E) {
}
public void salvaEv() throws java.lang.NullPointerException{ //Pega o que é escrito na célula para passar ao banco de dados
int row = table_1.getSelectedRow();
Object valor = table_1.getValueAt(row, 0).toString();
Object valor2 = table_1.getValueAt(row, 1).toString();
try {
Connection con = Conexao.getConexao();
Statement stmt = con.createStatement();
stmt.executeUpdate("Insert into tab2_perfil (idPac, Data, Evolucao) values ('" + id + "', '" + valor
+ "', '" + valor2 + "')");
} catch (java.lang.NullPointerException e) {
JOptionPane.showMessageDialog(null, "Finalize a edição da tabela!");
} catch (Exception e) {
}
CustomEditor:
class CustomEditor implements TableCellEditor {
JTextArea textArea;
JScrollPane scrollPane;
public String text;
public CustomEditor() {
textArea = new JTextArea();
scrollPane = new JScrollPane(textArea);
}
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
textArea.setText((String) value);
return scrollPane;
}
public void addCellEditorListener(CellEditorListener l) {
}
public void cancelCellEditing() {
}
public Object getCellEditorValue() {
return textArea.getText();
}
public boolean isCellEditable(EventObject anEvent) {
return true;
}
public void removeCellEditorListener(CellEditorListener l) {
}
public boolean shouldSelectCell(EventObject anEvent) {
return true;
}
public boolean stopCellEditing() {
return true;
}
}
Custom Renderer:
class CustomRenderer implements TableCellRenderer {
JScrollPane scrollPane;
JTextArea textArea;
public CustomRenderer() {
textArea = new JTextArea();
scrollPane = new JScrollPane(textArea);
}
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
textArea.setText((String) value);
return scrollPane;
}
}