wood burning stoves 2.0*
The moose likes Java in General and the fly likes setValueAt() and JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "setValueAt() and JTable" Watch "setValueAt() and JTable" New topic
Author

setValueAt() and JTable

henri henri
Ranch Hand

Joined: Oct 03, 2005
Posts: 115
I would like to be able to make changes to the data displayed in my JTable.
As you can see in the second class, I did not override setValueAt() which takes a two dimensional array. Instead I used the setRow() method seen below to set the row of JavaBean values in the JTable. It works very well.

I usually will try everything before coming here to ask for help, but I am really at a loss as to how to do this. The TreeMap in the method below is simply a temporary container to hold some JavaBeans in alphabetical order.
I would sincerely appreciate any advice that would help me find the solution myself. I just need advice on how to get and set the new value in the JTable. Getting it into a JavaBean is very straightforward.




Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
I'm not sure what you are asking? All you
really say is "It works very well." But
since you are posting here, I presume there
must be a gap between how your program is
behaving and how you would like it to behave.
Please elaborate.

Until you provide an elaboration, it's hard
to say much about your code, but I will say
two things.

1) The second line of the code you provided
calls model.setRowCount(0) but you haven't
shown us how 'model' was originally instantiated.

2) It may have been easier for you to subclass
AbstractTableModel instead of DefaultTableModel.
AbstractTableModel doesn't have an addRow() method,
though you could write one, but with AbstractTableModel
you wouldn't have to pull out all your data into arrays.
Your could implement getValueAt() sort of like this
On the minus side, if you do it this way you would have
to be responsible for calling the fireTableXXXX() methods
at the appropriate places. (DefaultTableModel.addRow()
call fireTableRowsInserted() for you.)


bitguru blog
henri henri
Ranch Hand

Joined: Oct 03, 2005
Posts: 115
Brain, I read your message. Thanks for the time you took to respond. I am finding JTable and Model very complicated. The reason for the setRowCount(0) is so when the user clicks on another letter button the JTable is cleared to make way for the next rows of Beans. Let me digest what you have written and try to post some further updates.
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by henri henri:
I am finding JTable and Model very complicated.


Well if it helps, it is kind of complicated,
at least at first. Alas, I find the tree models to
be more complicated still.

Usually in the java libraries if there are two
classes, say AbstractFoo extending DefaultFoo,
you would usually not bother much with AbstractFoo
and would go ahead and extend DefaultFoo. I think
table models are an exception to this guideline.
I would say for most purposes you should skip
DefaultTableModel and extend AbstractTableModel.

The reason for the setRowCount(0) is so when the user clicks on another letter button the JTable is cleared to make way for the next rows of Beans.


I have no problem with the call to setRowCount(),
I was just mentioning that you haven't shown us
how you instantiate your model. This means I have
to guess about the state of your table model when
you refer to it on line 2, instead of knowing for
sure.

Let me digest what you have written and try to post some further updates.


sounds good
[ November 07, 2005: Message edited by: Brian Cole ]
henri henri
Ranch Hand

Joined: Oct 03, 2005
Posts: 115
In this somewhat awkward method, I have acheived what I failed to express in this thread, namely that I want to have the user be able to change data in the JTable. I have succeeded in allowing a user to change the category and for the new data to be transmitted back to storage in the corresponding JavaBean. After studying the options, I discovered that it is possible to combine methods of JTable and methods of the DefaultTableModel.

I am not sure if is wise to have huge methods, or break them up into different smaller tasks. I am still learning all these things.

You have a visual guide below. The small JFrame in the foreground is a password gateway to prevent unintentional tampering with the JTable data.

Any advice you can give me on how to detect changes in other columns would be appreciated. Detecting a choice in column 1 was easy because there is a JComboBox for the film categories.

http://pollscanada.com/screenshot.html


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: setValueAt() and JTable