📄 booktablemodel.java
字号:
package com.book.ui;
import java.util.List;
import javax.swing.table.AbstractTableModel;
import com.book.domain.Book;
public class BookTableModel extends AbstractTableModel {
// 表格中第一行所要显示的内容存放在字符串数组columnNames中
private String[] columnNames = { "书籍编号", "书籍名称", "作者", "出版社", "出版日期",
"库存数量" };
// 表格中各行的内容保存在二维数组data中
private List data = null;
public BookTableModel(List data) {
this.data = data;
}
// 下述方法是重写AbstractTableModel中的方法,其主要用途是被JTable对象调用,
// 以便在表格中正确的显示出来。程序员必须根据采用的数据类型加以恰当实现。
// 获得列的数目
public int getColumnCount() {
return columnNames.length;
}
// 获得行的数目
public int getRowCount() {
return data.size();
}
// 获得某行某列的数据,而数据保存在对象数组data中
public Object getValueAt(int row, int col) {
// return data[row][col];
Book book = (Book) data.get(row);
switch (col) {
case 0:
return book.getBookid();
case 1:
return book.getBookname();
case 2:
return book.getAuthor();
case 3:
return book.getPublishing();
case 4:
return book.getPublishingdate();
case 5:
return new Integer(book.getStorage());
}
return null;
}
// 获得某列的名字,而目前各列的名字保存在字符串数组columnNames中
public String getColumnName(int col) {
return columnNames[col];
}
public Book getRowsData() {
int numRows = getRowCount();
// int numCols = getColumnCount();
return (Book) data.get(numRows);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -