Results 1 to 2 of 2
  1. #1
    fanjavaid is offline Member
    Join Date
    Dec 2012
    Location
    Jakarta, Indonesia
    Posts
    23
    Rep Power
    0

    Question Problem set icon in JTable cell from Model

    Hello, i have problem how to set icon image in JTable Cell,
    I have TableModel, and all data and column configured by this Model.
    Here is :

    Java Code:
    public class TabelDataModel extends AbstractTableModel{
        private List<ServerData> listServerData;
        
        public TabelDataModel() {
            listServerData = new ArrayList<>(0);
        }
        
        public void setData(List<ServerData> listServerData) {
            this.listServerData = listServerData;
            fireTableDataChanged();
        }
        
        @Override
        public int getRowCount() {
            return listServerData.size();
        }
    
        @Override
        public int getColumnCount() {
            return 6;
        }
    
        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            
            switch(columnIndex) {
                case 0 :
                    return listServerData.get(rowIndex).isDirectory();
                case 1 :
                    return listServerData.get(rowIndex).getFileName();
                case 2 :
                    return listServerData.get(rowIndex).getSize();
                case 3 :
                    return listServerData.get(rowIndex).getDateModified();
                case 4 :
                    return listServerData.get(rowIndex).getUser();
                case 5 :
                    return listServerData.get(rowIndex).getPermissions();    
                default :
                    return null;
            }
        }
        
        @Override
        public String getColumnName(int column) {
            switch(column) {
                case 0 :
                    return "icon";
                case 1 :
                    return "Filename";
                case 2 :
                    return "Size (Byte)";
                case 3 :
                    return "Date Modified";
                case 4 :
                    return "User";
                case 5 :
                    return "Permission";    
                default :
                    return null;
            }
        }
        
    }
    I want to set the column index 0 have an icon,
    and the icon is check if the value in column 0 is TRUE i set icon A, and else set icon B.

    This is my controller to render the content of table.
    Java Code:
    Vector <ChannelSftp.LsEntry> fileList = Helper.listDirectory("/home/medifile/", view.getChannelSftp());            
                for (int i = 0; i < fileList.size(); i++) {
                    String [] a = fileList.get(i).toString().split(" ");
                    
                    ServerData serverData = new ServerData();
                    
                    serverData.setDirectory(fileList.get(i).getAttrs().isDir());
                    serverData.setFileName(fileList.get(i).getFilename());
                    serverData.setPermissions(fileList.get(i).getAttrs().getPermissionsString());
                    serverData.setDateModified(fileList.get(i).getAttrs().getMtimeString());
                    serverData.setSize(fileList.get(i).getAttrs().getSize());
                    serverData.setUser(String.valueOf(fileList.get(i).getAttrs().getUId()));
                    
                    
                    list.add(serverData);
                }
                
                tbModel.setData(list);
                view.getTblData().setModel(tbModel);
    Please help me, thank you :)

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default Re: Problem set icon in JTable cell from Model

    1) You need to store the Icon you want displayed in the TableModel

    2) You need to override the getColumnClass(..) method to return Icon.class for the column that contains the icon.

Similar Threads

  1. Jtable colour cell problem
    By fuzzdn in forum New To Java
    Replies: 10
    Last Post: 07-30-2011, 05:30 PM
  2. Facing the Problem When I placed my Custom ComboCheckBox in JTable cell
    By miryala.rahul@gmail.com in forum AWT / Swing
    Replies: 0
    Last Post: 06-26-2010, 03:14 PM
  3. jtable cell background
    By anilkumar_vist in forum New To Java
    Replies: 0
    Last Post: 02-22-2010, 04:08 PM
  4. Merg JTABLE CELL
    By anilkumar_vist in forum New To Java
    Replies: 1
    Last Post: 01-25-2010, 04:02 PM
  5. JTable view/model sync problem
    By edcincy in forum AWT / Swing
    Replies: 0
    Last Post: 09-12-2008, 06:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •