I've been having a devil of a time trying to get the columns of my 
JTable to size correctly.  I have a very simple table inside  of a 
JScrollPane.  The rows consist of a 16x16 icon in column 0 and a 
String in column 1 (basically a status icon for a hostname).

I have specified a preferred width of 20 for column 0 and for a while 
this was sizing correctly.  However,  after adding  the code in the 
TableModel that returns the ImageIcon object for column 0 cells, now 
the table insists on  allocating equal space to the two columns.  I 
have verified that the width of the ImageIcons being returned is 16 
pixels.  Why is the table allocating so much space to the column?

Here is the relevant code.

     private HostTableModel tableModel = new HostTableModel();

     public HostList()
     {
         TableColumn column;

         setModel(tableModel);

         column = getColumnModel().getColumn(0);
         column.setPreferredWidth(20);
         column.setMinWidth(20);
         column.setMaxWidth(20);
         column.setResizable(false);

         column = getColumnModel().getColumn(1);
         column.setResizable(false);

         JTableHeader header = getTableHeader();
         header.setResizingAllowed(false);
         header.setReorderingAllowed(false);

         setAutoResizeMode(AUTO_RESIZE_LAST_COLUMN);
     }

     private class HostTableModel extends AbstractTableModel
     {

         public Class getColumnClass(int col)
         {
             if (col == 0)
                 return ImageIcon.class;
             else
                 return super.getColumnClass(col);
         }

         public int getColumnCount()
         {
             return columnNames.length;
         }

         public int getRowCount()
         {
             return hosts.size();
         }

         public String getColumnName(int col)
         {
             return columnNames[col];
         }

         public Object getValueAt(int row, int col)
         {
             RemoteHost remoteHost = (RemoteHost)hosts.elementAt(row);
             if (col == 0)
             {
                 return getHostStatusIcon(remoteHost);
             }
             else
             {
                 return remoteHost;
             }
         }
}

Thanks.

-- 
Bill Tschumy
Otherwise -- Austin, TX
[EMAIL PROTECTED]
_______________________________________________
Advanced-swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/advanced-swing

Reply via email to