Results 1 to 5 of 5
  1. #1
    stefan_banu is offline Member
    Join Date
    May 2013
    Posts
    7
    Rep Power
    0

    Default how to delete, update rows in AbstractTableModel

    hi guys i have 3 classes, i want to take input from the user store it in jtable and after adding rows, deleting rows
    this are the classes:
    GuiTable: // the gui with the jtable
    Java Code:
    public class GuiTable extends javax.swing.JFrame {
     GrupPersoane model = new GrupPersoane();
        /**
         * Creates new form GuiTable
         */
        public GuiTable() {
            initComponents();
            jTable2.setModel(model);
            jTable2.setFillsViewportHeight(true);
        }
    
        /**
         * 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() {
    
            jLabel1 = new javax.swing.JLabel();
            Tnume = new javax.swing.JTextField();
            Tprenume = new javax.swing.JTextField();
            TCNP = new javax.swing.JTextField();
            TNrTel = new javax.swing.JTextField();
            jLabel2 = new javax.swing.JLabel();
            jLabel3 = new javax.swing.JLabel();
            jLabel4 = new javax.swing.JLabel();
            jButton1 = new javax.swing.JButton();
            jScrollPane2 = new javax.swing.JScrollPane();
            jTable2 = new javax.swing.JTable();
            jButton2 = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            jLabel1.setText("Nume");
    
            jLabel2.setText("Prenume");
    
            jLabel3.setText("CNP");
    
            jLabel4.setText("NrTel");
    
            jButton1.setText("Adauga");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
    
            jTable2.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"
                }
            ));
            jScrollPane2.setViewportView(jTable2);
    
            jButton2.setText("Delete");
            jButton2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton2ActionPerformed(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()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel1)
                                .addComponent(jLabel2)
                                .addComponent(jLabel3)
                                .addComponent(jLabel4))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(Tprenume, javax.swing.GroupLayout.DEFAULT_SIZE, 61, Short.MAX_VALUE)
                                .addComponent(TCNP)
                                .addComponent(TNrTel)
                                .addComponent(Tnume))
                            .addGap(48, 48, 48))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(20, 20, 20)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jButton2)
                                .addComponent(jButton1))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 289, javax.swing.GroupLayout.PREFERRED_SIZE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(73, 73, 73)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel1)
                                .addComponent(Tnume, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(Tprenume, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel2))
                            .addGap(18, 18, 18)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(TCNP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel3))
                            .addGap(28, 28, 28)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(TNrTel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel4))
                            .addGap(55, 55, 55)
                            .addComponent(jButton1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(jButton2))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(59, 59, 59)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 264, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>
    
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    
    
            String nume = Tnume.getText();
            String prenume = Tprenume.getText();
             String CNP = TCNP.getText();
              String nrTel = TNrTel.getText();
    
    
            Person p = new Person(nume, prenume, CNP,nrTel);
    
    
            model.adauga(p);
        }                                        
    
        private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    
    
      
    
        }
    
        /**
         * @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(GuiTable.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(GuiTable.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(GuiTable.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(GuiTable.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 GuiTable().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify
        private javax.swing.JTextField TCNP;
        private javax.swing.JTextField TNrTel;
        private javax.swing.JTextField Tnume;
        private javax.swing.JTextField Tprenume;
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JLabel jLabel4;
        private javax.swing.JScrollPane jScrollPane2;
        private javax.swing.JTable jTable2;
        // End of variables declaration
    }
    Person: // the propertys of object Person
    Java Code:
    public class Person {
    
        String nume;
        String prenume;
        String CNP;
        String nrTel;
    
        public Person(String nume, String prenume, String CNP, String nrTel) {
            this.nume = nume;
            this.prenume = prenume;
            this.CNP = CNP;
            this.nrTel = nrTel;
        }
    
    
        public String getNume() {
            return nume;
        }
    
        public void setNume(String nume) {
            this.nume = nume;
        }
    
        public String getPrenume() {
            return prenume;
        }
    
        public void setPrenume(String prenume) {
            this.prenume = prenume;
        }
    
        public String getCNP() {
            return CNP;
        }
    
        public void setCNP(String CNP) {
            this.CNP = CNP;
        }
    
        public String getNrTel() {
            return nrTel;
        }
    
        public void setNrTel(String nrTel) {
            this.nrTel = nrTel;
        }
    
        @Override
        public String toString() {
            return "Person{" + "nume=" + nume + ", prenume=" + prenume + ", CNP=" + CNP + ", nrTel=" + nrTel + '}';
        }
    
        
    
    }
    GrupPersoane: // the class the extends AbstractTableModel
    Java Code:
    public class GrupPersoane extends AbstractTableModel {
    
        GuiTable gui = new GuiTable();
    
        ArrayList<Person> persoane = new ArrayList<>();
    
        private static final String[] COLUMN_NAMES = {"Nume", "Prenume", "CNP" ,"NrTel"};
    
    
        public void adauga(Person p){
    
            persoane.add(p);
    
            fireTableDataChanged();
        }
    
    
        @Override
        public int getRowCount() {
          return persoane.size();
        }
    
        public String getColumnName(int col) {
        return COLUMN_NAMES[col];
    }
    
        @Override
        public int getColumnCount() {
           return COLUMN_NAMES.length;
        }
         public boolean isCellEditable(int row, int col) {
    
             return true;
         }
    
    
        @Override
        public Object getValueAt(int row, int col) {
    
    
          switch (col){
            case 0:
        return persoane.get(row).getNume();
    
             case 1:
          return  persoane.get(row).getPrenume();
    
             case 2:
            return persoane.get(row).getCNP();
    
    
             case 3:
             return persoane.get(row).getNrTel();
    
                 default:
                     System.out.println("no choices");
    
    
          }
          return null;
        }
    
    public void removeRow(int row)
    {
    
        persoane.remove(row);
        fireTableRowsDeleted(row, row);
    }
    }
    I've succeed with getting input and display in the jtable, but the informations are not updating, and dont know how to delete a row, how to insert a new empty row etc

    if someone can help me pls

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: how to delete, update rows in AbstractTableModel

    Moved from Advanced Java.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,023
    Rep Power
    6

    Default Re: how to delete, update rows in AbstractTableModel

    If you really want to understand what is going on I suggest two things. First, stay away from GUI Designers. They muddle the code and you will never really understand what is going on. Second, check out How to Use Tables (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components) and write some simple programs to get the feel of all that is discussed.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    stefan_banu is offline Member
    Join Date
    May 2013
    Posts
    7
    Rep Power
    0

    Default Re: how to delete, update rows in AbstractTableModel

    OK, thanks for your answer

    i'll take note of what you said

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: how to delete, update rows in AbstractTableModel

    how to delete, update rows in AbstractTableModel
    You are not using an AbstractTableModel. You are using a DefaultTableModel. Read the API. The DefaultTableModel has methods for dynamically adding/removing rows.

Similar Threads

  1. Replies: 2
    Last Post: 03-16-2012, 03:39 PM
  2. How to update rows in 2 D arrays?
    By udayjava in forum New To Java
    Replies: 2
    Last Post: 02-13-2012, 07:38 AM
  3. Replies: 0
    Last Post: 03-03-2011, 03:20 PM
  4. Update Table GUI that extends custom AbstractTableModel
    By nidhirastogi in forum AWT / Swing
    Replies: 0
    Last Post: 02-09-2011, 04:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •