Remove JTable row that read txt file records

Ads
 

Remove JTable row that read txt file records

Hi every one. i have a jtable that correctly read data frome 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 dont 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!

View Answers

Ads









Related Tutorials/Questions & Answers:
Remove JTable row that read txt file records
Remove JTable row that read txt file records  Hi every one. i have a jtable that correctly read data frome file and show them in own. I want to add a "Delete" button that when select a row and clicked button, row must deleted
How to read and retrieve jtable row values into jtextfield on clicking at particular row ...
How to read and retrieve jtable row values into jtextfield on clicking... application in which i have to display database records in jtable .now I want to read all the values of particular row at which mouse is clicked. and display
Advertisements
Backup selected records into txt file
Backup selected records into txt file       Backup selected records into txt file is used to copies... into txt file. To restore the selected backup records into txt file, we create
how to read text file in jtable in netbeans7.0
how to read text file in jtable in netbeans7.0  text file is: contig00001 length=586 numreads=4... want to displaythe above .txt file in jtable as following format having 3
Backup selected records into txt file
Backup selected records into txt file       Backup selected records into txt file is used... Selected records into txt file. To restore the selected backup records into txt
jtable insert row swing
jtable insert row swing  How to insert and refresh row in JTable?   Inserting Rows in a JTable example
JTable duplicate values in row
JTable duplicate values in row  JTable duplicate values in row
jtable-adding a row dynamically
jtable-adding a row dynamically  hi..i am doing a project for pharmacy .. *pblm:* when i want to enter the details in jtable while running... to add one more row dynamically as a new row 4th if the details are large.but every
Thread for reading txt file
Thread for reading txt file  how to use 3 thread to read 3 txt file? To create three threads for reading the file and three threads for getting the strings out of the queue and printing them. thanks
Java: Adding Row in JTable
Java: Adding Row in JTable   how about if we already have the JTAble created earlier. And i just found nothing to get its DefaultTableModel, thus, I can't call insertRow() method. Is there any work around for this? I found
Convesion of txt file to doc file.??????
Convesion of txt file to doc file.??????  how to convert text file to doc file using java
Backup Into txt File
Backup Into txt File       Backup Into txt File is used to put the backup file from a ... illustrate an example from 'Backup Into txt File'. To understand this example
ModuleNotFoundError: No module named 'file_open_gips_txt'
named 'file_open_gips_txt' How to remove the ModuleNotFoundError: No module named 'file_open_gips_txt' error? Thanks   Hi, In your...ModuleNotFoundError: No module named 'file_open_gips_txt'  Hi, My
read a file
read a file  read a file byte by byte   import java.io.File... static void main(String[] args) { File file = new File("D://Try.txt"); try { FileInputStream fin = new FileInputStream(file); byte
how update JTable after adding a row into database
how update JTable after adding a row into database  J have two... in JTable, and it's OK, but after adding a row into database table does't update. How update JTable after adding a row into database? package djile pak.java
Uploading and download pdf or .txt file.
Uploading and download pdf or .txt file.  I want admin user to upload pdf file into database and the users can download those pdf format from database
file read
file read  hi i am reaing from a file which has punjabi words. can some one help with me some code
CONVERT JTable DATA TO PDF FILE
the jtable data from the jframe and stored the data into the pdf file in the form...(JTable table, int row_index, int col_index){ return table.getModel().getValueAt...CONVERT JTable DATA TO PDF FILE  HOW TO CONVERT JTable DATA TO .PDF
Backup Into txt File
Backup Into txt File       Backup Into txt File is used to put the backup file from... The Tutorial illustrate an example from 'Backup Into txt File
Read file in java
Read file in java  Hi, How to Read file in java? Thanks   Hi, Read complete tutorial with example at Read file in java. Thanks
java read file
java read file  Hello i need some help... i want to read an MS Excel file in java so how to read that file
Need to Remove Duplicate Records from Excel Sheet
Need to Remove Duplicate Records from Excel Sheet  Need to Remove Duplicate Records from Excel Sheet. I have one excel sheet having two fields... empnum rating (without using sql query have to remove records from excel using java
Read and write file
Read and write file  HI, How to read and write file from Java program? Thanks   Hi, See the following tutorials: Java Write To File Read File Thanks
java jtable
in which i have to display database records in jtable .now I want to read only the values of particular row at which the jtextfield has been set. and display that in jtable. plz help me with the code
reading a csv file from a particular row
reading a csv file from a particular row  how to read a csv file from a particular row and storing data to sql server by using jsp servlet
reading the records from a .xlsx file and storing those records in database table
reading the records from a .xlsx file and storing those records in database table  Here is my requirement, I want to read the records from a .xlsx file and store that records in database table. I tried like this public class
Read external file in PHP
Read external file in PHP  How to read external files in PHP in particular time duration and save that file with the latest date. How is it possible? Please explain with an example. Thanks
Read from file java
Read from file java  How to Read from file java? What is the best method for a text file having a size of 10GB. Since i have to process the file one line at a time so tell me the very best method. Thank you
how to change file from .txt to .mat(matrix)
how to change file from .txt to .mat(matrix)  i have a big file.txt and i want to change this file to file.mat(matrix) ...this is in windows not on any os ..thx if u answering quickly please
Java Read CSV file
Java Read CSV file In this tutorial, you will learn how to read csv file. CSV (Comma Separated Value) is a common type of data file which can be exported... opened a file data.csv  and created a BufferedReader object to read
Java read binary file
Java read binary file  I want Java read binary file example code that is easy to read and learn. Thanks   Hi, Please see the code at Reading binary file into byte array in Java. Thanks   Hi, There is many

Ads