0

The add() method is supposed to add data from file to table but that does not work. The design is for a phone book using table. The add button adds row to table and write the data to the existing file.

package democardlayout;

public class panel2 extends javax.swing.JPanel {

        public ObjectInputStream in;
        public ObjectOutputStream out;
        public panel2() {
            initComponents();
           // addRow(1);
           //  add();

        }

        @SuppressWarnings("unchecked")

        private void initComponents() {

            jLabel1 = new javax.swing.JLabel();
            jScrollPane1 = new javax.swing.JScrollPane();
            PhoneBookTable = new javax.swing.JTable();
            NameField = new javax.swing.JTextField();
            PhoneField = new javax.swing.JTextField();
            EmailField = new javax.swing.JTextField();
            AddButton = new javax.swing.JButton();
            UpdateButton = new javax.swing.JButton();
            DeleteButton = new javax.swing.JButton();
            TableData = new javax.swing.JButton();

            jLabel1.setBackground(new java.awt.Color(0, 240, 240));
            jLabel1.setFont(new java.awt.Font("Arial", 3, 12)); // NOI18N
            jLabel1.setText("Welcome");

            PhoneBookTable.setModel(new javax.swing.table.DefaultTableModel(
                new Object [][] {

                },
                new String [] {
                    "Name", "PhoneNumber", "Email"
                }
            ));
            jScrollPane1.setViewportView(PhoneBookTable);

            AddButton.setText("Add");
            AddButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    AddButtonActionPerformed(evt);
                }
            });

            UpdateButton.setText("Update");

            DeleteButton.setText("Delete");

            TableData.setText("ShowTableData");
            TableData.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    TableDataActionPerformed(evt);
                }
            });

            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
            this.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()
                            .addGap(35, 35, 35)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(NameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(46, 46, 46)
                                    .addComponent(PhoneField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(40, 40, 40)
                                    .addComponent(EmailField, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(AddButton, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(29, 29, 29)
                                    .addComponent(UpdateButton)
                                    .addGap(27, 27, 27)
                                    .addComponent(DeleteButton))
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 368, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(161, 161, 161)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(TableData))
                    .addContainerGap(30, Short.MAX_VALUE))
            );

            layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {EmailField, NameField, PhoneField});

            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(TableData)
                    .addGap(8, 8, 8)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 245, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(NameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(PhoneField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(EmailField, 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(AddButton, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(UpdateButton)
                        .addComponent(DeleteButton))
                    .addContainerGap(46, Short.MAX_VALUE))
            );

            layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {AddButton, DeleteButton, UpdateButton});

        }

        public  void add()
        {
            try
            {
             in=new ObjectInputStream(new FileInputStream("record.ser"));
            while(true)
            {
                PhoneRecord1 ob=(PhoneRecord1)in.readObject();
                Vector<String>v=new Vector(3);
                v.add(ob.Name);
                v.add(ob.Phone);
                v.add(ob.Email);
                System.out.println(ob.Name+" "+ob.Phone+" "+ob.Email);
                DefaultTableModel model=(DefaultTableModel)PhoneBookTable.getModel();
                model.addRow(v);
            }
            }
            catch(Exception e)
            {

            }


        }

      /*  public void addRow(int N)
        {
            Vector<String>v=new Vector(3);
            v.clear();
            for(int i=0;i<N;i++)
            {
                DefaultTableModel model=(DefaultTableModel)PhoneBookTable.getModel();
                model.addRow(v);
            }


        }*/
        private void AddButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          
                DefaultTableModel model=(DefaultTableModel)PhoneBookTable.getModel();
                Vector<String> a=new Vector(3);
                a.add(NameField.getText());
                a.add(PhoneField.getText());
                a.add(EmailField.getText());
               String s=a.elementAt(0);
               String s1=a.elementAt(1);
               String s2=a.elementAt(2);
                model.addRow(a);
                //System.out.println(a.elementAt(0)+" "+ a.elementAt(1)+" "+a.elementAt(2));
                try
                {
                  out=new ObjectOutputStream(new FileOutputStream("record.ser",true));
                 PhoneRecord1 ob1=new PhoneRecord1(s,s1,s2);
                 System.out.println(ob1.Name);
                 out.writeObject(ob1);
                 out.close();


                }
                catch(Exception e)
                {

                }
               // model.setValueAt("line",1,1);
                //model.addRow("i","a","v");
                //add();



               // model.addRow(new Object[]{NameField.getText(),PhoneField.getText(),EmailField.getText()});

        }                                         

        private void TableDataActionPerformed(java.awt.event.ActionEvent evt) {                                          
                    add();        // TODO add your handling code here:
        }                                         


        // Variables declaration - do not modify                     
        private javax.swing.JButton AddButton;
        private javax.swing.JButton DeleteButton;
        private javax.swing.JTextField EmailField;
        private javax.swing.JTextField NameField;
        private javax.swing.JTable PhoneBookTable;
        private javax.swing.JTextField PhoneField;
        private javax.swing.JButton TableData;
        private javax.swing.JButton UpdateButton;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JScrollPane jScrollPane1;
        // End of variables declaration                   
    }


/* the class for phone record*/
package democardlayout;


    public class PhoneRecord1 {
        public String Name="";
        public String Phone="";
        public String Email="";

        public PhoneRecord1(String Name, String Phone, String Email) {
            this.Name = Name;
            this.Phone = Phone;
            this.Email = Email;
        }

    }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
newbie
  • 41
  • 6
  • 2
    Remove your empty `catch (Exception)` blocks. You're ignoring an exception. So, if one is thrown, you're not even aware of it. And learn using your debugger, to step through the code line by line and see what happens. – JB Nizet Oct 24 '14 at 07:37
  • I dont understand exception well thats why i used system.out.println to see if it actually works but it does not i think theres an error but cant understand thats why i need help – newbie Oct 24 '14 at 07:41
  • *"but cant understand thats why i need help"* There is little chance we can help without the stack-trace. For each catch add `e.printStackTrace();` inside the curly brackets. Then copy/paste the output as an [edit to the question](http://stackoverflow.com/posts/26543411/edit). – Andrew Thompson Oct 24 '14 at 08:12
  • 1
    See also [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) – Andrew Thompson Oct 24 '14 at 08:13
  • Variable names should NOT start with an upper case character. Half of your names are correct and half are not. Be consistent!!! – camickr Oct 24 '14 at 15:05

0 Answers0