Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By KevinWorkman

Thread: trying to use AbstractTableModel

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

    Default trying to use AbstractTableModel

    hi guys,
    this is my code, but i need help with the displaying in jtable

    classes :
    class GrupPersoane
    Java Code:
    import java.util.ArrayList;
    import javax.swing.table.AbstractTableModel;
    
    /**
     *
     * @author Stefan
     */
    public class GrupPersoane extends AbstractTableModel {
    
    
        ArrayList<Person> persoane = new ArrayList<>();
    
        private static final String[] COLUMN_NAMES = {"Nume", "Prenume", "CNP" ,"NrTel"};
    
    
        public void adauga(Person p){
    
            persoane.add(p);
        }
    
    
    
        @Override
        public int getRowCount() {
          return persoane.size();
        }
    
        public String getColumnName(int col) {
        return COLUMN_NAMES[col];
    }
    
        @Override
        public int getColumnCount() {
           return COLUMN_NAMES.length;
        }
    
        @Override
        public Object getValueAt(int row, int col) {
    
    
          switch (col){
            case 1:
         persoane.get(row).getNume();
                break;
             case 2:
            persoane.get(row).getPrenume();
                 break;
             case 3:
             persoane.get(row).getCNP();
                    break;
    
             case 4:
             persoane.get(row).getNrTel();
                    break;
                 default:
                     System.out.println("bla bla");
    
    
          }
          return null;
        }
    }
    class 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 + '}';
        }
    
        
    
    }
    and the last class is the GUI
    Java Code:
    public class GuiTable extends javax.swing.JFrame {
     GrupPersoane model = new GrupPersoane();
        /**
         * Creates new form GuiTable
         */
        public GuiTable() {
            initComponents();
            jTable2.setModel(model);
        }
    
        /**
         * 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();
    
            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);
    
            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)
                            .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))
                        .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);
        }                                        
    
        /**
         * @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.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                   
    }
    this is made in netbeans so the last class GuiTable is standard you can look at the method jButton1ActionPerformed
    that is the button for adding the rows in jtable,

    the problem is that after i fill in the textfields and push the button jButton1ActionPerformed nothing displaying in jtable
    and have no rows just the title of the jtable

    i want to insert rows, to delete rows and editing
    if someone know why is not working?

    thanks

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default Re: trying to use AbstractTableModel

    The best advice I can offer is to ditch the gui builder until you know what's going on under the hood. Recommended reading: How to Use Tables (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
    PhHein likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. JTable not updating with my AbstractTableModel
    By agarta in forum New To Java
    Replies: 5
    Last Post: 03-31-2013, 10:34 PM
  2. creating a AbstractTableModel
    By darrenw in forum AWT / Swing
    Replies: 0
    Last Post: 10-31-2010, 12:04 PM
  3. The right way to inherit AbstractTableModel
    By skywriter in forum New To Java
    Replies: 3
    Last Post: 10-19-2010, 11:28 AM
  4. DefaultTableModel vs AbstractTableModel
    By chyrl in forum AWT / Swing
    Replies: 4
    Last Post: 05-02-2010, 12:10 PM
  5. Replies: 3
    Last Post: 11-07-2009, 05:25 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
  •