如何在 jtable 中为焦点单元格正确设置边框



我有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单元格的每一侧都有边框有任何想法吗?

谢谢!

  • prepareRenderer默认是第 Renderer 行,是关于突出显示整行

  • 您必须设置和测试行和列,两个坐标,当您要更改整个单个单元格时

最新更新