Search code examples
javagwtgwt-celltable

CellTable with Image Column


I have celltable with 4 column (name size addedBy modifiedBy )

all the value is filled during the run time.

The table actully show the documents.

Documents can be pdf, txt , doc so

  1. i want to add icon before the name of the document.

  2. and i also want one more Image column before name column

My code so far.

*
private CellTable<FDocument> getDocumentTable() {
        if (documentTable == null) {
            documentTable = new CellTable<FDocument>();
            documentTable.setSize("600px", "300px");
            documentTable.addColumn(nameColumnD, "NAME");
            documentTable.addColumn(sizeColumnD, "SIZE");
            documentTable.addColumn(modified_by_ColumnD, "MODIFIED BY");
            documentTable.addColumn(dateColumnD, "MODIFIED ON");


        }
        return documentTable;
    }


TextColumn<FDocument> idColumnD = new TextColumn<FDocument>() {             
            @Override
            public String getValue(FDocumentobject) {
                // TODO Auto-generated method stub
                return object.getId();
            }
        };
        TextColumn<FDocument> nameColumnD = new TextColumn<FDocument>() {
            @Override
            public String getValue(FDocumentobject) {
                return object.getName();
            }
        };          
        TextColumn<FDocument> sizeColumnD = new TextColumn<FDocument>() {
            @Override
            public String getValue(FDocumentobject) {
                return object.getSize();                            
            }
        };
        TextColumn<FDocument> modified_by_ColumnD = new TextColumn<FDocument>() {
            @Override
            public String getValue(FilenetDocument object) {
                return object.getModifiedBy();
            }
        };
        TextColumn<FDocument> dateColumnD = new TextColumn<FDocument>(){
            @Override
            public String getValue(FDocumentobject){
                return object.getModifiedOn();
            } 
        };

 private void addValuesToTable(){
        List<FDocument> FDC = null;


        /*
         * Adding data to folder Table
         */
        ArrayList<FDocument> documentsArrayList = new ArrayList<FDocument>();
        Iterator<String> iteratorDocument = documents.getDocuments().getDocumentCollection().keySet().iterator();
        while(iteratorDocument.hasNext()){
            String key = iteratorDocument.next().toString();
            FDocument value = documents.getDocuments().getDocumentCollection().get(key);
            documentsArrayList.add(new FDocument(value.getName(), value.getSize(),value.getModifiedBy(), value.getModifiedOn(),value.getId()));

        }
        FDC = documentsArrayList;

    // Create a data provider.

        ListDataProvider<FDocument> dataProvider1 = new ListDataProvider<FDocument>();

    // Connect the table to the data provider.

        dataProvider1.addDataDisplay(documentTable);

    // Add the data to the data provider, which automatically pushes it to the widget.

        List<FDocument> listDocument = dataProvider1.getList();
            for (FDocument fDocument: FDC) {

                listDocument.add(fDocument1);
            }

This the way i have made my table and the value.

Plz any one tell me the procedure

  1. to add icon before the name of the document.

  2. and i also want one more Image column before name column


Solution

  • Use DefaultTableCellRenderer to set custom renderer against the column with the desired icon. As shown below, the renderer creates a new label to mask all the cells within the selected column.

    private class CellRenderer extends DefaultTableCellRenderer
    {
    
     public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
     {
        JLabel label = new JLabel((String)value);
        label.setOpaque(true);
        Icon icon = new ImageIcon("icon.png");
        label.setIcon(icon);
        return label;
     }
    }
    

    And then apply it to your table as shown below:

    table.getColumnModel().getColumn(0).setCellRenderer(new CellRenderer());
    

    Edited: If you're using GWT then see here for an example code for Grid Cell format.