在JTable的一个单元格中能否显示两种颜色或字体的字符串?
例如:"ABCDEFG",把中间的"D"在单元格中显示时变为粗体或改变颜色使用HTML标签可以做到,例如:
<html>ABC<font color='red'>D</font>EFG</html>但是选中此单元格复制的时候会连标签一起复制,有没有方法在复制时不复制标签,或者不用HTML也能实现以上功能?
例如:"ABCDEFG",把中间的"D"在单元格中显示时变为粗体或改变颜色使用HTML标签可以做到,例如:
<html>ABC<font color='red'>D</font>EFG</html>但是选中此单元格复制的时候会连标签一起复制,有没有方法在复制时不复制标签,或者不用HTML也能实现以上功能?
import javax.swing.table.*;
import java.awt.Component;public class ColorTableRenderer extends DefaultTableCellRenderer {
private int PSP_UP;
public ColorTableRenderer() {
this.PSP_UP = 7;
this.setHorizontalAlignment(SwingConstants.CENTER); //居中
} public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
String peptide = String.valueOf(value);
StringBuffer sb = new StringBuffer("<html>");
for (int i = 0; i < peptide.length(); i++) {
if (i == PSP_UP) {
sb.append("<font color='blue'>" + peptide.charAt(i) + "</font>");
} else {
sb.append(peptide.charAt(i));
}
}
sb.append("</html>");
JTable tablecell = new JTable(1, 1);
tablecell.setValueAt(sb.toString(), 0, 0);
if (isSelected) {
tablecell.setBackground(table.getSelectionBackground());
tablecell.setGridColor(table.getSelectionBackground());
}else{
tablecell.setBackground(table.getBackground());
tablecell.setGridColor(table.getBackground());
}
tablecell.setRowHeight(table.getRowHeight());
return tablecell;
}
}