我需要向表标题(即JLabel)添加图标。
当文本在左侧对齐时,它工作正常。
但是,当文本居中时,不考虑以某种方式调用setIconTextGap()。这样的效果是,图标也位于标签的中心:
这通常无法正常工作,还是我错过了一些事情?
我正在使用Java 6(无法更改该事实)。
我在下面创建了SSCCE:
import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import java.awt.*;
public class Main {
public static void main(String[] args)
{
IconLabel label1 = new IconLabel("test text left", new TableArrowIcon(TableArrowIcon.TableArrowDirection.NORTH), SwingConstants.LEFT);
IconLabel label2 = new IconLabel("test text center", new TableArrowIcon(TableArrowIcon.TableArrowDirection.NORTH), SwingConstants.CENTER);
Object[][] data = {
{"Speed reading", 20, true },
{"Joe", "Brown", "Pool", 10, false }
};
String[] columnNames = { "test", "test" };
JPanel panel = new JPanel(new BorderLayout());
final JTable table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
label2.setHorizontalTextPosition(JLabel.CENTER);
TableColumn column1 = new TableColumn();
TableColumn column2 = new TableColumn();
column1.setHeaderRenderer(label1);
column2.setHeaderRenderer(label2);
table.addColumn(column1);
table.addColumn(column2);
JScrollPane scrollPane = new JScrollPane(table);
panel.add(scrollPane);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(1);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
public static class IconLabel extends JLabel implements TableCellRenderer {
private final Icon icon;
private final int alignment;
public IconLabel(String text, Icon icon, int alignment) {
super(text);
this.icon = icon;
setIcon(icon);
setPreferredSize(new Dimension(50, 20));
this.alignment = alignment;
}
@Override
public void setBounds(int x, int y, int width, int height) {
super.setBounds(x, y, width, height);
int textWidth = getFontMetrics(getFont()).stringWidth(getText());
Insets insets = getInsets();
int iconTextGap = width - textWidth - icon.getIconWidth() - insets.left - insets.right - 3;
setIconTextGap(iconTextGap);
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
setHorizontalTextPosition(alignment);
return this;
}
}
public static class TableArrowIcon implements Icon {
public enum TableArrowDirection {
NORTH() {
@Override
public void drawArrow(Graphics2D g, int controlSize, int x, int y) {
g.drawLine(x + 3, y + (controlSize - 10), x + 7, y + (controlSize - 14));
g.drawLine(x + 7, y + (controlSize - 14), x + (controlSize - 9), y + (controlSize - 10));
}
};
public abstract void drawArrow(Graphics2D g, int controlSize, int x, int y);
}
private final TableArrowDirection direction;
public TableArrowIcon(TableArrowDirection direction) {
this.direction = direction;
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.setColor(Color.darkGray);
int controlSize = 20;
((Graphics2D) g).setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
direction.drawArrow((Graphics2D) g, controlSize, x, y);
}
@Override
public int getIconWidth() {
return 14;
}
@Override
public int getIconHeight() {
return 14;
}
}
}
预先感谢任何指针。