Thread View: borland.public.jbuilder.java.swing
2 messages
2 total messages
Started by Jacques Chaurette
Mon, 10 Oct 2005 20:31
problem with jtable cell data
Author: Jacques Chaurette
Date: Mon, 10 Oct 2005 20:31
Date: Mon, 10 Oct 2005 20:31
83 lines
2811 bytes
2811 bytes
I am having trouble getting reliable access to an individual cell content.
The code for my jtable is like this:
class MyTableModel extends AbstractTableModel {
private String[] columnNames = {"Dia. (in)",
"Instal. Cost",
};
private Object[][] data = {
{"3", "25"},
{"4", "35"},
{"5", "40"},
{"6", "45"},
{"8", "50"}
};
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];
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
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;
//}
}
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("--------------------------");
}
}
When I edit a cell value in the table GUI, the change is not effective until
another cell is selected. and I want to use this modified value somewhere
else in the program. I cannot count on the user selecting another cell. How
can I get the change to be instantly incorporated into the table model?
Any help appreciated.
Thanks.
Jacques
Re: problem with jtable cell data
Author: Jacques Chaurette
Date: Tue, 11 Oct 2005 02:17
Date: Tue, 11 Oct 2005 02:17
101 lines
3548 bytes
3548 bytes
Found the solution on the internet, it seems that the celLEditor is still
active and must be de-activated. AND IT WORKS
public void stopCellEditing() {
TableCellEditor editor = jTable1.getCellEditor();
if ( editor != null ) {
editor.stopCellEditing();
}
}
Cheers,
Jacques
"Jacques Chaurette" <jacques.chaurette@sympatico.ca> wrote in message
news:434acf22$1@newsgroups.borland.com...
>I am having trouble getting reliable access to an individual cell content.
>The code for my jtable is like this:
> class MyTableModel extends AbstractTableModel {
> private String[] columnNames = {"Dia. (in)",
> "Instal. Cost",
> };
> private Object[][] data = {
> {"3", "25"},
> {"4", "35"},
> {"5", "40"},
> {"6", "45"},
> {"8", "50"}
> };
>
> 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];
> }
>
> public Class getColumnClass(int c) {
> return getValueAt(0, c).getClass();
> }
>
> 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;
> //}
> }
>
> 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("--------------------------");
> }
> }
>
>
> When I edit a cell value in the table GUI, the change is not effective
> until another cell is selected. and I want to use this modified value
> somewhere else in the program. I cannot count on the user selecting
> another cell. How can I get the change to be instantly incorporated into
> the table model?
>
> Any help appreciated.
> Thanks.
>
> Jacques
>
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads