Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Remove JTable row that read txt file records

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Remove JTable row that read txt file records

    Hi every one.
    I have a jtable that correctly read data from file and show them in own. I want to add a "Delete" button that when select a row and clicked button, row must deleted. But, when i click the button, row don't deleted and a ArrayOutOutBoundsException occur.

    my whole code is this:
     import java.awt.event.ActionEvent;  
        import java.awt.event.ActionListener;  
        import java.io.BufferedReader;  
        import java.io.FileInputStream;  
        import java.io.InputStreamReader;  
        import java.util.StringTokenizer;  
        import java.util.Vector;  
        import javax.swing.JButton;  
        import javax.swing.JFrame;  
        import javax.swing.JPanel;  
        import javax.swing.JScrollPane;  
        import javax.swing.JTable;  
        import javax.swing.table.AbstractTableModel;  
     
        public class RemoveRow extends AbstractTableModel{  
     
        Vector data;  
        Vector columns;  
        public RemoveRow() {  
     
                String line;  
                data = new Vector();  
                columns = new Vector();  
                try {  
                FileInputStream fis = new FileInputStream("D:\\AllUserRecords.txt");  
                BufferedReader br = new BufferedReader(new InputStreamReader(fis));  
              StringTokenizer st1 = new StringTokenizer(br.readLine(), " ");  
                      while (st1.hasMoreTokens())  
                       columns.addElement(st1.nextToken());  
                        while ((line = br.readLine()) != null) {  
                               StringTokenizer st2 = new StringTokenizer(line, " ");  
                                while (st2.hasMoreTokens())  
                                        data.addElement(st2.nextToken());  
                        }  
                        br.close();  
                } catch (Exception e) {  
                        e.printStackTrace();  
                }  
        }  
     
            public int getRowCount() {  
                return data.size() / getColumnCount();  
        }  
     
        public int getColumnCount() {  
                return columns.size();  
        }  
     
        public Object getValueAt(int rowIndex, int columnIndex) {  
                return (String) data.elementAt((rowIndex * getColumnCount())  
                                + columnIndex);  
        }  
        public static void main(String[] args){  
     
            final RemoveRow rR1=new RemoveRow();  
            JFrame frame=new JFrame();  
            final JTable table=new JTable();  
            table.setModel(rR1);  
            JPanel panel=new JPanel();  
            JButton button1=new JButton("Delete");  
     
            button1.addActionListener(new ActionListener() {  
     
                    public void actionPerformed(ActionEvent e) {                 
                        table.remove(table.getSelectedRow());  
                    }  
                });  
                panel.add(button1);  
                panel.add(new JScrollPane(table));  
                frame.add(panel);  
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
                frame.setBounds(10, 10, 600, 300);  
                frame.setVisible(true);  
        }  
     
        }

    please repair my code!

    Thanks!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,104
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: Remove JTable row that read txt file records

    ArrayOutOutBoundsException occur.
    Please copy and post the full text of the error message.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Remove JTable row that read txt file records

    Cross-posted on stackoverflow where he posted the same or similar question three times and where it has been answered 3 times. Talk about wasting their time and ours.

    doNotHelpList.add(sajjad4563);

  4. The Following User Says Thank You to curmudgeon For This Useful Post:

    Norm (December 30th, 2012)

Similar Threads

  1. How to skip and read lines in txt file
    By saran123 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: December 26th, 2012, 07:07 AM
  2. Replies: 21
    Last Post: November 27th, 2012, 09:58 PM
  3. Add or remove a row on a jtable with Netbeans 6.0
    By Pieter in forum Java Theory & Questions
    Replies: 1
    Last Post: July 8th, 2010, 02:40 PM

Tags for this Thread