Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JTable setValueAt(Object o, int col, int row)  RSS feed

 
Atrus Greyor
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using the basic JTable.

If my JTable is named data, and I call:

data.setValueAt("Moo", 1, 1);

The String is inserted, however, it's not visible unless I double click on the cell. How do I modify the cell so that it will appear without me having to double click?

Thanks,
Atrus.
 
Rob Camick
Ranch Hand
Posts: 2752
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The String is inserted, however, it's not visible unless I double click on the cell. How do I modify the cell so that it will appear without me having to double click?


This is the default behaviour. If it doesn't work then you have written some custom code that is causing the problem.

Read the JTable API and follow the link to the Swing tutorial on "How to Use Tables" for a working example.

If you need further help then you need to create a SSCCE (Short, Self Contained, Compilable and Executable, Example Program), that demonstrates the incorrect behaviour.

Don't forget to use the Code Formatting Tags so the posted code retains its original formatting. That is done by selecting the code and then clicking on the "Code" button above the question input area.
 
Brian Cole
Author
Ranch Hand
Posts: 932
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Atrus Greyor wrote:I'm using the basic JTable.

If my JTable is named data, and I call:

data.setValueAt("Moo", 1, 1);

The String is inserted, however, it's not visible unless I double click on the cell. How do I modify the cell so that it will appear without me having to double click?

Who wrote the setValueAt() method that you are calling?

It sounds like setValueAt() is not calling fireTableCellUpdated(row, int col). If it did, then you would see the change appear immediately.

[note: I'm presuming that your table model inherits from AbstractTableModel. If it doesn't, then setValueAt() should still fire an event, but the fireTableCellUpdated() method isn't available so you'll have to figure out how it should be done.]
 
Atrus Greyor
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my TableModel:



There isn't anything special. I don't modify the "setValueAt()" at all. However it does extend DefaultTableModel instead of AbstractTableModel
 
Brian Cole
Author
Ranch Hand
Posts: 932
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Atrus Greyor wrote:I don't modify the "setValueAt()" at all. However it does extend DefaultTableModel instead of AbstractTableModel


DefaultTableModel extends AbstractTableModel, so no problem there.

If you take a look at the source code for DefaultTableModel.setValueAt(), you'll see it does call fireTableCellUpdated()...

...so I'm at a loss to explain why it wouldn't be working. I guess we need you to construct a small compilable example that exhibits the problem.

Btw, DefaultTableModel will copy the cell data from the two-dimensional array you pass it into a Vector of Vectors. You might be better off extending AbstractTableModel and writing getValueAt()/setValueAt() methods that access the data directly from wherever you store it (no copying). This has no bearing on your problem, though.
 
Atrus Greyor
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I see what my problem is, but, I'm not exactly sure how to fix it.

With the below, it imports correctly, however the images do not display correctly, showing their filename instead of the actual image:



I learned before that for the JTable to properly display ImageIcons, you need to add the getColumnClass method to the table. Which I did.



This is where the problem lies, and where you have to double click to see "Moo".
 
Rob Camick
Ranch Hand
Posts: 2752
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all why did you create a custom TableModel. Your model does nothing special and you should just be using the DefaultTableModel to keep the program simple.

it imports correctly, however the images do not display correctly, showing their filename instead of the actual image:


It works fine for me when I use the DefaultTableModel.

This is where the problem lies, and where you have to double click to see "Moo".


Well you told the table, via the getColumnClass() method that the columns contain Icons, so the Icon renderer is used to renderer each cell. The String "moo" is not an icon to nothing is displayed.

When you double click you invoke the editor. The editor simply display the toString() value of the Object in the cell. That is why you now see "moo" or the file name of the icon.
 
Atrus Greyor
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How exactly did you use the DefaultTableModel? I did it like the below, and I still only saw the path to the image.

 
Rob Camick
Ranch Hand
Posts: 2752
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You already know the answer to that question. From your previous posting you stated:

I learned before that for the JTable to properly display ImageIcons, you need to add the getColumnClass method to the table. Which I did.


Please take a little time to think about it...



 
Atrus Greyor
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's hanging me up with that is I would like the column to display both ImageIcons and Strings. So I don't think that the "getColumnClass" is the right way to go. Unless it needs to be written in a way that I'm just not seeing.
 
Rob Camick
Ranch Hand
Posts: 2752
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's hanging me up with that is I would like the column to display both ImageIcons and Strings


Well, then ask a proper question. No where in your fist 6 postings did you state this was the problem you where trying to solve so you have been wasting all of our time. We are not mind readers.

Override the getCellRenderer() method to return the appropriate renderer for the type of data in the cell. There are plenty of examples on the net.
 
Atrus Greyor
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I apparently wasn't sure of my question either. Thanks for helping to point me in the correct direction though.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!