Java jtable自定义模型
我正在努力实现一个jTable。我创建了自己的TableModel类。这就是问题所在。不知何故,我的tableData数组(Obejct[]的ArrayList)没有正确写入。最后我得到一个表,其中所有行都有值 有人知道为什么ArrayList写得不正确吗Java jtable自定义模型,java,swing,jtable,tablemodel,Java,Swing,Jtable,Tablemodel,我正在努力实现一个jTable。我创建了自己的TableModel类。这就是问题所在。不知何故,我的tableData数组(Obejct[]的ArrayList)没有正确写入。最后我得到一个表,其中所有行都有值 有人知道为什么ArrayList写得不正确吗 class MyTableModel extends AbstractTableModel { private String[] columnNames = {"Auftragsnummer",
class MyTableModel extends AbstractTableModel {
private String[] columnNames = {"Auftragsnummer",
"Kunde",
"Kunden Nr.",
"Erfasst",
"Kommt",
"Geht",
"Kommentar"};
String[] temp_delete = new String[10];
int index_delete = 0;
private ArrayList<Object[]> tableData = new ArrayList<Object[]>();
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return tableData.size();
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
Object [] temp = tableData.get(row);
return temp[col];
}
public void removeAllEntry(){
for (int i = 0; i < tableData.size(); i++) {
tableData.remove(i);
}
model.fireTableDataChanged();
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public boolean isCellEditable(int row, int col) {
switch (col){
case 4:
return true;
default: return false;
}
}
public void addText(Object[] object) {
tableData.add(object);
fireTableDataChanged();
}
类MyTableModel扩展了AbstractTableModel{
私有字符串[]columnNames={“auftragsummer”,
“昆德”,
“昆登号”,
“电子助理”,
“Kommt”,
“Geht”,
“Kommentar”};
字符串[]临时删除=新字符串[10];
int index_delete=0;
私有ArrayList tableData=新ArrayList();
public int getColumnCount(){
返回columnNames.length;
}
public int getRowCount(){
返回tableData.size();
}
公共字符串getColumnName(int-col){
返回列名[col];
}
公共对象getValueAt(整数行,整数列){
Object[]temp=tableData.get(行);
返回温度[col];
}
公共空间移除入口(){
对于(int i=0;i}setValueAt()的父实现不起任何作用。您需要实现
setValueAt()
来更新内部数据结构,tableData
,并触发相应的tablemodeleevent
,它将更新您的视图
@Override
public void setValueAt(Object aValue, int row, int col) {
... // update tableData
this.fireTableCellUpdated(row, col); // notify the view
}
作为一个旁白,考虑<代码>列表>代码>而不是<代码> ARARYLIST/<代码> .< /P> < P>谢谢TRAHOTHOOD,我想出来了(见他的帖子)。此处的工作代码:
class MyTableModel extends AbstractTableModel {
private String[] columnNames = {"Auftragsnummer",
"Kunde",
"Kunden Nr.",
"Erfasst",
"Kommt",
"Geht",
"Kommentar",
"Abteilung"};
String[] temp_delete = new String[10];
int index_delete = 0;
private ArrayList<ArrayList<Object>> tableData = new ArrayList<ArrayList<Object>>();
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return tableData.size();
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
if(tableData.size()> 0){
return tableData.get(row).get(col);
}
return null;
}
public void removeAllEntry(){
tableData.clear();
model.fireTableDataChanged();
}
/*
* JTable uses this method to determine the default renderer/
* editor for each cell. If we didn't implement this method,
* then the last column would contain text ("true"/"false"),
* rather than a check box.
*/
public Class getColumnClass(int c) {
if(tableData.size()> 0){
return getValueAt(0, c).getClass();
}
return String.class;
}
/*
* Don't need to implement this method unless your table's
* editable.
*/
public boolean isCellEditable(int row, int col) {
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
switch (col){
case 4:
return true;
default: return false;
}
}
/*
* Don't need to implement this method unless your table's
* data can change.
*/
public void setValueAt(Object value, int row, int col) {
if(tableData.size() <= row){
ArrayList<Object> arrayList = new ArrayList<Object>();
for(int i = 0; i < columnNames.length;i++){
arrayList.add("");
}
tableData.add(arrayList);
}
ArrayList<Object> object = tableData.get(row);
object.add(col, value);
tableData.set(row, object);
fireTableDataChanged();
}
类MyTableModel扩展了AbstractTableModel{
私有字符串[]columnNames={“auftragsummer”,
“昆德”,
“昆登号”,
“电子助理”,
“Kommt”,
“Geht”,
“Kommentar”,
“Abteilung”};
字符串[]临时删除=新字符串[10];
int index_delete=0;
私有ArrayList tableData=新ArrayList();
public int getColumnCount(){
返回columnNames.length;
}
public int getRowCount(){
返回tableData.size();
}
公共字符串getColumnName(int-col){
返回列名[col];
}
公共对象getValueAt(整数行,整数列){
如果(tableData.size()>0){
返回tableData.get(行)、get(列);
}
返回null;
}
公共空间移除入口(){
tableData.clear();
model.fireTableDataChanged();
}
/*
*JTable使用此方法确定默认渲染器/
*每个单元格的编辑器。如果我们没有实现此方法,
*最后一列将包含文本(“真”/“假”),
*而不是复选框。
*/
公共类getColumnClass(int c){
如果(tableData.size()>0){
返回getValueAt(0,c).getClass();
}
返回字符串.class;
}
/*
*不需要实现此方法,除非您的表
*可编辑。
*/
公共布尔值可编辑(int行,int列){
//请注意,数据/单元地址是恒定的,
//无论单元格出现在屏幕上的何处。
开关(col){
案例4:
返回true;
默认:返回false;
}
}
/*
*不需要实现此方法,除非您的表
*数据可以改变。
*/
public void setValueAt(对象值、整行、整列){
如果(tableData.size(),但我真的需要实现这个吗?我使用addText()将数据插入表中。这不起作用吗?是的。有什么理由不这样做吗?
JTable
使用setValueAt()
;它不知道addText()
。你的addText()吗
真的要更改所有单元格值吗?fireTableRowsUpdated()
不够吗?没有理由,你是对的。我已经实现了methode并删除了setText()(我从其他论坛获得的)。很有效!非常感谢!我将在下一个答案中再次发布代码,供所有感兴趣的人使用。对接口进行编码可以根据需要更改实现,尽管我担心需要插入空行。