How to get Icon from JTable

后端 未结 4 1986
既然无缘
既然无缘 2020-11-30 15:38

I have changed the cell render in JTable to show image instead of text using the following code:

base_table.getColumnModel().getColumn(3).setCel         


        
4条回答
  •  眼角桃花
    2020-11-30 16:02

    below is the correct image renderer class.

    class SimpleCellRenderer extends DefaultTableCellRenderer{
    
    
    
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) 
    
        {
             Component cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row,
                   column);
          ((JLabel)cell).setIcon((Icon)value);
          ((JLabel)cell).setText("");
          ((JLabel)cell).setHorizontalAlignment(JLabel.CENTER);
    
          if (isSelected) {
             cell.setBackground(Color.white);
          } else {
             cell.setBackground(null);
          }
          //((AbstractTableModel)table.getModel()).fireTableCellUpdated(row,column);
    
          return cell;
       }
    
    
        }
    

    below is the method from where everything gets filled automtically. private void formWindowOpened(java.awt.event.WindowEvent evt)

    {                                  
            // TODO add your handling code here:
    
            fillIcon();  
        } 
    
       public void fillIcon() {
            int i,j,rowValue,colValue;
             int cols= student.getColumnCount();
             int rows=student.getRowCount();
    
             for(i =0 ;i

提交回复
热议问题