如何在jtable中的图像旁边添加文本?

How to add text next to image in jtable?

我有一个三列的 jtable,希望前两列旁边有一个图像图标和一个字符串。

目前我正在显示像

这样的 ImageIcons
 DefaultTableModel model = new DefaultTableModel(rows, columns) {
                            @Override
                            public Class<?> getColumnClass(int column) {
                                switch (column) {
                                    case 0:
                                    case 1:
                                        return ImageIcon.class;
                                    case 2:
                                        return String.class;
                                    default:
                                        return Object.class;
                                }
                            }
                        };

jTable.setModel(model);

我确实找到了这个,但不知道 ... 里有什么,也不知道如何用这个在 jtable 中设置 ImageIcon 和字符串:

Java - Is it possible to put an image and a String in the same JTable cell?

任何帮助将不胜感激。

I did find this but don't know what goes in ... or how to set the ImageIcon and string in the jtable with this:

您需要创建自定义对象(假设您将其称为 MyCustomObject)来保存要显示的信息。因此您的自定义对象将具有两个属性:图标和文本。

然后您创建对象并添加到 TableModel,就像您对 table 中的任何其他对象所做的那样。

对于前两列,您需要将 getColumnClass() 方法覆盖为 return MyCustomObject。然后您还需要为 MyCustomObject.

设置自定义渲染器

那么在渲染代码中你会做这样的事情:

MyCustomObject data = (MyCustomObject)value;
setIcon(data.getIcon());
setText(data.getText());

因为DefaultTableCellRenderer is JLabel, you can use it's text alignment properties in a custom renderer to label the icon. The example below overrides getRowHeight() to ensure visibility. I've updated the example to use a custom class, as suggested LabelIcon 的实例为每一行保存一个图标和标签对。

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;

/**
 * @see 
 */
public class TableExample {

    private JFrame frame = new JFrame("Table Demo");
    private Icon errorIcon = (Icon) UIManager.getIcon("OptionPane.errorIcon");
    private Icon infoIcon = (Icon) UIManager.getIcon("OptionPane.informationIcon");
    private Icon warnIcon = (Icon) UIManager.getIcon("OptionPane.warningIcon");
    private String[] columnNames = {"String", "Icon"};
    private Object[][] data = {
        {"One", new LabelIcon(errorIcon, "Error")},
        {"Two", new LabelIcon(infoIcon, "Information")},
        {"Three", new LabelIcon(warnIcon, "Warning")},
        {"Four", new LabelIcon(errorIcon, "Error")},
        {"Five", new LabelIcon(infoIcon, "Information")},
        {"Six", new LabelIcon(warnIcon, "Warning")}};
    private final TableModel model = new DefaultTableModel(data, columnNames) {

        @Override
        public Class<?> getColumnClass(int column) {
            switch (column) {
                case 0:
                    return String.class;
                case 1:
                    return LabelIcon.class;
                default:
                    return String.class;
            }
        }
    };

    private static class LabelIcon {

        Icon icon;
        String label;

        public LabelIcon(Icon icon, String label) {
            this.icon = icon;
            this.label = label;
        }
    }

    private static class LabelIconRenderer extends DefaultTableCellRenderer {

        public LabelIconRenderer() {
            setHorizontalTextPosition(JLabel.CENTER);
            setVerticalTextPosition(JLabel.BOTTOM);
        }

        @Override
        public Component getTableCellRendererComponent(JTable table, Object
            value, boolean isSelected, boolean hasFocus, int row, int col) {
            JLabel r = (JLabel) super.getTableCellRendererComponent(
                table, value, isSelected, hasFocus, row, col);
            setIcon(((LabelIcon) value).icon);
            setText(((LabelIcon) value).label);
            return r;
        }
    }

    public TableExample() {
        JTable table = new JTable(model) {
            @Override
            public int getRowHeight() {
                return super.getRowHeight() + infoIcon.getIconHeight();
            }

            @Override
            public Dimension getPreferredScrollableViewportSize() {
                return new Dimension(
                    (5 * super.getPreferredSize().width) / 4,
                    4 * this.getRowHeight());
            }
        };
        table.setAutoCreateRowSorter(true);
        table.setDefaultRenderer(LabelIcon.class, new LabelIconRenderer());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new JScrollPane(table));
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            TableExample tableExample = new TableExample();
        });
    }
}