This week's book giveaways are in the Jython/Python and Object-Oriented programming forums.
We're giving away four copies each of Machine Learning for Business: Using Amazon SageMaker and Jupyter and Object Design Style Guide and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

problem displaying jtable values

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Author
Posts: 974
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jacques Chaurette
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 974
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Normally trees don't drive trucks. Does this tiny ad have a license?
Level Up Your Code with a Pro Java IDE
https://www.jetbrains.com/idea
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!