0

I am trying to add a row with an image to a JTable. The code below displays only the textual information from the ImageIcon.

DefaultTableModel model = new DefaultTableModel(); 
JTable chat = new JTable(model); 
    model.addColumn(null); 
    model.addRow(new Object[]{"v1"});
    model.addRow(new Object[]{emojiImages.get("smile.png")});

ImageIcon for DefaultTableModel In this thread, tenorsax says "Override getColumnClass() and return Icon.class." I have looked at his linked resource about Editors and Renderers, but I am having trouble figuring out how to apply that.

Community
  • 1
  • 1
NikonJR
  • 59
  • 7
  • 1
    That's exactly it -- you need to extend DefaultTableModel, and you can do this as an anonymous class if you like, and override that method so that the renderer knows that it has an Icon to display, but I don't see you doing this anywhere in your code above. How are you stuck? Do you understand how to extend classes and how to create anonymous inner classes? Your lack of attempt suggests a possible issue here with Java basics more so than Swing. – Hovercraft Full Of Eels May 01 '17 at 21:26
  • Hovercraft, I understand extending classes, though I don't know what you mean by anonymous inner classes. I'm still learning, but documentation can be vague sometimes. +1 for elaborating – NikonJR May 01 '17 at 21:46
  • Please have a look at the answer to the dup as it shows an anonymous inner class being created and used. – Hovercraft Full Of Eels May 01 '17 at 22:04
  • Ok, I see how that renders differently depending on what column it is, how would I do that on one row. I'm thinking it would be like checking if the object is a string or ImageIcon, then rendering accordingly. Is that right, or am I over-complicating it? – NikonJR May 01 '17 at 22:09
  • Let's see what you come up with. – Hovercraft Full Of Eels May 01 '17 at 22:12
  • Even my upper-classmate friend didn't know, so I'm compromising and using the column specific setup. Still open to an answer for sake of learning and for future users who want to know the same thing. – NikonJR May 02 '17 at 02:33

0 Answers0