java 更改 Jtable 单元格值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14818681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
change Jtable Cell Value
提问by Sajad
I have a jtable that keep book file records and sho them.
我有一个 jtable 来保存书籍文件记录并保存它们。
I create a "Borrow" jbutton that when clicked to a row that not borrowed, The "Borrow Status" of that row should change to "Yes".
我创建了一个“借用”jbutton,当单击未借用的行时,该行的“借用状态”应更改为“是”。
I use this codes, but did not changed!
我用这个代码,但没有改变!
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;
}
}
Second Class:
第二类:
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();
}
}
Thanks.
谢谢。
回答by Vishal K
To override setValueAt
You should use:
覆盖setValueAt
你应该使用:
@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);
fireTableCellUpdated(row,col);
}
Define this method within class user_AllBooks
在类中定义此方法 user_AllBooks
回答by MadProgrammer
You could use TableModel#setValueAt
, but you would also become responsible for updating the table, which breaks the MCV model.
您可以使用TableModel#setValueAt
,但您还将负责更新表,这会破坏 MCV 模型。
Instead, you could add a setBorrowed
method to your user_AllBooks
class.
相反,您可以setBorrowed
向user_AllBooks
类中添加一个方法。
public void setBorrowed(int row, boolean borrowed) {
BookInformation book = bData.get(row);
book.setBorrowedState(borrowed);
fireTableCellUpdated(row, 3);
}
回答by Ashwinee K Jha
You have overridden the getValueAt method. So it always returns value from your bData
arraylist.
您已经覆盖了 getValueAt 方法。所以它总是从你的bData
数组列表中返回值。
public Object getValueAt(int rowIndex, int columnIndex) {
BookInformation bookInf=bData.get(rowIndex);
So you need to implement the setValueAt method also in your table model so change actual data in the bData
arraylist.
因此,您还需要在表模型中实现 setValueAt 方法,以便更改bData
数组列表中的实际数据。
Also note that bTable.getSelectedRow()
will give you selected row in terms of the view. In model the index might be different if the table is sorted.
另请注意,这bTable.getSelectedRow()
将根据视图为您提供选定的行。在模型中,如果表已排序,则索引可能会有所不同。