Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package MP3UF5GUIEx1;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.util.ArrayList;
- import javax.swing.ListSelectionModel;
- import javax.swing.table.DefaultTableModel;
- import javax.swing.table.TableModel;
- import javax.swing.table.TableRowSorter;
- /**
- *
- * @author galandriel
- */
- public class Llistar extends javax.swing.JFrame {
- /**
- * Creates new form Llistar
- */
- static final int LONG=15;
- TableRowSorter<TableModel> sorter;
- public Llistar() {
- initComponents();
- CarregarDades();
- }
- public void CarregarDades(){
- ArrayList<Alumne> llista = new ArrayList<>();
- int total = 0;
- try {
- File fitxer=new File("datos.dat");
- ObjectInputStream in = new ObjectInputStream(new FileInputStream(fitxer));
- String[] columnNames = {
- "Nom",
- "Cognoms",
- "Edat",
- "DNI",
- "Telèfon",
- "E-mail",
- "MPs"
- };
- for(int i=0;true;){
- try{
- llista.add((Alumne)in.readObject());
- }
- //Si arribem al final del vector ho indiquem, decrementem l'índex i sortim del bucle infinit
- catch(ArrayIndexOutOfBoundsException ex){
- i--;
- break;
- }
- //Quan arribem al final del fitxer sortim del bucle infinit
- catch(Exception ex){
- i--;
- break;
- }
- }
- in.close();
- Object[][] data = new Object[llista.size()][columnNames.length];
- for(int i=0;i<llista.size();i++){
- data[i][0] = llista.get(i).getNom();
- data[i][1] = llista.get(i).getCognoms();
- data[i][2] = llista.get(i).getEdat();
- data[i][3] = llista.get(i).getDni();
- data[i][4] = llista.get(i).getTelefon();
- data[i][5] = llista.get(i).getEmail();
- data[i][6] = llista.get(i).getModuls().toString();
- }
- DefaultTableModel dftm = new DefaultTableModel(data, columnNames);
- Tabla.setModel(dftm);
- sorter = new TableRowSorter<TableModel>(dftm);
- Tabla.setRowSorter(sorter);
- Tabla.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
- } catch (IOException ex) {
- System.out.println("Error al carregar les dades");
- }
- }
- /**
- * This method is called from within the constructor to initialize the form.
- * WARNING: Do NOT modify this code. The content of this method is always
- * regenerated by the Form Editor.
- */
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- jScrollPane1 = new javax.swing.JScrollPane();
- Tabla = new javax.swing.JTable();
- cancelar = new javax.swing.JButton();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- setPreferredSize(new java.awt.Dimension(617, 364));
- Tabla.setModel(new javax.swing.table.DefaultTableModel(
- new Object [][] {
- {null, null, null, null},
- {null, null, null, null},
- {null, null, null, null},
- {null, null, null, null}
- },
- new String [] {
- "Title 1", "Title 2", "Title 3", "Title 4"
- }
- ));
- jScrollPane1.setViewportView(Tabla);
- cancelar.setText("Cancel·lar");
- cancelar.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- cancelarActionPerformed(evt);
- }
- });
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 597, Short.MAX_VALUE)
- .addContainerGap())
- .addGroup(layout.createSequentialGroup()
- .addGap(261, 261, 261)
- .addComponent(cancelar)
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 285, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(cancelar)
- .addContainerGap(23, Short.MAX_VALUE))
- );
- pack();
- setLocationRelativeTo(null);
- }// </editor-fold>
- private void cancelarActionPerformed(java.awt.event.ActionEvent evt) {
- this.setVisible(false);
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String args[]) {
- /* Set the Nimbus look and feel */
- //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
- /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
- * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
- */
- try {
- for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
- if ("Nimbus".equals(info.getName())) {
- javax.swing.UIManager.setLookAndFeel(info.getClassName());
- break;
- }
- }
- } catch (ClassNotFoundException ex) {
- java.util.logging.Logger.getLogger(Llistar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(Llistar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(Llistar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(Llistar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- //</editor-fold>
- /* Create and display the form */
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new Llistar().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JTable Tabla;
- private javax.swing.JButton cancelar;
- private javax.swing.JScrollPane jScrollPane1;
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement