ورود

View Full Version : سوال: حذف سطر انتخاب شده در Jtable و حذف از فایل متنی



sajjad4563
سه شنبه 10 بهمن 1391, 16:35 عصر
سلام بچه ها
من یه جدول دارم که فایل متنی رو میخونه و تو خودش نشون میده و یه دکمه حذف هم دارم که وقتی کاربر روی سطری از جدول انتخاب کرد و روی دکمه دلت کلیک کرد، اون سطر هم ازجدول و هم از فایل حذف بشه.
من برای اینکار از دوکلاس استفاده کردم که تو قسمت حذف سطر مشکل داره، همچنین در قسمت حذف از فایل:


public class Readuser_A extends AbstractTableModel
{ String[] columns = { "Fname", "Lname", "Gender", "Date", "ID" };
ArrayList<String> Listdata = new ArrayList<String>();
String[][] Arraydata;
public Readuser_A() {
try {
FileReader fr = new FileReader("AllUserRecords.txt");
BufferedReader br = new BufferedReader(fr);
String line;
while ((line = br.readLine()) != null) {
Listdata.add(line); }
br.close();
Arraydata = new String
[Listdata.size()][];
for (int i = 0; i < Listdata.size(); i++) {
Arraydata[i] = Listdata.get(i).split(" "); }
}
catch (IOException e) {
} }
@Override
public String getColumnName(int colu) { return columns[colu]; }
public int getRowCount() { if (null != Arraydata) { return Arraydata.length; } else { return 0
; } }
public int getColumnCount() { return columns.length; }
public Object getValueAt(int rowIndex, int columnIndex) { return Arraydata[rowIndex][columnIndex]; } }

کلاس دوم
public class ReaduserM_A extends DefaultTableModel {
final JLabel myLable = new JLabel();
public ReaduserM_A() {
final Readuser_A RU = new Readuser_A();
final JTable mytable = new JTable(RU);
final JFrame Uframe = new JFrame("All Users");
JButton DellButton = new JButton("Delete User");
DellButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (mytable.getSelectedRow() != -1) {
removeRow(mytable.getSelectedRow());
RU.fireTableRowsDeleted(mytable.getSelectedRow(), mytable.getSelectedRow());
} else {
JOptionPane.showMessageDialog(null, "No Row Selected");
return; }
//Now, Delete from text file too deleteFromFile(); }
});
JPanel panel = new JPanel(); JScrollPane sp = new JScrollPane(mytable);
panel.add(sp);
panel.add(DellButton);
panel.add(myLable);
Uframe.add(panel);
Uframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE);
Uframe.setSize(570, 500);
Uframe.setLocation(300, 60);
Uframe.setVisible(true); } public void deleteFromFile() { File Mf = new File("AllUserRecords.txt");
File Tf = new File("Uoutput.txt");
try {
FileReader Ufr = new FileReader(Mf);
BufferedReader Ubr = new BufferedReader(Ufr); PrintWriter Upw = new PrintWriter(new FileWriter(Tf));
String Us;
while ((Us = Ubr.readLine()) != null) { String[] Ust = Us.split(" ");
String Unumber = Ust[4];
//How find the selected row line by it's ID and delete that row? }
Upw.close();
Ubr.close(); Mf.delete(); Tf.renameTo(Mf); }
catch (FileNotFoundException e1) { myLable.setText("File Not Found"); }
catch (IOException ioe) { myLable.setText("IO Error");
ioe.printStackTrace(); } }
public static void main(String[] args) { new ReaduserM_A(); }
}

خطای من:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 6 >= 0 at java.util.Vector.removeElementAt(Vector.java:554) at javax.swing.table.DefaultTableModel.removeRow(Defa ultTableModel.java:463) at Array.ReaduserM_A$1.actionPerformed(ReaduserM_A.ja va:33) at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2018) at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2341) at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:402) at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:259) at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:252) at java.awt.Component.processMouseEvent(Component.jav a:6505) at javax.swing.JComponent.processMouseEvent(JComponen t.java:3321) at java.awt.Component.processEvent(Component.java:627 0) at java.awt.Container.processEvent(Container.java:222 9) at java.awt.Component.dispatchEventImpl(Component.jav a:4861) at java.awt.Container.dispatchEventImpl(Container.jav a:2287)
...

البته این سری که این خطا رو گرفته بودم، از تو جدول شیشمین سطر(با احتساب صفر برای اولین سطر) رو انتخاب کردم که اون شیش واسه اونه.


ممنونم