我已经编写了以下方法来创建表但是每次调用任务列表时都会调用但是表不能更改表的值
public void displayAllTaskByProjectId(Task[] task) {
System.out.println("Success fully call");
/**
* The table to display tasks related to the selected project and timer
* for that
*/
tblTaskList = new JTable();
LOG.info(" get here");
int length;
DefaultTableModel dm = new DefaultTableModel(0, 0) {
private static final long serialVersionUID = 1L;
public Class getColumnClass(int column) {
return getValueAt(0, column).getClass();
};
public boolean isCellEditable(int row, int column) {
switch (column) {
case 5:
return true;
default:
return false;
}
};
};
dm.fireTableDataChanged();
String header[] = new String[] { "Prority", "Task Title", "Start",
"Pause", "Stop", "Statulses" };
dm.setColumnIdentifiers(header);
// Add image icon in table
ImageIcon imgStart = new ImageIcon("../../../../start.png");
ImageIcon imgStop = new ImageIcon("../../../../stop.png");
ImageIcon imgPause = new ImageIcon("../../../../pause.png");
length = task.length;
for (int count = 0; count < length; count++) {
dm.addRow(new Object[] { count, task[count].getTaskTitle() + count,
imgStart, imgPause, imgStop, "Active" });
}
tblTaskList.setModel(dm);
TableColumn tbcStatus = tblTaskList.getColumnModel().getColumn(5);
JComboBox cmbStatus = new JComboBox();
cmbStatus.setModel(new DefaultComboBoxModel(new String[] { "Active",
"Close", "Deactive", "Offline" }));
tbcStatus.setCellEditor(new DefaultCellEditor(cmbStatus));
tblTaskList.getColumnModel().getColumn(0).setMinWidth(0);
tblTaskList.getColumnModel().getColumn(0).setMaxWidth(0);
tblTaskList.getColumnModel().getColumn(1).setPreferredWidth(350);
tblTaskList.getColumnModel().getColumn(2).setPreferredWidth(35);
tblTaskList.getColumnModel().getColumn(3).setPreferredWidth(35);
tblTaskList.getColumnModel().getColumn(4).setPreferredWidth(25);
tblTaskList.getTableHeader().setReorderingAllowed(false);
tblTaskList.getTableHeader().setResizingAllowed(false);
tblTaskList.setBounds(93, 34, 614, 324);
jspTasklist = new JScrollPane(tblTaskList);
jspTasklist.setOpaque(false);
jspTasklist.setFocusTraversalPolicyProvider(true);
jspTasklist.setBounds(108, 34, 605, 335);
frmTaskList.getContentPane().add(jspTasklist);
}
请尽快快速解决我的问题
答案 0 :(得分:1)
请尽快快速解决我的问题
(尽快和第一次)
创建JTable
和DefaultTableModel
作为本地变量
add, remove, modify data in model,不要重新创建整个结构,在紧急情况下切换一个JTable的模型(在Swing GUI中已经可见)
无论Task[] task
是什么must to notify EDT,代码model.add/remove/insertRow
都必须通过封送到invokeLater
答案 1 :(得分:1)
你应该只删除tblTaskList = new JTable();从displayAllTaskByProjectId函数和代码将正常工作。仅在init组件
时才使用此行tblTaskList = new JTable()答案 2 :(得分:0)
数据未刷新的问题是您在为表格设置模型之前打电话给fireTableDataChanged
,
对代码进行了更改,如下所示:
public void displayAllTaskByProjectId(Task [] task){
System.out.println("Success fully call");
/**
* The table to display tasks related to the selected project and timer
* for that
*/
tblTaskList = new JTable();
LOG.info(" get here");
int length;
DefaultTableModel dm = new DefaultTableModel(0, 0) {
private static final long serialVersionUID = 1L;
public Class getColumnClass(int column) {
return getValueAt(0, column).getClass();
};
public boolean isCellEditable(int row, int column) {
switch (column) {
case 5:
return true;
default:
return false;
}
};
};
tblTaskList.setModel(dm);
String header[] = new String[] { "Prority", "Task Title", "Start",
"Pause", "Stop", "Statulses" };
dm.setColumnIdentifiers(header);
// Add image icon in table
ImageIcon imgStart = new ImageIcon("../../../../start.png");
ImageIcon imgStop = new ImageIcon("../../../../stop.png");
ImageIcon imgPause = new ImageIcon("../../../../pause.png");
length = task.length;
for (int count = 0; count < length; count++) {
dm.addRow(new Object[] { count, task[count].getTaskTitle() + count,
imgStart, imgPause, imgStop, "Active" });
}
dm.fireTableDataChanged();
TableColumn tbcStatus = tblTaskList.getColumnModel().getColumn(5);
JComboBox cmbStatus = new JComboBox();
cmbStatus.setModel(new DefaultComboBoxModel(new String[] { "Active",
"Close", "Deactive", "Offline" }));
tbcStatus.setCellEditor(new DefaultCellEditor(cmbStatus));
tblTaskList.getColumnModel().getColumn(0).setMinWidth(0);
tblTaskList.getColumnModel().getColumn(0).setMaxWidth(0);
tblTaskList.getColumnModel().getColumn(1).setPreferredWidth(350);
tblTaskList.getColumnModel().getColumn(2).setPreferredWidth(35);
tblTaskList.getColumnModel().getColumn(3).setPreferredWidth(35);
tblTaskList.getColumnModel().getColumn(4).setPreferredWidth(25);
tblTaskList.getTableHeader().setReorderingAllowed(false);
tblTaskList.getTableHeader().setResizingAllowed(false);
tblTaskList.setBounds(93, 34, 614, 324);
jspTasklist = new JScrollPane(tblTaskList);
jspTasklist.setOpaque(false);
jspTasklist.setFocusTraversalPolicyProvider(true);
jspTasklist.setBounds(108, 34, 605, 335);
frmTaskList.getContentPane().add(jspTasklist);
}
请查看此Swing Tutorial for JTables以获取更多信息。