简体   繁体   English

jTable细胞不会改变

[英]jTable cell dont change

I'm making a database management system using java and mySQL. 我正在使用java和mySQL创建一个数据库管理系统。 I'm using a jTable as a interface to the database. 我正在使用jTable作为数据库的接口。

Though the post look little long, the problem wont be that much of complex, for an experienced person (i guess). 虽然帖子看起来不长,但对于有经验的人来说,问题不会那么复杂(我猜)。

This is my problem. 这是我的问题。 After selecting a cell, i can enter values, (no prob with that). 选择一个单元格后,我可以输入值,(没有问题)。 But after entering values, when i click on some other cell, the entered values get disappeared and it gets back to null. 但是在输入值之后,当我点击其他一些单元格时,输入的值会消失并返回null。

Can't figure out the reason. 无法弄清楚原因。 I don't have much experience on jTables. 我对jTables没有太多经验。 But i think the problem is with the tablemodel. 但我认为问题在于tablemodel。

This is my tablemodel 这是我的桌面模型

import Templates.TableEntry;
import java.util.LinkedList;
import javax.swing.table.AbstractTableModel;

public class myTableModel extends AbstractTableModel {

public static final int DATE_INDEX = 0;
public static final int ORDERNO_INDEX = 1;
public static final int ROOT_INDEX = 2;
public static final int HIDDEN_INDEX = 3;
public String[] columnnames;
public LinkedList<TableEntry> entryList;

public myTableModel(String[] columnNames) {
    this.columnnames = columnNames;
    entryList = new LinkedList<TableEntry>();
}

@Override
public String getColumnName(int column) {
    return columnnames[column];
}

@Override
public boolean isCellEditable(int row, int column) {
    if (column == HIDDEN_INDEX) {
        return false;
    } else {
        return true;
    }
}

@Override
public Class getColumnClass(int column) {
    return String.class;
}

@Override
public String getValueAt(int row, int column) {
    TableEntry record = entryList.get(row);
    switch (column) {
        case DATE_INDEX:
            return record.date;
        case ORDERNO_INDEX:
            return record.jobOdrerNo;
        case ROOT_INDEX:
            return record.rootCardNos;
        default:
            return null;
    }
}

public void setValueAt(String value, int row, int column) {
    TableEntry record = entryList.get(row);
    switch (column) {
        case DATE_INDEX:
            record.date = value;
            break;
        case ORDERNO_INDEX:
            record.jobOdrerNo = value;
            break;
        case ROOT_INDEX:
            record.rootCardNos = value;
            break;
        default:
            System.out.println("invalid index");
    }
    updateTable(row, column);
}

public void updateTable(int row, int column) {
    fireTableCellUpdated(row, column);
}

@Override
public int getRowCount() {
    return entryList.size();
}

@Override
public int getColumnCount() {
    return columnnames.length;
}

public boolean hasEmptyRow() {
    if (entryList.size() == 0) {
        return false;
    }
    TableEntry entry = entryList.get(entryList.size() - 1);
    if ("".equals(entry.date)) {
        return true;
    } else {
        return false;

    }
}

public void addEmptyRow() {
    entryList.add(new TableEntry());
    fireTableRowsInserted(entryList.size() - 1, entryList.size() - 1);
}

public void deleteRow(int i) {
    if (i != 0) {
        entryList.remove(i);
        fireTableRowsDeleted(i - 1, i + 1);
    }
}

} }

Sorry about the length. 抱歉长度。 But i posted the whole code for the sake of completeness. 但为了完整起见,我发布了整个代码。 Most of the parts can be neglected. 大部分零件都可以忽略不计。

TableEntry is a simple class. TableEntry是一个简单的类。

package Templates;

public class TableEntry {

public String date;
public String jobOdrerNo;
public String rootCardNos;
public String yardRootCard;
public String MCISO_NO;
public String service_maintenance_breakdown;
public String jobNo;
public String machineName;
public String fault;
public String problematicPart;
public String person;
public String action;
public String startTime;
public String finishedTime;
public String durationOfRepair;
public String spareParts;
public String itemCode;
public String no;
public String value;
public String totalCost;
public String remark;
public String breakdownAndSolution;

} }

Hope I've provided all the details. 希望我已经提供了所有细节。 This has been a real bug for me. 这对我来说是一个真正的错误。 Any help is appreciated. 任何帮助表示赞赏。 Thanx in advance..! Thanx提前..!

(If any clarification is needed, please let me know.. difficult to post whole project. It's a bit huge.. :D) (如果需要澄清,请告诉我..很难发布整个项目。这有点大..:D)

There's a small bug in your code. 你的代码中有一个小错误。 Next time, be carefull.. 下次,小心..

In the tablemodel's method "setValueAt(---)" you've put wrong arguments. 在tablemodel的方法“setValueAt(---)”中你提出了错误的参数。 you were trying to override a method of class "AbstractTableModel". 你试图覆盖类“AbstractTableModel”的方法。 Original method is, 原始方法是,

public void setValueAt(Object aValue, int rowIndex, int columnIndex){
}

But you've written your method as, 但你已经写了你的方法,

public void setValueAt(String value, int row, int column) {
...//method body
}

Hence, it wont override the intended function. 因此,它不会覆盖预期的功能。 Everytime a cell is changed, tablemodel will call the original "setValueAt(...)" function. 每次更改单元格时,tablemodel将调用原始的“setValueAt(...)”函数。 But since it hasn't overriden, it will do nothing (you must override it, original method has no body). 但由于它没有覆盖,它什么都不做(你必须覆盖它,原始方法没有主体)。

Hope this helps.. 希望这可以帮助..

You can use defaultTableModal for normal use. 您可以使用defaultTableModal进行正常使用。 It has extended abstractTableModal and provide methods for frequently used requirements. 它扩展了abstractTableModal并提供了常用需求的方法。 Try it first, if not enough, then go develop your own class... 先尝试一下,如果还不够,那就去开发自己的班级......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM