我有jtable有不同的三种类型的渲染器。所以,我没有看到提供自定义单元格渲染器的简单方法。但我需要在当前选中的单元格周围自定义边框。为此,我使用prepareRenderer方法。这有效,但有一个小错误 - 只显示顶部和底部边框,但不显示左右边框。
请复制,粘贴以查看问题:
public class CellBorderDemo extends JFrame
{
private JTable dataSearchResultTable;
public CellBorderDemo()
{
JPanel panel = new JPanel(new GridLayout(2, 1, 5, 10));
panel.setPreferredSize(new Dimension(500, 300));
dataSearchResultTable = new JTable(new MyTableModel())
{
private EmptyBorder emptyBorder = new EmptyBorder(0, 1, 0, 1);
private Border redBorder = new CompoundBorder(new MatteBorder(1, 0, 1, 0, Color.RED), emptyBorder);
private Border unselectedBorder = super.getBorder();
@Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int column)
{
Object value = getValueAt(row, column);
boolean isSelected = false;
boolean hasFocus = false;
// Only indicate the selection and focused cell if not printing
if (!isPaintingForPrint()) {
isSelected = isCellSelected(row, column);
boolean rowIsLead = (selectionModel.getLeadSelectionIndex() == row);
boolean colIsLead = (columnModel.getSelectionModel().getLeadSelectionIndex() == column);
hasFocus = (rowIsLead && colIsLead) && isFocusOwner();
}
JComponent cellRenderer = (JComponent) renderer.getTableCellRendererComponent(this, value, isSelected,
hasFocus, row, column);
if (isSelected && hasFocus) {
cellRenderer.setBorder(redBorder);
} else {
cellRenderer.setBorder(unselectedBorder);
}
return cellRenderer;
}
};
dataSearchResultTable.setSelectionBackground(new Color(0xccccff));
dataSearchResultTable.setFillsViewportHeight(true);
dataSearchResultTable.setRowSelectionAllowed(true);
dataSearchResultTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
dataSearchResultTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
dataSearchResultTable.setRowHeight(25);
dataSearchResultTable.getColumnModel().setColumnMargin(0);
dataSearchResultTable.setShowGrid(true);
// dataSearchResultTable.setIntercellSpacing(new Dimension(0, 0));
dataSearchResultTable.setCellSelectionEnabled(true);
panel.add(new JScrollPane(dataSearchResultTable));
super.getContentPane().add(panel);
super.pack();
super.setDefaultCloseOperation(EXIT_ON_CLOSE);
super.setVisible(true);
}
class MyTableModel extends AbstractTableModel
{
private String[] columnNames = { "First Name", "Last name", "Vegetarian" };
private Object[][] data;
MyTableModel()
{
data = new Object[][] { { "Vova", "KipokKipokKipokKipok", false }, { "Olia", "Duo", true },
{ "Ivan", "Brown", false } };
}
public int getColumnCount()
{
return columnNames.length;
}
public int getRowCount()
{
return data.length;
}
public String getColumnName(int col)
{
return columnNames[col];
}
public Object getValueAt(int row, int col)
{
if (data.length > 0 && data[0] != null) {
return data[row][col];
}
return null;
}
public Class getColumnClass(int c)
{
Object valueAt = getValueAt(0, c);
return valueAt == null ? Object.class : valueAt.getClass();
}
public boolean isCellEditable(int row, int col)
{
return true;
}
public void setValueAt(Object value, int row, int col)
{
if (data.length > 0 && data[0] != null) {
data[row][col] = value;
fireTableCellUpdated(row, col);
}
}
}
public static void main(String[] args) throws ParseException
{
new CellBorderDemo();
}
}
您对如何在jtable单元格的每一边都有边框有什么想法吗?
谢谢!
答案 0 :(得分:2)
prepareRenderer
默认为行Renderer
,是关于高亮整行
当您想要更改整个单个单元格时,您必须设置和测试行和列,两个坐标