Reputation: 81
I am creating a program which nests a JTable
. For the creation of the JTable
i have created an AbstractTableModel
which overrides the methods setValueAt
, isCellEditable
, etc. (Code is posted on bottom). I also have added a tableModelListener
with the method tableChanged
in the class which keeps the table to notify that a cell value has changed. When i change a cell value to another value, the listener works perfectly and changes the cell value. The problem is that when i change the cell value to null(practically delete the value of the cell), the listener does not work and the value stays the same. Any idea how can i fix it?
Class Cell:
public class Cell {
private int x;
private int y;
private String content;
public Cell(int x, int y){
this.x = x;
this.y = y;
this.content = content;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public String getContent(){
return content;
}
public void setContent(String content){
this.content = content;
}
}
Class CreateTable:
import javax.swing.table.AbstractTableModel;
import javax.swing.undo.UndoManager;
public class CreateTable extends AbstractTableModel {
private int nrows=1000;
private int ncolumns=26;
private String[] columnNames;
private Cell[][] data;
public CreateTable(int nrows,int ncolumns){
this.nrows = nrows;
this.ncolumns = ncolumns;
this.columnNames= new String[ncolumns];
this.data = new Cell[nrows][ncolumns];
for (int i=0;i<ncolumns;i++){
columnNames[i] = super.getColumnName(i);
}
for (int i=0;i<data.length;i++){
for (int j=0;j<columnNames.length;j++){
data[i][j] = new Cell(i,j);
}
}
}
public CreateTable(){
this.columnNames= new String[ncolumns];
this.data = new Cell[nrows][ncolumns];
for (int i=0;i<ncolumns;i++){
columnNames[i] = super.getColumnName(i);
}
for (int i=0;i<data.length;i++){
for (int j=0;j<columnNames.length;j++){
data[i][j] = new Cell(i,j);
}
}
}
public int getColumnCount() {
// TODO Auto-generated method stub
return columnNames.length;
}
@Override
public int getRowCount() {
// TODO Auto-generated method stub
return data.length;
}
@Override
public String getValueAt(int row, int col) {
// TODO Auto-generated method stub
return data[row][col].getContent();
}
public void setValueAt(Object value,int row,int col){
if(value == null || value.toString().isEmpty()){
return;
}
data[row][col].setContent(value.toString());
fireTableCellUpdated(row,col);
}
public boolean isCellEditable(int row, int col){
return true;
}
public void setRowCount(int nrows){
}
public int findColumn(String columnName){
for (int i=0;i<getColumnCount();i++){
if (columnName.equals(columnNames[i])){
return i;
}
}
return -1;
}
}
Part of Class Sheet whith the table listener:
public class Sheet extends JTable {
private int nrows;
private int ncolumns;
private String name;
public Sheet(String name,int nrows,int ncolumns){
this.name = name;
this.setModel(new CreateTable(nrows,ncolumns));
this.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
this.getModel().addTableModelListener(e->{
int row = this.getEditingRow();
int column = this.getEditingColumn();
});
this.setCellSelectionEnabled(true);
}
}
Upvotes: 1
Views: 541
Reputation: 13407
when i change the cell value to null(practically delete the value of the cell), the listener does not work and the value stays the same.
As mentioned by MadProgrammer in the comments, your problem is in
public void setValueAt(Object value,int row,int col){
if(value == null || value.toString().isEmpty()){
return;
}
data[row][col].setContent(value.toString());
fireTableCellUpdated(row,col);
}
If the value is a non-empty or a null string, you set the cell's content to be that string. However, if the string is empty (not sure how it can be null
) you do nothing and so the current value stays the same.
What you want is probably
public void setValueAt(Object value,int row,int col){
if(value == null || value.toString().isEmpty()){
data[row][col].setContent("");
}
else {
data[row][col].setContent(value.toString());
}
fireTableCellUpdated(row,col);
}
But since the true
and false
conditions of the if
do the same, just do
public void setValueAt(Object value,int row,int col){
data[row][col].setContent(value.toString());
fireTableCellUpdated(row,col);
}
(Again, unless value
can get null
somehow.)
Upvotes: 1