Duplicate data when setValueAt is called

Hi,

I have 2 column 1st column contains the check box(isBold text) and 2nd column contains the text. What I want to achieve is when I click the checkbox in the table, the size of the text is change and will be concat when the size of the text if more than 10 char lenght. (e.g."I'm lazy to code" when I click the checkbox, the text in the cell will concat to "I'm lazy t"). But My problem is when I select other cell, like row 10 in the table...the previous text I entered is copied to the current selected cell. why?

I am using JTable and AbstractTableModel. Any idea, why it is copied? I've tried table.stopAllCellEditing(). But it still wont work.

publicvoid setValueAt(Object aValue,int rowIndex,int columnIndex){

MyObj obj = this.rows.get(rowIndex);

switch(columnIndex){

case 0: obj.setBoldText((Boolean)aValue).booleanValue());break;

case 1: obj.setText((String)aValue);break;

fireTableCellUpdated(rowIndex, columnIndex);

}

publicvoid setObjValueAt(String text,int rowIndex,int columnIndex,int len){

if(len == BOLD_TEXT){

if(text.length() > BOLD_TEXT){

text = text.substring(0, BOLD_TEXT);

}

this.textField.setLength(BOLD_TEXT);

}else{

this.textField.setLength(SIMPLE_TEXT);

}

this.textField.setText(text);

setValueAt(textField.getText(), rowIndex, 1);

}

[2260 byte] By [mutant2000a] at [2007-11-26 18:08:13]
# 1
you are probably fireing the event again in the new cell and the previous information being set inmto the new cell.
grilleda at 2007-7-9 5:39:49 > top of Java-index,Desktop,Core GUI APIs...
# 2

any solution? :)

I place the code in isCellEditable MyTableModel class.

public boolean isCellEditable(int row, int col) {

if(col == 0) return false;

else{

setValueAt(true, row, 0);

if(((Boolean).getValueAt(row, 1)).booleanValue()){

setObjValueAt(text, row, 1, BOLD_TEXT);

}else{

setObjValueAt(text, row, 1, SIMPLE_TEXT);

}

}

}

mutant2000a at 2007-7-9 5:39:49 > top of Java-index,Desktop,Core GUI APIs...
# 3

> any solution?

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.

camickra at 2007-7-9 5:39:49 > top of Java-index,Desktop,Core GUI APIs...
# 4

All 3 methods above are included in MyTableModel class. If I try to modify row 1 col 1, and add the this text in the cell "Hello World" and click the Bold checkbox in row 1 col 0....The text is concat to char max of 10 which is "Hello Worl". The problem is when i select the new cell, row 3 col 1, the previous data "Hello Worl" is copied to the new selected cell. Why is that? Grilled said that it fires the event again in the new cell which is correct...but I wanted to fire the event so that the cell in col 1 is always updated(char length size and concat according to char length set in the cell) when the user clicks the checkbox in col 0.

public class MyApp extends JPanel{

private JTable table = null;

private MyTableModel tableModel = null;

public void MyApp(){

this.tableModel = new MyTableModel();

this.table = new JTable();

this.table.setModel(tableModel);

}

}

public class MyTableModel extends AbstractTableModel{

private String[] myHeader = {"Bold Text", "Text"};

public void setValueAt(Object aValue, int rowIndex, int columnIndex) {

MyObj obj = this.rows.get(rowIndex);

switch(columnIndex){

case 0: obj.setBoldText((Boolean)aValue).booleanValue()); break;

case 1: obj.setText((String)aValue); break;

}

fireTableCellUpdated(rowIndex, columnIndex);

public void setObjValueAt(String text, int rowIndex, int columnIndex, int len){

if(len == BOLD_TEXT){

if(text.length() > BOLD_TEXT){

text = text.substring(0, BOLD_TEXT);

}

this.textField.setLength(BOLD_TEXT);

}else{

this.textField.setLength(SIMPLE_TEXT);

}

this.textField.setText(text);

setValueAt(textField.getText(), rowIndex, 1);

}

public boolean isCellEditable(int row, int col) {

if(col == 0) return false;

else{

setValueAt(true, row, 0);

if(((Boolean).getValueAt(row, 1)).booleanValue()){

setObjValueAt(text, row, 1, BOLD_TEXT);

}else{

setObjValueAt(text, row, 1, SIMPLE_TEXT);

}

}

}

// some methods here...

}

mutant2000a at 2007-7-9 5:39:49 > top of Java-index,Desktop,Core GUI APIs...
# 5
any solution?
mutant2000a at 2007-7-9 5:39:49 > top of Java-index,Desktop,Core GUI APIs...
# 6
Where's the SSCCE?
camickra at 2007-7-9 5:39:49 > top of Java-index,Desktop,Core GUI APIs...
# 7

Here's the SSCCE you've requested. Hope there are solutions.

Thanks in advance.

public class MyApp extends JFrame{

private JTable table = null;

private MyTableModel tableModel = null;

public void MyApp(){

this.tableModel = new MyTableModel();

this.table = new JTable();

this.table.setModel(tableModel);

JScrollPane scrollPane = new JScrollPane( table );

getContentPane().add( scrollPane );

}

public static void main(String[] args)

{

MyApp frame = new MyApp();

frame.setDefaultCloseOperation( EXIT_ON_CLOSE );

frame.pack();

frame.setLocationRelativeTo( null );

frame.setVisible(true);

}

}

public class MyTableModel extends AbstractTableModel{

private String[] myHeader = {"Bold Text", "Text"};

private ArrayList<Row> list;

private int BOLD_TEXT = 10;

private int SIMPLE_TEXT = 20;

public MyTableModel(){

super();

this.list = new ArrayList<Row>();

Row dataList = new Row();

dataList.setBoldText(true);

dataList.setText("Test 01");

Row dataList2 = new Row();

dataList2.setBoldText(false);

dataList2.setText("Test 02");

this.list.add(dataList);

this.list.add(dataList2);

fireTableDataChanged();

}

public void setValueAt(Object aValue, int rowIndex, int columnIndex) {

MyObj obj = this.rows.get(rowIndex);

switch(columnIndex){

case 0: obj.setBoldText((Boolean)aValue).booleanValue()); break;

case 1: obj.setText((String)aValue); break;

}

fireTableCellUpdated(rowIndex, columnIndex);

}

public void setObjValueAt(String text, int rowIndex, int columnIndex, int len){

if(len == BOLD_TEXT){

if(text.length() > BOLD_TEXT){

text = text.substring(0, BOLD_TEXT);

}

this.textField.setLength(BOLD_TEXT);

}else{

this.textField.setLength(SIMPLE_TEXT);

}

this.textField.setText(text);

setValueAt(textField.getText(), rowIndex, 1);

}

public boolean isCellEditable(int row, int col) {

if(col == 0) return false;

else{

setValueAt(true, row, 0);

if(((Boolean).getValueAt(row, 1)).booleanValue()){

setObjValueAt(text, row, 1, BOLD_TEXT);

}else{

setObjValueAt(text, row, 1, SIMPLE_TEXT);

}

}

}

public int getColumnCount() {

return this.myHeader.length;

}

public int getRowCount() {

return this.list.size();

}

public Object getValueAt(int rowIndex, int columnIndex) {

switch(columnIndex){

case 0: return (new Boolean(this.list.get(rowIndex).isBoldText()));

case 1: return this.list.get(rowIndex).getText();

}

}

public void setValueAt(Object aValue, int rowIndex, int columnIndex) {

Row rowData = this.list.get(rowIndex);

switch (columnIndex) {

case 0 :

rowData.setBoldText(((Boolean)aValue).booleanValue());

break;

case 1 :

rowProxy.setText(aValue.toString());

break;

}

fireTableCellUpdated(rowIndex, columnIndex);

}

public Class<?> getColumnClass(int col) {

switch(col) {

case 0 : return Boolean.class;

case 1 : return String.class;

default : return String.class;

}

}

public String getColumnName(int col) {

return this.myHeader[col];

}

public class Row{

boolean boldText;

String text;

public boolean isBoldText(){

return this.boldText;

}

public void setBoldText(boolean isBold){

this.boldText = isBold;

}

public void setText(String text){

this.text = text;

}

public String getText(){

return this.text;

}

}

}

ciao

mutant2000a at 2007-7-9 5:39:49 > top of Java-index,Desktop,Core GUI APIs...
# 8
One of the "C"s in SSCCE stands for compileable.a) where are the import statementsb) what is MyObj ?c) what is this.textField?...We want something that we can copy, paste, compile, and execute to see the behaviour you described.
camickra at 2007-7-9 5:39:49 > top of Java-index,Desktop,Core GUI APIs...