jtable change values problem

J

Jacques Chaurette

Hello All, and thanks for any help.

I have a jTable that is populated with an array of strings. During the
course of the program these values get changed depending on the selection of
a jcombobox. What I have noticed is that I am unable to set the initial
values back in the table. I can set the values to any other of my data
string arrays except the initial ones. I am using the setValue method of the
table model. Here is the jtable tablemodel constructor:

class MyTableModel extends AbstractTableModel {
String[] columnNames = {"Nom. dia.","Dia. (in)",
"Instal. Cost",
};
Object [][] data = pipeschedlight;


public int getColumnCount() {
return columnNames.length;
}

public int getRowCount() {
return data.length;
}

public String getColumnName(int col) {
return columnNames[col];
}

public Object getValueAt(int row, int col) {
return data[row][col];
}

/*
* 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) {
return getValueAt(0, c).getClass();
}

/*
* 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.
//if (col < 2) {
//return false;
//} else {
return true;
//}
}

/*
* Don't need to implement this method unless your table's
* data can change.
*/
public void setValueAt(Object value, int row, int col) {
if (DEBUG) {
System.out.println("Setting value at " + row + "," + col
+ " to " + value
+ " (an instance of "
+ value.getClass() + ")");
}

data[row][col] = value;
fireTableCellUpdated(row, col);

if (DEBUG) {
System.out.println("New value of data:");
printDebugData();
}
}

private void printDebugData() {
int numRows = getRowCount();
int numCols = getColumnCount();

for (int i=0; i < numRows; i++) {
System.out.print(" row " + i + ":");
for (int j=0; j < numCols; j++) {
System.out.print(" " + data[j]);
}
System.out.println();
}
System.out.println("--------------------------");
}
}

Cheers,

Jacques
 
R

Roedy Green

I have a jTable that is populated with an array of strings. During the
course of the program these values get changed depending on the selection of
a jcombobox. What I have noticed is that I am unable to set the initial
values back in the table. I can set the values to any other of my data
string arrays except the initial ones.

What happens when you try?

1. you get an exception? What is it?

2. the model itself does not change?

3. the model does not repaint?

Are you saying that

model.setValueAt( "begin", 0,0 ); displays "begin"
model.setValueAt( "different", 0,0,) displays "different"
but
model.setValueAt( "begin",0,0); does something odd?

This line looks a little fishy:
Object [][] data = pipeschedlight;

are you sure that is not just a null?
 
J

Jacques Chaurette

Hi Roedy, there is no exception, the model will not repaint the initial
values on the displayed jtable grid after a change of values. After the
initial values are entered I make a change with

setValueAt( "different", 0,0,) then when I return to

setValueAt( "initial", 0,0,) "different" is still displayed.

Object [][] data = pipeschedlight; is the initial set of values for
the table.

Jacques






Roedy Green said:
I have a jTable that is populated with an array of strings. During the
course of the program these values get changed depending on the selection
of
a jcombobox. What I have noticed is that I am unable to set the initial
values back in the table. I can set the values to any other of my data
string arrays except the initial ones.

What happens when you try?

1. you get an exception? What is it?

2. the model itself does not change?

3. the model does not repaint?

Are you saying that

model.setValueAt( "begin", 0,0 ); displays "begin"
model.setValueAt( "different", 0,0,) displays "different"
but
model.setValueAt( "begin",0,0); does something odd?

This line looks a little fishy:
Object [][] data = pipeschedlight;

are you sure that is not just a null?
 
R

Roedy Green

Hi Roedy, there is no exception, the model will not repaint the initial
values on the displayed jtable grid after a change of values. After the
initial values are entered I make a change with

setValueAt( "different", 0,0,) then when I return to

setValueAt( "initial", 0,0,) "different" is still displayed.

Let's look at your code for setValueAt.

I suspect this won't work either:
setValueAt( "a", 0,0);
setValueAt( "b", 0,0);
setValueAt( "c",0,0);

if it does but
setValueAt( "a", 0,0);
does not work,
I suspect you may have some filter in there to toss bad value and your
initial bad value snuck in via pipeschedlight.
 
J

Jacques Chaurette

Nope, I have tried mixing up a, b, or c and always the initial values cannot
be re-entered.

Jacques
 
J

Jacques Chaurette

OK, there`s probably a better solution but I found a workaround.

I initialize the jTable grid with values that I will never use or call again
and this solves the problem.

Jacques

Jacques Chaurette said:
Nope, I have tried mixing up a, b, or c and always the initial values
cannot be re-entered.

Jacques
 
T

Thomas Hawtin

Jacques said:
Object [][] data = pipeschedlight;

You don't define pipeschedlight anywhere, so this isn't the complete source.
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}

Much better to not depend on the data. In general, there may be no rows,
or there may be nulls or the classes may use polymorphism. Better to be
explicit.

Tom Hawtin
 
V

Vova Reznik

Thomas said:
Jacques said:
Object [][] data = pipeschedlight;


You don't define pipeschedlight anywhere, so this isn't the complete
source.
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}


Much better to not depend on the data. In general, there may be no rows,
or there may be nulls or the classes may use polymorphism. Better to be
explicit.

Tom Hawtin

If model has no records then model won't check class.
But any way, better to have:
Object o = getValueAt(0, columnIndex);
return o == null ? Object.class : o.getClass();
 
Z

zero

Hello All, and thanks for any help.

I have a jTable that is populated with an array of strings. During the
course of the program these values get changed depending on the
selection of a jcombobox. What I have noticed is that I am unable to
set the initial values back in the table. I can set the values to any
other of my data string arrays except the initial ones. I am using the
setValue method of the table model. Here is the jtable tablemodel
constructor:

<snipped>

When you try to reset the original data, do the debug lines output the
correct info, something incorrect, or nothing at all?

Some general remarks:

- class MyTableModel extends AbstractTableModel
it is better to extend DefaultTableModel and just override what needs to be
different. Oh, and try to come up with a more original name for the class
;-)

- Object [][] data = pipeschedlight;
like Roedy Green I am very suspicious of this line. DefaultTableModel
includes a protected Vector dataVector to store the data, wich will be more
efficient. You can then use a constructor to set the data you want, eg
MyTableModel(String[][] data). Don't forget to provide a no-argument
constructor as well.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,585
Members
45,080
Latest member
mikkipirss

Latest Threads

Top