1

ファイルからレコードを読み取って表示するテーブルがあり、ユーザーが行を選択してクリックすると、その行がテーブルとテキストファイルからも削除される削除ボタンがあります。

(更新しました)

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) {
    }
}

 public void RemoveMyRow(int row){
  Listdata.RemoveElement(row);
   }

@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];
}
}

私の2番目のクラス:

public class ReaduserM_A {
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_CLOSE);
    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();
}
}

ありがとうございました

4

1 に答える 1

3
removeRow(mytable.getSelectedRow());

上記のステートメントが問題になる可能性があります。選択された行がない場合getSelectedRowは-1を返すためです。

行が存在するかどうかを確認します。次に、存在する場合は削除します。

if(mytable.getSelectedRow() != -1) {
  removeRow(mytable.getSelectedRow());
}

アップデート:

クラスのメソッドで取得NullPointerExceptionしたコードを実行しました。getRowCountTableModel

public int getRowCount() {
   return Arraydata.length;
}

したがってnull、カウントを取得する前にチェックを行ってください。

public int getRowCount() {
if(null != Arraydata) {
    return Arraydata.length;
} else {
    return 0;
}
}

ここで実行するとArrayOutOfBoundException、インデックス-1が取得されます。これは、削除アクションが原因です。前に述べたように、行が存在するかどうかを確認してから、それぞれのアクションを実行します。次のコードはそれを行います。

public void actionPerformed(ActionEvent e) {
    if(mytable.getSelectedRow() != -1) { 
      removeRow(mytable.getSelectedRow());
      rftl2.fireTableRowsDeleted(mytable.getSelectedRow(), mytable.getSelectedRow());
    } else {
      JOptionPane.showMessageDialog(null, "No Row Selected");
       return;
    }

    //Now, Delete from text file too
    deleteFromFile();
 }

最後に、出力を取得します(このような選択された行がない場合)。

ここに画像の説明を入力してください

于 2013-01-25T16:37:14.467 に答える