Advertisement
Guest User

Untitled

a guest
Apr 29th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. public void preenche_evolucao(){ //Pega do banco de dados
  2.  
  3. DefaultTableModel modelo = (DefaultTableModel)table_1.getModel();
  4. modelo.setNumRows(0);
  5. table_1.getColumnModel().getColumn(2).setCellRenderer(new CustomRenderer());
  6. table_1.getColumnModel().getColumn(2).setCellEditor(new CustomEditor());
  7. table_1.setRowHeight(50);
  8. try{
  9.  
  10. Connection con = Conexao.getConexao();
  11. Statement stmt=con.createStatement();
  12. ResultSet RS=stmt.executeQuery("SELECT id, Data, Evolucao FROM tab2_perfil where IdPac='"+id+"'");
  13.  
  14. while(RS.next()){
  15. String dat = RS.getString("Data");
  16. String tab=RS.getString("Evolucao");
  17. String id = RS.getString("id");
  18.  
  19. modelo.addRow(new Object[] {id, dat, tab});
  20. }
  21. }catch(Exception E){
  22.  
  23. }
  24.  
  25.  
  26. public void salvaEv() throws java.lang.NullPointerException{ //Pega o que é escrito na célula para passar ao banco de dados
  27.  
  28. int row = table_1.getSelectedRow();
  29. Object valor = table_1.getValueAt(row, 0).toString();
  30. Object valor2 = table_1.getValueAt(row, 1).toString();
  31.  
  32. try{
  33. Connection con = Conexao.getConexao();
  34. Statement stmt = con.createStatement();
  35. stmt.executeUpdate("Insert into tab2_perfil (idPac, Data, Evolucao) values ('"+id+"', '"+valor+"', '"+valor2+"')");
  36. }catch(java.lang.NullPointerException e){
  37. JOptionPane.showMessageDialog(null, "Finalize a edição da tabela!");
  38. }
  39. catch(Exception e){
  40.  
  41. }
  42. }
  43.  
  44. class CustomEditor implements TableCellEditor
  45. {
  46. JTextArea textArea;
  47. JScrollPane scrollPane;
  48. public String text;
  49.  
  50. public CustomEditor()
  51. {
  52. textArea = new JTextArea();
  53. scrollPane = new JScrollPane(textArea);
  54. }
  55.  
  56. public Component getTableCellEditorComponent(JTable table,
  57. Object value,
  58. boolean isSelected,
  59. int row, int column)
  60. {
  61. textArea.setText((String)value);
  62. return scrollPane;
  63. }
  64.  
  65. public void addCellEditorListener(CellEditorListener l) { }
  66. public void cancelCellEditing() { }
  67. public Object getCellEditorValue()
  68. {
  69. return textArea.getText();
  70. }
  71. public boolean isCellEditable(EventObject anEvent)
  72. {
  73. return true;
  74. }
  75. public void removeCellEditorListener(CellEditorListener l) { }
  76. public boolean shouldSelectCell(EventObject anEvent)
  77. {
  78. return true;
  79. }
  80. public boolean stopCellEditing()
  81. {
  82. return true;
  83. }
  84. }
  85.  
  86. class CustomRenderer implements TableCellRenderer
  87. {
  88. JScrollPane scrollPane;
  89. JTextArea textArea;
  90.  
  91. public CustomRenderer()
  92. {
  93. textArea = new JTextArea();
  94. scrollPane = new JScrollPane(textArea);
  95. }
  96.  
  97. public Component getTableCellRendererComponent(JTable table,
  98. Object value,
  99. boolean isSelected,
  100. boolean hasFocus,
  101. int row, int column)
  102. {
  103. textArea.setText((String)value);
  104. return scrollPane;
  105. }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement