How to render an image to a JTable cell

前端 未结 2 1756
無奈伤痛
無奈伤痛 2021-01-20 15:37

I want to apply a renderer on a cell of my JTable, to do so I created a class named myRenderer :

import java.awt.Component;

import javax.swing.ImageIcon;
im         


        
相关标签:
2条回答
  • 2021-01-20 16:17

    You can use getTableCellRendererComponent function in as described below. The icon, row and coulmn to display icon, you can set from out side using a setter methode

    import java.awt.Component;
    
    import javax.swing.JTable;
    import javax.swing.table.DefaultTableCellRenderer;
    
    
      public class MyRenderer extends DefaultTableCellRenderer {
    
    public MyRenderer() {
        // TODO Auto-generated constructor stub
    }
    
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
        super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
                row, column);
    
        int neededRow=0; // set the needed row here in which the icon to be dispayed
        int neededcolumn=0; // set the needed column here in which the icon to be dispayed
    
        if(row==neededRow && column==neededcolumn)
        {
            setIcon(icon);
        }
    
        return this
    }
    }
    
    0 讨论(0)
  • 2021-01-20 16:32

    Besides the fact that you are not even properly overriding the getTableCellRendererComponent method, you don't even need a custom renderer to display an image in a column

    From How to Use Tables. Here's a list of types with default pre-configured renderers

    • Boolean — rendered with a check box.
    • Number — rendered by a right-aligned label.
    • Double, Float — same as Number, but the object-to-text translation is performed by a NumberFormat instance (using the default number format for the current locale).
    • Date — rendered by a label, with the object-to-text translation performed by a DateFormat instance (using a short style for the date and time).
    • ImageIcon, Icon — rendered by a centered label.
    • Object — rendered by a label that displays the object's string value.

    So you can put add an ImageIcon to the table and it will be rendered as such, given you properly override the getColumnClass()

    Also from tutorial:

    To choose the renderer that displays the cells in a column, a table first determines whether you specified a renderer for that particular column. If you did not, then the table invokes the table model's getColumnClass method, which gets the data type of the column's cells. Next, the table compares the column's data type with a list of data types for which cell renderers are registered

    So say you have a DefaultTableModel with three columns and you want an ImageIcon in the last column. You would do something like this

    DefaultTableModel model = new DefaultTableModel(...) {
        @Override
        public Class<?> getColumnClass(int column) {
            switch (column) {
                case 2: return ImageIcon.class;
                default: return String.class
            }
        }
    };
    JTable table = new JTable(model);
    

    Then by just adding an ImageIcon to the third column, it will be rendered as such

    String colOneDate = "Data";
    String colTwoData = "Data";
    ImageIcon colThreeIcon = new ImageIcon(...);
    model.addRow(new Object[] { colOneData, colTwoData, colThreeIcon });
    

    You will probably also want to set the column widths and height accordingly, to the size of the image. For that you can see any of these questions

    0 讨论(0)
提交回复
热议问题