0
I know how to use the DefaultTableModel
is not the best way to make the tables, however I have not mastered anything of AbstractTableModel
, i have a table that is edited as Jtextarea, but when I type does not come in the table cell variable, I wonder how I get what was typed... the Cellrenderer is doing what I want, now I want him to take everything I type in textarea
of jTable
, follows the 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;
}
}
From one formatted in this code, it is difficult to read a code without indentation, full of empty spaces.;
– user28595
Okay, I gave a Ctrl+shift+f kkkkkk ve se para entender algo agora, resuming I just want to get what is typed in the jtable textarea, ta ta me returning null...
– Matheus Lopes
It is not clear the problem. Pick up where?
– user28595
Da para postar print aqui? , It would be very clear... I’ll explain better, I have the Defaulttablemodel, it has the fields id, date and evolution, id and date are standard, but the evolution needs to be in textarea format, the Celleditor does it for me, however
– Matheus Lopes
Can’t pick up where? Your celleditor is very strange, see a simple example of a celleditor, use it as a reference. http://www.java2s.com/Code/Java/Swing-JFC/CreatingaCustomTableCellEditorinaJTableComponent.htm
– user28595
Can’t get the values from the evolution column
– Matheus Lopes
With this editor you can save what you type in the textarea??
– user28595
Ah, I tested here your reference, Monday I adapt to my code, thank you friend
– Matheus Lopes
What I needed was just to get the data from the textarea, but the Celleditor was wrong, I could not, but with its simplified example in my test worked well, agr just adapt to my code
– Matheus Lopes
How I put a scroll bar?
– Matheus Lopes