Advertisement
Guest User

Untitled

a guest
Dec 19th, 2014
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. //===============Cari method tampilTabel(); kemudian ganti kodennya jadi ini
  2.  
  3. public void tampilTabel() {
  4. if(lblId.getText().contains("E")){
  5. try {
  6. hapusIsiTabel();
  7. Connection con = (Connection) Koneksi.getKoneksi();
  8. Statement state = (Statement) con.createStatement();
  9. String query = "select distinct Target.IdKerusakan,Kerusakan.NmKerusakan from Target,Kerusakan where Kerusakan.IdKerusakan = Target.IdKerusakan and Target.IdGejala='"+lblId.getText()+"'";
  10. ResultSet rs = (ResultSet) state.executeQuery(query);
  11. while (rs.next()) {
  12. //tambahan
  13. Object obj[] = new Object[2];
  14. obj[0] = rs.getString(1);
  15. obj[1] = rs.getString(2);
  16. tabelModel.addRow(obj);
  17. sesuaikanKolom();
  18. }
  19. rs.close();
  20. state.close();
  21. } catch (Exception ex) {
  22. System.out.println(ex);
  23. }
  24. }
  25.  
  26. else if(lblId.getText().contains("D") || lblId.getText().contains("C") || lblId.getText().contains("B") || lblId.getText().contains("A")){
  27. try {
  28. hapusIsiTabelkedua();
  29. Connection con = (Connection) Koneksi.getKoneksi();
  30. Statement state = (Statement) con.createStatement();
  31. String query = "select distinct Target.IdKerusakan,Kerusakan.NmKerusakan from Target,Kerusakan where Kerusakan.IdKerusakan = Target.IdKerusakan and Target.IdGejala='"+lblId.getText()+"'";
  32. ResultSet rs = (ResultSet) state.executeQuery(query);
  33. while (rs.next()) {
  34.  
  35. // Query didalam query; untuk menampilkan gejala
  36. Statement state1 = (Statement) con.createStatement();
  37. String jalary = "select distinct Kerusakan.IdKerusakan,Kerusakan.NmKerusakan,Gejala.IdGejala ,Gejala.NmGejala from Target,Gejala,Kerusakan where Target.IdKerusakan = '"+ rs.getString(1) +"' and Gejala.IdGejala > '"+ lblId.getText() +"' and Gejala.IdGejala = Target.IdGejala and Target.IdKerusakan = Kerusakan.IdKerusakan";
  38. ResultSet rs1 = (ResultSet) state1.executeQuery(jalary);
  39. while(rs1.next()){
  40. Object obj1[] = new Object[4];
  41. obj1[0] = rs1.getString(1);
  42. obj1[1] = rs1.getString(2);
  43. obj1[2] = rs1.getString(3);
  44. obj1[3] = rs1.getString(4);
  45. tabelModel.addRow(obj1);
  46. sesuaikanKolom();
  47. }
  48. rs1.close();
  49. state1.close();
  50.  
  51. }
  52. rs.close();
  53. state.close();
  54. } catch (Exception ex) {
  55. System.out.println(ex);
  56. }
  57. }
  58. }
  59. void sesuaikanKolomkedua() {
  60. // cara untuk menyesuaikan kolom dari tabel adalah mengambil
  61. // lebar kolom yang ada kemudian sesuaikan
  62. TableColumnModel modelKolom = table.getColumnModel();
  63.  
  64. for (int kol = 0; kol < modelKolom.getColumnCount(); kol++) {
  65. int lebarKolomMax = 0;
  66. for (int baris = 0; baris < table.getRowCount(); baris++) {
  67. TableCellRenderer rend = table.getCellRenderer(baris, kol);
  68. Object nilaiTablel = table.getValueAt(baris, kol);
  69. Component comp = rend.getTableCellRendererComponent(table,
  70. nilaiTablel, false, false, baris, kol);
  71. lebarKolomMax = Math.max(comp.getPreferredSize().width,
  72. lebarKolomMax);
  73. }// akhir for baris
  74. TableColumn kolom = modelKolom.getColumn(kol);
  75. kolom.setPreferredWidth(lebarKolomMax);
  76. }// akhir for kolom
  77. }
  78.  
  79. public void hapusIsiTabelkedua() {
  80. int a = table.getRowCount();
  81. int brs;
  82.  
  83. for (brs = 0; brs < a; brs++) {
  84. tabelModel.removeRow(0);
  85.  
  86. }
  87. }
  88.  
  89.  
  90.  
  91. void sesuaikanKolom() {
  92. // cara untuk menyesuaikan kolom dari tabel adalah mengambil
  93. // lebar kolom yang ada kemudian sesuaikan
  94. TableColumnModel modelKolom = table.getColumnModel();
  95.  
  96. for (int kol = 0; kol < modelKolom.getColumnCount(); kol++) {
  97. int lebarKolomMax = 0;
  98. for (int baris = 0; baris < table.getRowCount(); baris++) {
  99. TableCellRenderer rend = table.getCellRenderer(baris, kol);
  100. Object nilaiTablel = table.getValueAt(baris, kol);
  101. Component comp = rend.getTableCellRendererComponent(table,
  102. nilaiTablel, false, false, baris, kol);
  103. lebarKolomMax = Math.max(comp.getPreferredSize().width,
  104. lebarKolomMax);
  105. }// akhir for baris
  106. TableColumn kolom = modelKolom.getColumn(kol);
  107. kolom.setPreferredWidth(lebarKolomMax);
  108. }// akhir for kolom
  109. }
  110.  
  111. public void hapusIsiTabel() {
  112. int a = table.getRowCount();
  113. int brs;
  114.  
  115. for (brs = 0; brs < a; brs++) {
  116. tabelModel.removeRow(0);
  117.  
  118. }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement