Link to home
Start Free TrialLog in
Avatar of T_Shah
T_Shah

asked on

JTable: Update Value

Hello:
I have created JTree using Arraylist. So Left side is JTree and when I pick the node it display its content to right side in JTable.  In the Table there are 5 colunms. 2 columns are JComboBox.
I have created JTable like this:
model = new DefaultTableModel(data, columns);
table = new JTable(model)
{

      public void setValueAt(Object value, int row, int col)
      {
            data[row][col] = value;
      }
};


// I add new Row like this
if(e.getSource() == AddNewRow)
{
  // add to array list  
  model.addRow(data);
}

Everything is working fine but the only problem is  The value that I change does not update right away.
When I select another node and go back to the previous node I see all the value that I have changed.

Can you please show me how to make this thing work?

Thanks
Avatar of hoomanv
hoomanv
Flag of Canada image

You dont need to override the setValueAt method
The original setValueAt invokes fireTableCellUpdated to notify the JTable of changes

public void setValueAt(Object value, int row, int col) {
      rowData[row][col] = value;
      fireTableCellUpdated(row, col);
}
Avatar of T_Shah
T_Shah

ASKER

You are right about that.
It was working fine but since Add tried to add new rows it is not working
Avatar of T_Shah

ASKER

Is it possible because of size of data.
Initially I have size of data as follow

data = new String[arraylist.size()][5];

now I added new row I add like this:

if(e.getSource() == AddNewRow)
{
  // add values to array list  
  model.addRow(data);
}

Becaue I am having this problem since I am trying to add new Row...

Any suggesstion!!!!
DefaultTableModel is not restricted to row or column count
There is somthing else working wrong in your code
Avatar of T_Shah

ASKER

Thanks for the advise.
I am doing this way
model = new DefaultTableModel(data, columns);
table = new JTable(model)

It does not work

But If I do this
table = new JTable(data, columns)

It let me change value and works fine but the important thing is I can not add/remove row
ASKER CERTIFIED SOLUTION
Avatar of hoomanv
hoomanv
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial