Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Appending image to text inside table cell

 
Rite Sara
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
I am trying to insert an image along with some text for all cells in second column of a table using swings.
But when I do " model.addRow(new Object[]{fields[i].getFullName(),icon}); " where icon is an IconImage and model is table model. I get path to the image instead of actual image.
Please provide some sample code to achieve this in simple manner.



Thanks,
 
Rob Camick
Ranch Hand
Posts: 2616
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to tell the table the column contains an Icon so the proper renderer can be used.


 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ehm Rob? Shouldn't you override that method in the model, not the view? Especially since index 0 of the JTable may not be index 0 of its model after dragging around some columns.
 
Rob Camick
Ranch Hand
Posts: 2616
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote: Especially since index 0 of the JTable may not be index 0 of its model after dragging around some columns.


The getValueAt(...) method of the table does the conversion of the row/column value from the view to the model before it invokes model.getValueAt(...).
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, of course. I mistook that 0 for the column index. I still think that having the model return the proper column class is a better idea though.
 
Rite Sara
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys thanks for replying so soon ... I am now able to add image to the column ..
But as per my requirement I need to add some text after the image in the same column ..

I tried using label :

label1 = new JLabel(messg ,icon, JLabel.CENTER);
model.addRow(new Object[]{fieldName , label1});

But in this case it's not taking the image and showing some source code instead of it ...
Please let me know how I can append some text to an image in the same column ..



Thanks,
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a custom TableCellRenderer that returns the component itself. Here you'll find an example that uses a ListCellRenderer like that; the TableCellRenderer would work the same.
 
Rite Sara
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob,
I created following inner class :
class MyCellRenderer implements TableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value,boolean isSelected, boolean hasFocus,int row,int column){
Component com = (Component)value;
return com;
}
}

but how to bind it with table ? as there is no setCellRenderer() method or getCellRenderer() method .. I am attaching sample code given by you. I am trying to run on this code first before applying to my code. Could you please have a look ?

 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no setCellRenderer, true. There is setDefaultCellRenderer though. Make your TableModel return Component.class for that column, then call setDefaultCellRenderer(Component.class, renderer).
 
Rite Sara
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Rob ,

Could you please give some sample code where you are using setDefaultCellRenderer(Component.class, renderer) ..
How to implement the renderer to work for both images and text ??
Please give some sample code ...

This is how I am implementing model in my code ..

model = new DefaultTableModel();
jTable1 = new javax.swing.JTable(model){

public Class getColumnClass(int column) {
return getValueAt(1, column).getClass();
}
};
model.addColumn("Field");
model.addColumn("Status");

label1 = new JLabel(messg,icon,JLabel.CENTER); // icon is an image
model.addRow(new Object[]{customfield.customfield.getFullName(),label1});

but it won't render image from the label and shows some source code instead ..
Please let me know how to get this work ..
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anywhere after you've created the JTable will do. In this case I'd do it just after setting up the columns.
 
Rite Sara
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please let me know how to implement
setDefaultCellRenderer(Component.class, renderer) ..

How to get the renderer ??
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
new MyCellRenderer() maybe?
 
Rob Camick
Ranch Hand
Posts: 2616
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please let me know how to implement


Did you search the forum or the web for examples?

Now that you've been introduced to the proper method to use you can do you own searching. We should not have to post a specific example because examples can be found all over the web.

You can also read the JTable API and follow the link to the Swing tutorial on "How to Use Tables" for more information about renderers.
 
Rite Sara
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
I am able to resolve this and add label and text together in a table column.
But now the issue is ... If I click on any of the image (i.e. label = image + text ) in the column the images and text disappears and it displays the source code in that palce in the table ..

How to make the table cells unclickable or somthing like that ..
Please give some solution for this ...
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Override isCellEditable to return false.
 
Rite Sara
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This perfectly resolved my issue ..
Thanks Darryl ..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic