java从数据库中得到的数据怎么添加到JTable,有哪位大大给我传一个demo呗
2个回答
展开全部
import java.util.List;
import gui.dao.channelDao;
import gui.orm.channel;
import javax.swing.table.AbstractTableModel;
@SuppressWarnings("serial")
public class channelMode extends AbstractTableModel {
final static String[] columnHeaders = { "栏目编号","栏目名称","栏目简介","LOGO","子栏目","排序"};
private Object[][] datavalues = {{"","","","","",""}};
public Object[][] list(int page){
channelDao cdao = new channelDao();
List<channel> list = cdao.getList(page,20);
Object[][] obj = new Object[list.size()][6];
for(int i=0;i<list.size();i++){
obj[i][0] = list.get(i).getCid();
obj[i][1] = list.get(i).getTitle();
obj[i][2] = list.get(i).getDes();
obj[i][3] = list.get(i).getLogo();
obj[i][4] = (list.get(i).getSon()==0)?"NO":"YES";
obj[i][5] = list.get(i).getSort();
}
return obj;
}
public int getColumnCount() {
return datavalues[0].length;
}
public int getRowCount() {
return datavalues.length;
}
public Object getValueAt(int rowIndex, int columnIndex) {
return datavalues[rowIndex][columnIndex];
}
public String getColumnName(int column) {
return columnHeaders[column];
}
public void setDatavalues(Object[][] datavalues) {
this.datavalues = datavalues;
}
}
channelMode cm = new channelMode();
JTable jtb = new JTable(cm);
cm.setDatavalues(cm.list(page));
import gui.dao.channelDao;
import gui.orm.channel;
import javax.swing.table.AbstractTableModel;
@SuppressWarnings("serial")
public class channelMode extends AbstractTableModel {
final static String[] columnHeaders = { "栏目编号","栏目名称","栏目简介","LOGO","子栏目","排序"};
private Object[][] datavalues = {{"","","","","",""}};
public Object[][] list(int page){
channelDao cdao = new channelDao();
List<channel> list = cdao.getList(page,20);
Object[][] obj = new Object[list.size()][6];
for(int i=0;i<list.size();i++){
obj[i][0] = list.get(i).getCid();
obj[i][1] = list.get(i).getTitle();
obj[i][2] = list.get(i).getDes();
obj[i][3] = list.get(i).getLogo();
obj[i][4] = (list.get(i).getSon()==0)?"NO":"YES";
obj[i][5] = list.get(i).getSort();
}
return obj;
}
public int getColumnCount() {
return datavalues[0].length;
}
public int getRowCount() {
return datavalues.length;
}
public Object getValueAt(int rowIndex, int columnIndex) {
return datavalues[rowIndex][columnIndex];
}
public String getColumnName(int column) {
return columnHeaders[column];
}
public void setDatavalues(Object[][] datavalues) {
this.datavalues = datavalues;
}
}
channelMode cm = new channelMode();
JTable jtb = new JTable(cm);
cm.setDatavalues(cm.list(page));
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询