我的整個代碼是這樣的,但我的代碼有一個錯誤,沒有運行: 從文件讀取是正確的,但沒有刪除行。行刪除表
public class AllUserTable extends AbstractTableModel{
Vector data;
Vector columns;
public AllUserTable() {
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 AllUserTable aut1=new AllUserTable();
final JFrame frame1=new JFrame();
final JTable table=new JTable();
final JPanel panel=new JPanel();
JButton button1=new JButton("Delete");
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DefaultTableModel model=new DefaultTableModel(data,columns); //Error!
model.removeRow(table.getSelectedRow());
table.setModel(model);
table.setModel(aut1);
panel.add(table);
}
});
JScrollPane scroolpane=new JScrollPane();
scroolpane.setViewportView(table);
panel.add(scroolpane);
panel.add(button1);
frame1.add(panel);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setBounds(200, 80, 600, 500);
frame1.setVisible(true);
}
}
請修復我的代碼!
謝謝你,你喜歡特別的書嗎? – Sajad
不,我在很多年前就學到了這一切,現在我使用教程和javadoc作爲主要的信息來源。 –
非常感謝你! – Sajad