gpt4 book ai didi

java - 影响 JTable 单元格值在文本文件上的更改

转载 作者:行者123 更新时间:2023-11-29 07:10:00 29 4
gpt4 key购买 nike

我使用这种方法来更改我的表格单元格值, 它在 jtable 上改变但在文本文件上没有改变!

public class user_AllBooks extends AbstractTableModel {

BookInformation book_info = new BookInformation();
String[] columns = new String[]{"Book Name", "Book Date", "Book ID", "Borrow Status"};
ArrayList<BookInformation> bData = new ArrayList<BookInformation>();

public user_AllBooks() {
try {
BufferedReader br = new BufferedReader(new FileReader("AllBookRecords.txt"));
String line;
while ((line = br.readLine()) != null) {
bData.add(initializeBookData(line));
}
br.close();
} catch (IOException ioe) {
}
}

public BookInformation initializeBookData(String myline) {
BookInformation book_infos = new BookInformation();
String[] celledLine = myline.split(" ");
book_infos.setBookName(celledLine[0]);
book_infos.setBookDate(celledLine[1]);
book_infos.setBookID(celledLine[2]);
book_infos.setBorrowStatus(celledLine[3]);
return book_infos;
}

@Override
public String getColumnName(int col) {
return columns[col];
}

@Override
public int getRowCount() {
if (bData != null) {
return bData.size();
} else {
return 0;
}
}

@Override
public int getColumnCount() {
return columns.length;
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
BookInformation bookInf = bData.get(rowIndex);
Object value;

switch (columnIndex) {
case 0:
value = bookInf.getBookName();
break;
case 1:
value = bookInf.getBookDate();
break;
case 2:
value = bookInf.getBookID();
break;
case 3:
value = bookInf.getBorrowStatus();
break;
default:
value = "...";
}
return value;
}


@Override
public void setValueAt(Object value, int row, int col)
{
BookInformation book_infos = bData.get(row);
if (col==0)
book_infos.setBookName((String)value);
else if (col==1)
book_infos.setBookDate((String)value);
else if (col==2)
book_infos.setBookID((String)value);
else if (col==3)
book_infos.setBorrowStatus((String)value);
}
}

第二类:

public class user_AllBooksM extends JFrame implements ActionListener {

user_AllBooks uAllBooks = new user_AllBooks();
final JTable bTable = new JTable(uAllBooks);
JButton borrowButton;

public user_AllBooksM() {
setTitle("All Books");
exitButton = new JButton("Exit");
borrowButton = new JButton("Borrow");
borrowButton.addActionListener(this);
JPanel Bpanel = new JPanel();
Bpanel.setLayout(new FlowLayout());
JScrollPane sp = new JScrollPane(bTable);
Bpanel.add(sp);
Bpanel.add(borrowButton);
this.add(Bpanel);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(300, 60, 550, 550);
this.setResizable(false);
this.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent event) {
borrowInitialize(bTable.getSelectedRow());
}

public void borrowInitialize(int row) {
if (uAllBooks.getValueAt(row, 3).equals("yes")) {
JOptionPane.showMessageDialog(null, "This Book Was Borrowed");
} else {
uAllBooks.setValueAt("Yes", row, 3);
uAllBooks.fireTableRowsUpdated(row, row);
}
}

public static void main(String[] args) {
new user_AllBooksM();
}
}

我的文本文件:

sds wew     88     77     no
moon 889 988 yes
ccc 30 33 no
testing 76 77 no
yes 999 444 no
hoop 100 200 no
name 60 20 no
pp 14 15 no
vbnet 49 94 yes
sdsd 232 dsds no
gh 12 21 no
khoyBook 322 233 no

最佳答案

将 TableModelListener 添加到您的 TableModel。然后,无论何时首先发生更新事件,您都需要遍历 TableModel 的行/列并重新创建文本文件。

您的 TableModel 实现不正确。您还没有实现 setValueAt(...) 方法。

应从 TableModel 的 setValueAt() 方法调用“fireTableRowsUpdated()”方法。它不应从您的 ActionListener 代码中调用。

关于java - 影响 JTable 单元格值在文本文件上的更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14837588/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com