JTable是Swing編程中很常用的控件
一
JTable table = new JTable();
Object[][] cellData = {{
String[] columnNames = {
JTable table = new JTable(cellData
String[] headers = {
Object[][] cellData = null;
DefaultTableModel model = new DefaultTableModel(cellData
public boolean isCellEditable(int row
return false;
}
};
table = new JTable(model);
二
table
TableColumn firsetColumn = table
firsetColumn
firsetColumn
firsetColumn
DefaultTableModel tableModel = (DefaultTableModel) table
int count=
tableModel
int cols = table
DefaultTableModel tableModel = (DefaultTableModel) table
tableModel
table
三
table
DefaultTableModel tableModel = (DefaultTableModel) table
int n=
tableModel
int rows = table
DefaultTableModel tableModel = (DefaultTableModel) table
tableModel
DefaultTableModel tableModel = (DefaultTableModel) table
model
四
DefaultTableModel tableModel = (DefaultTableModel) table
String cellValue=(String) tableModel
注:數據是Member類型的鏈表
public class Member{
// 名稱
private String name;
// 年齡
private String age;
// 職務
private String title;
}
填充數據的代碼:
public void fillTable(List<Member> members){
DefaultTableModel tableModel = (DefaultTableModel) table
tableModel
// 填充數據
for(Member member:members){
String[] arr=new String[
arr[
arr[
arr[
// 添加數據到表格
tableModel
}
// 更新表格
table
}
public List<Member> getShowMembers(){
List<Member> members=new ArrayList<Member>();
DefaultTableModel tableModel = (DefaultTableModel) table
int rowCount=tableModel
for(int i=
Member member=new Member();
member
member
member
members
}
return members;
}
五
int selectRows=table
DefaultTableModel tableModel = (DefaultTableModel) table
if(selectRows==
int selectedRowIndex = table
}
int selectRows=table
DefaultTableModel tableModel = (DefaultTableModel) table
if(selectRows>
int[] selRowIndexs=table
for(int i=
// 用tableModel
String cellValue=(String) tableModel
}
}
六
view
public void mousePressed(MouseEvent e) {
// 鼠標按下時的處理
}
public void mouseReleased(MouseEvent e) {
// 鼠標松開時的處理
}
public void mouseEntered(MouseEvent e) {
// 鼠標進入表格時的處理
}
public void mouseExited(MouseEvent e) {
// 鼠標退出表格時的處理
}
public void mouseClicked(MouseEvent e) {
// 鼠標點擊時的處理
}
});
From:http://tw.wingwit.com/Article/program/Java/hx/201311/27152.html