Advertisement
Guest User

llistar

a guest
Nov 24th, 2014
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.66 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package MP3UF5GUIEx1;
  7.  
  8. import java.io.File;
  9. import java.io.FileInputStream;
  10. import java.io.IOException;
  11. import java.io.ObjectInputStream;
  12. import java.util.ArrayList;
  13. import javax.swing.ListSelectionModel;
  14. import javax.swing.table.DefaultTableModel;
  15. import javax.swing.table.TableModel;
  16. import javax.swing.table.TableRowSorter;
  17.  
  18. /**
  19. *
  20. * @author galandriel
  21. */
  22. public class Llistar extends javax.swing.JFrame {
  23.  
  24. /**
  25. * Creates new form Llistar
  26. */
  27. static final int LONG=15;
  28. TableRowSorter<TableModel> sorter;
  29. public Llistar() {
  30. initComponents();
  31. CarregarDades();
  32. }
  33.  
  34. public void CarregarDades(){
  35. ArrayList<Alumne> llista = new ArrayList<>();
  36. int total = 0;
  37. try {
  38. File fitxer=new File("datos.dat");
  39. ObjectInputStream in = new ObjectInputStream(new FileInputStream(fitxer));
  40. String[] columnNames = {
  41. "Nom",
  42. "Cognoms",
  43. "Edat",
  44. "DNI",
  45. "Telèfon",
  46. "E-mail",
  47. "MPs"
  48. };
  49. for(int i=0;true;){
  50. try{
  51. llista.add((Alumne)in.readObject());
  52. }
  53. //Si arribem al final del vector ho indiquem, decrementem l'índex i sortim del bucle infinit
  54. catch(ArrayIndexOutOfBoundsException ex){
  55. i--;
  56. break;
  57. }
  58. //Quan arribem al final del fitxer sortim del bucle infinit
  59. catch(Exception ex){
  60. i--;
  61. break;
  62. }
  63. }
  64. in.close();
  65. Object[][] data = new Object[llista.size()][columnNames.length];
  66. for(int i=0;i<llista.size();i++){
  67. data[i][0] = llista.get(i).getNom();
  68. data[i][1] = llista.get(i).getCognoms();
  69. data[i][2] = llista.get(i).getEdat();
  70. data[i][3] = llista.get(i).getDni();
  71. data[i][4] = llista.get(i).getTelefon();
  72. data[i][5] = llista.get(i).getEmail();
  73. data[i][6] = llista.get(i).getModuls().toString();
  74. }
  75. DefaultTableModel dftm = new DefaultTableModel(data, columnNames);
  76. Tabla.setModel(dftm);
  77. sorter = new TableRowSorter<TableModel>(dftm);
  78. Tabla.setRowSorter(sorter);
  79. Tabla.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  80. } catch (IOException ex) {
  81. System.out.println("Error al carregar les dades");
  82. }
  83. }
  84. /**
  85. * This method is called from within the constructor to initialize the form.
  86. * WARNING: Do NOT modify this code. The content of this method is always
  87. * regenerated by the Form Editor.
  88. */
  89. @SuppressWarnings("unchecked")
  90. // <editor-fold defaultstate="collapsed" desc="Generated Code">
  91. private void initComponents() {
  92.  
  93. jScrollPane1 = new javax.swing.JScrollPane();
  94. Tabla = new javax.swing.JTable();
  95. cancelar = new javax.swing.JButton();
  96.  
  97. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  98. setPreferredSize(new java.awt.Dimension(617, 364));
  99.  
  100. Tabla.setModel(new javax.swing.table.DefaultTableModel(
  101. new Object [][] {
  102. {null, null, null, null},
  103. {null, null, null, null},
  104. {null, null, null, null},
  105. {null, null, null, null}
  106. },
  107. new String [] {
  108. "Title 1", "Title 2", "Title 3", "Title 4"
  109. }
  110. ));
  111. jScrollPane1.setViewportView(Tabla);
  112.  
  113. cancelar.setText("Cancel·lar");
  114. cancelar.addActionListener(new java.awt.event.ActionListener() {
  115. public void actionPerformed(java.awt.event.ActionEvent evt) {
  116. cancelarActionPerformed(evt);
  117. }
  118. });
  119.  
  120. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  121. getContentPane().setLayout(layout);
  122. layout.setHorizontalGroup(
  123. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  124. .addGroup(layout.createSequentialGroup()
  125. .addContainerGap()
  126. .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 597, Short.MAX_VALUE)
  127. .addContainerGap())
  128. .addGroup(layout.createSequentialGroup()
  129. .addGap(261, 261, 261)
  130. .addComponent(cancelar)
  131. .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  132. );
  133. layout.setVerticalGroup(
  134. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  135. .addGroup(layout.createSequentialGroup()
  136. .addContainerGap()
  137. .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 285, javax.swing.GroupLayout.PREFERRED_SIZE)
  138. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  139. .addComponent(cancelar)
  140. .addContainerGap(23, Short.MAX_VALUE))
  141. );
  142.  
  143. pack();
  144. setLocationRelativeTo(null);
  145. }// </editor-fold>
  146.  
  147. private void cancelarActionPerformed(java.awt.event.ActionEvent evt) {
  148. this.setVisible(false);
  149. }
  150.  
  151. /**
  152. * @param args the command line arguments
  153. */
  154. public static void main(String args[]) {
  155. /* Set the Nimbus look and feel */
  156. //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  157. /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  158. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  159. */
  160. try {
  161. for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  162. if ("Nimbus".equals(info.getName())) {
  163. javax.swing.UIManager.setLookAndFeel(info.getClassName());
  164. break;
  165. }
  166. }
  167. } catch (ClassNotFoundException ex) {
  168. java.util.logging.Logger.getLogger(Llistar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  169. } catch (InstantiationException ex) {
  170. java.util.logging.Logger.getLogger(Llistar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  171. } catch (IllegalAccessException ex) {
  172. java.util.logging.Logger.getLogger(Llistar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  173. } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  174. java.util.logging.Logger.getLogger(Llistar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  175. }
  176. //</editor-fold>
  177.  
  178. /* Create and display the form */
  179. java.awt.EventQueue.invokeLater(new Runnable() {
  180. public void run() {
  181. new Llistar().setVisible(true);
  182. }
  183. });
  184. }
  185.  
  186. // Variables declaration - do not modify
  187. private javax.swing.JTable Tabla;
  188. private javax.swing.JButton cancelar;
  189. private javax.swing.JScrollPane jScrollPane1;
  190. // End of variables declaration
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement