Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

JTable: Update Value

Posted on 2006-10-23
9
Medium Priority
?
238 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:T_Shah
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
9 Comments
 
LVL 14

Expert Comment

by:hoomanv
ID: 17790103
You dont need to override the setValueAt method
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 17790210
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);
}
0
 

Author Comment

by:T_Shah
ID: 17790481
You are right about that.
It was working fine but since Add tried to add new rows it is not working
0
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 

Author Comment

by:T_Shah
ID: 17790705
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!!!!
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 17790755
DefaultTableModel is not restricted to row or column count
There is somthing else working wrong in your code
0
 

Author Comment

by:T_Shah
ID: 17791039
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
0
 
LVL 14

Accepted Solution

by:
hoomanv earned 300 total points
ID: 17794036
> I can not add/remove row
You can, thru the DefaultTableModel
Call model.addRow()
And it will invoke fireTableRowsInserted() internally
0

Featured Post

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question