| Author |
Appending image to text inside table cell
|
Rite Sara
Ranch Hand
Joined: Feb 13, 2010
Posts: 56
|
|
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
Joined: Jun 13, 2009
Posts: 1788
|
|
You need to tell the table the column contains an Icon so the proper renderer can be used.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
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.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Rob Camick
Ranch Hand
Joined: Jun 13, 2009
Posts: 1788
|
|
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
Joined: Oct 27, 2005
Posts: 19216
|
|
|
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
Joined: Feb 13, 2010
Posts: 56
|
|
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
Joined: Oct 27, 2005
Posts: 19216
|
|
|
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
Joined: Feb 13, 2010
Posts: 56
|
|
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
Joined: Oct 27, 2005
Posts: 19216
|
|
|
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
Joined: Feb 13, 2010
Posts: 56
|
|
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
Joined: Oct 27, 2005
Posts: 19216
|
|
|
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
Joined: Feb 13, 2010
Posts: 56
|
|
Could you please let me know how to implement
setDefaultCellRenderer(Component.class, renderer) ..
How to get the renderer ??
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
new MyCellRenderer() maybe?
|
 |
Rob Camick
Ranch Hand
Joined: Jun 13, 2009
Posts: 1788
|
|
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
Joined: Feb 13, 2010
Posts: 56
|
|
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
Joined: May 03, 2008
Posts: 4163
|
|
|
Override isCellEditable to return false.
|
luck, db
There are no new questions, but there may be new answers.
|
 |
Rite Sara
Ranch Hand
Joined: Feb 13, 2010
Posts: 56
|
|
This perfectly resolved my issue ..
Thanks Darryl ..
|
 |
 |
|
|
subject: Appending image to text inside table cell
|
|
|