wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes problem displaying jtable values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "problem displaying jtable values" Watch "problem displaying jtable values" New topic
Author

problem displaying jtable values

Jacques Chaurette
Greenhorn

Joined: Oct 13, 2005
Posts: 5
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[i][j]);
}
System.out.println();
}
System.out.println("--------------------------");
}
}

Cheers,

Jacques
Jacques Chaurette
Greenhorn

Joined: Oct 13, 2005
Posts: 5
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
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
I've tried your code and it seems fine to me.

How exactly does selection of a JComboBox change
the array values? It seems to me that if all the
changes to pipeschedlight happen via a call
to theInstanceOfMyTableModel.setValue() that
all will be well.


bitguru blog
Jacques Chaurette
Greenhorn

Joined: Oct 13, 2005
Posts: 5
I found a work around, it seems that the array that contains the initial values entered into the table cannot be re-used so I use another array for initial values represented by pipeschedinitialvalues[] that I do not use again and then everything works.

Jacques
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
I think I understand your work-around, but from your
code it makes no sense that it should make a difference.

If you want to be done with it, fine, but otherwise my
questions from the Oct 17 post stand.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem displaying jtable values
 
Similar Threads
JTable as User's input (?)
JScrollPane within JTable cell issue
Reg Appending new row to already existing JTable
Invisible JTable Column Header!
JCheckBox in JTable