I’m making a database management system using java and mySQL. I’m using a jTable as a interface to the database.
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.
Can’t figure out the reason. I don’t have much experience on jTables. But i think the problem is with the 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.
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..!
(If any clarification is needed, please let me know.. difficult to post whole project. It’s a bit huge.. :D)
There’s a small bug in your code. Next time, be carefull..
In the tablemodel’s method “setValueAt(—)” you’ve put wrong arguments. you were trying to override a method of class “AbstractTableModel”. Original method is,
But you’ve written your method as,
Hence, it wont override the intended function. Everytime a cell is changed, tablemodel will call the original “setValueAt(…)” function. But since it hasn’t overriden, it will do nothing (you must override it, original method has no body).
Hope this helps..