my dog learned polymorphism*
The moose likes Swing / AWT / SWT and the fly likes JTable Cell Rendere Not showing Icons properly Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable Cell Rendere Not showing Icons properly" Watch "JTable Cell Rendere Not showing Icons properly" New topic
Author

JTable Cell Rendere Not showing Icons properly

Muhammad Asghar
Greenhorn

Joined: Mar 15, 2011
Posts: 6

Hi All,

in my application i need to display file system files in JTable, when i click on the JTree node which is any system folder, the contents of that folder are shown in the JTable,
In the first column of the Jtable, with the name of the file or folder icon is shown, the icon is fetched from the system icon and is displayed.
Every thing is working fine the problem is that, when renderer renders icon, the icon of the first file(first row of Jtable) is repeated in all, i mean the icon does not change in the subsequent rows of the JTable, here my code is in which a render gets icon and the model displays it in the JTable

/////
class KeyIconCellRenderer extends DefaultTableCellRenderer {
public KeyIconCellRenderer(String ext) {
File file = new File(ext);
Icon icon = FileSystemView.getFileSystemView().getSystemIcon(file);
setIcon(icon);
}
}
//////
and here is code where i am using rendrer to display
///
private class Selection implements TreeSelectionListener {
public void valueChanged(TreeSelectionEvent e) {
Object[] myData= new Object[6];
TreePath path = e.getPath();
FileUtil util= new FileUtil();
FileMetaData metaData;

Vector<FileMetaData> vList = new Vector<FileMetaData>();
DefaultMutableTreeNode node = (DefaultMutableTreeNode)treeMainView.getLastSelectedPathComponent();

FileInfo info =(FileInfo)node.getUserObject();
File filePath= info.getFilepath();
vList=util.getChildList(filePath);
dtModel.getDataVector().removeAllElements();

for(int i=0;i<vList.size(); i++){
Vector v= new Vector();
metaData=(FileMetaData)vList.get(i);
v.add(metaData.getName());
tblMainView.getColumnModel().getColumn(0).setCellRenderer(new KeyIconCellRenderer(metaData.getClientpath()));
v.add(metaData.getClientpath());
if(metaData.isDirectory()){
v.add("");
}else
{
v.add((FileHelper.getSizeString(metaData.getSize())));
}
if(metaData.isDirectory()){
v.add("");
}else
{
v.add(new Date(metaData.getTime()));
}
if(metaData.isDirectory()){
v.add("Folder");
}else
{
v.add("File");
}
v.add("Pending Upload");

dtModel.insertRow(0, v);
}
tblMainView.repaint();
}
}

///

as in the attached image, only the icon of the fist file is repeated in all rows,

Please help, it will be a huge favor,
Thanks



[Thumbnail for attachment.PNG]

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4531
    
    5

Hi Muhammad, please BeForthrightWhenCrossPostingToOtherSites
http://stackoverflow.com/questions/5456721/java-jtable-not-showing-icons-properly


luck, db
There are no new questions, but there may be new answers.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JTable Cell Rendere Not showing Icons properly