我有一个三列jtable,并希望前两列有一个Imageicon和一个紧挨着它的字符串。
目前我正在显示像
这样的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?
任何帮助将不胜感激。
答案 0 :(得分:1)
我确实找到了这个,但不知道是什么...或者如何在jtable中设置ImageIcon和字符串:
您需要创建自定义对象(假设您将其称为MyCustomObject)来保存您要显示的信息。因此,您的自定义对象将具有两个属性:图标和文本。
然后创建Object并添加到TableModel,就像对表中的任何其他Object一样。
您需要覆盖getColumnClass()方法以返回前两列的MyCustomObject
。然后,您还需要为MyCustomObject
设置自定义渲染器。
那么在渲染代码中你会做类似的事情:
MyCustomObject data = (MyCustomObject)value;
setIcon(data.getIcon());
setText(data.getText());
答案 1 :(得分:1)
由于DefaultTableCellRenderer
为JLabel
,您可以在自定义renderer中使用它的文字对齐属性来标记图标。以下示例将覆盖getRowHeight()
以确保可见性。我updated示例使用自定义类,如建议的here; LabelIcon
的实例为每行保存一个图标和标签对。
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
/**
* @see https://stackoverflow.com/a/36830558/230513
*/
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();
});
}
}