Results 1 to 3 of 3
  1. #1
    Dinesh_rockz is offline Member
    Join Date
    Jan 2011
    Location
    India
    Posts
    22
    Rep Power
    0

    Thumbs up [SOLVED]How to Add,Remove Rows in JTable

    hi to all i need add a new rows with existing values,and remove the existing values in JTable

    here my code
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JTable;
    import javax.swing.table.AbstractTableModel;
    import javax.swing.*;
    
    
    public class user_table1 extends JDialog implements ActionListener{
    JTable tab;
    JButton print_but,add_but,rem_but;
    mymodel md;
    
        public user_table1() {
            super();
             setLayout(null);
            setTitle("Table Demo...");
         md=new mymodel();
            tab=new JTable(md);
          
            JScrollPane js=new JScrollPane(tab,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
            js.setBounds(0,0,500,300);
            add(js);
    
            print_but=new JButton("Print Data");
            print_but.addActionListener(this);
            print_but.setBounds(20,410,120,30);
            add(print_but);    
            add_but=new JButton("Add New User");
            add_but.setBounds(160,410,120,30);
       	add_but.addActionListener(this);
            add(add_but);
    
            rem_but=new JButton("Remove User");
            rem_but.setBounds(300,410,120,30);
            rem_but.addActionListener(this);
            add(rem_but);
        
            setSize(500,500);
    
        }
    
         public void actionPerformed(ActionEvent e) {
           if(e.getSource().equals(print_but))
           {
               md.printdata();
           }
    
           if(e.getSource().equals(add_but))
    
           {
    
           //Here i need Code For Adding New Row with Existing Value
    
           }
    
           if(e.getSource().equals(rem_but))
    
           {
    
           	//Here i need Code For Remove the Seleced row Value in JTable
    
           }
    
           
    
           
    
           
    
        }
    
        public static void main(String sr[])
    
    {
    
        
    
                    user_table1 dialog = new user_table1();
                    dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                        public void windowClosing(java.awt.event.WindowEvent e) {
                            System.exit(0);
                        }
    
                        });
    
                    dialog.setVisible(true);
    
                }
    
            
    
    
    
    
    
       
    
    
    
    }
    
    class mymodel extends AbstractTableModel
    
    {
      String[] col={"User name","Enabled..."};
    Object [][]  data ={{"Dinesh",new Boolean(true)},{"Kamal",new Boolean(false)}};
    
    Class[] types = new Class [] {java.lang.String.class,java.lang.Boolean.class };
    
                public Class getColumnClass(int columnIndex) {
                    return types [columnIndex];
    
                }
    
        public int getRowCount() {
    
            return data.length;
    
        }
    
        public int getColumnCount() {
    
           return col.length;
    
        }
    
        public Object getValueAt(int rowIndex, int columnIndex)
    
        {
           return data[rowIndex][columnIndex];
        }
    
        public boolean isCellEditable(int row, int col) {
    
                if (col < 0) { 
    
                    return false;
    
                } else {
    
                    return true;
    
                }
    
            }
    
         public void setValueAt(Object value, int row, int col) {
                System.out.println (row+" ,"+col);
                   data[row][col] = value;
    
                }
    
                public void printdata()
    
                {
                    int numrow=getRowCount();
                    int numcol=getColumnCount();
                    System.out.println("UserName\tEnabled");
                    for(int i=0;i<numrow;i++)
                    {
                       for (int j = 0; j < numcol; j++) {
                            System.out.print(data[i][j].toString());
                            System.out.print("\t\t");
                            
                        }
                      System.out.println();
    
                    }
    
                }
    
    }
    please give some code or examples for this problem

    regards,
    Dinesh...:)
    Last edited by Dinesh_rockz; 02-04-2011 at 01:50 AM. Reason: Problem Solved..

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I'd use a class that extends DefaultTableModel rather than AbstractTableModel, then you could simply call the DefaultTableModel's methods.

  3. #3
    Dinesh_rockz is offline Member
    Join Date
    Jan 2011
    Location
    India
    Posts
    22
    Rep Power
    0

    Default

    Thanks For your Reply sir

    i changed AbstractTableModel class into DefaultTableModel its working well now
    here the code

    Java Code:
    import javax.swing.table.*;
    import java.awt.*;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.event.TableModelEvent;
    import java.awt.event.*;
    
    /**
     *Append the New Row in JTable,Remove the Row in JTable 
     * @author  Dinesh Kumar
     */
    
    
    public class user_table1 extends JDialog implements ActionListener{
    JTable tab;
    JButton print_but,add_but,rem_but;
    DefaultTableModel md;
    
        public user_table1() {
            super();
            setLayout(new BorderLayout());
            setTitle("Table Demo...");
            add_but=new JButton("AddNew User");
            rem_but=new JButton("Remove User");
             md=new javax.swing.table.DefaultTableModel(
                new Object [][] {
                    {"Dinesh", null},
                    {"Kumar", null},
                    
                },
                new String [] {
                    "user name", "Enabled"
                }
            ) {
                Class[] types = new Class [] {
                    java.lang.String.class, java.lang.Boolean.class
                };
    
                public Class getColumnClass(int columnIndex) {
                    return types [columnIndex];
                }
            };
            tab=new JTable(md);
        
            
            print_but=new JButton("Print Data");
            print_but.addActionListener(this);
            add(tab,BorderLayout.CENTER);
            add(print_but,BorderLayout.SOUTH);
            add(add_but,BorderLayout.NORTH);
            add(rem_but,BorderLayout.WEST);
    
            add_but.addActionListener(this);
            rem_but.addActionListener(this);
            setSize(500,500);
            
        }
         public void actionPerformed(ActionEvent e) {
           if(e.getSource().equals(print_but))
           {
             int nu=tab.getRowCount();
             int co=tab.getColumnCount();
             for(int i=0;i<nu;i++)
             {
             	for(int j=0;j<co;j++)
             	{
             		System.out.print (tab.getModel().getValueAt(i,j)+"\t\t");
             	}
             	System.out.println ();
             }
               
           }
           if(e.getSource().equals(add_but))
           {
          md.addRow(new Object [][] {{"New User", null}});
            md.setValueAt("Dinesh Kumar",tab.getRowCount()-1,0);
           }
           if(e.getSource().equals(rem_but))
            {
                    if(tab.getSelectedRow()<0)
                    {
                     JOptionPane.showMessageDialog(this,"Select value To Remove");
                    
                    }
                    else
                    {
                           md.removeRow(tab.getSelectedRow());
                    }
            }
        }
        
        public static void main(String sr[])
    {
        java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    user_table1 dialog = new user_table1();
                    dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                        public void windowClosing(java.awt.event.WindowEvent e) {
                            System.exit(0);
                        }
                    });
                    dialog.setVisible(true);
                }
            });
    }
    
    }
    Happy Programming...:):):):D
    Last edited by Dinesh_rockz; 02-04-2011 at 01:39 AM.

Similar Threads

  1. Remove Jtable rows
    By anilkumar_vist in forum Advanced Java
    Replies: 2
    Last Post: 09-17-2010, 08:03 AM
  2. Listening rows in JTable ??
    By Stephen Douglas in forum New To Java
    Replies: 2
    Last Post: 04-10-2010, 04:45 PM
  3. Highlight rows of a jtable
    By simmi in forum AWT / Swing
    Replies: 5
    Last Post: 08-20-2009, 01:13 PM
  4. Deleting All rows in the JTable
    By surot in forum New To Java
    Replies: 1
    Last Post: 04-16-2008, 10:44 AM
  5. display rows in jtable
    By osval in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 08:54 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
  •