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!