Volodymyr Levytskyi
Volodymyr Levytskyi

Reputation: 3432

How to set correctly border for focused cell in jtable?

I have jtable that has different three types of renderers. So, I don't see easy way to provide custom cell renderer. But I need custom border around cell which is currently selected. To achieve this I use prepareRenderer method. This works but has small bug - only top and bottom border is displayed but left and right borders are not displayed.

Please, copy,paste this to see the problem :

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();
    }
}

Do you have any ideas on how to have border on each side of cell of jtable?

Thank you!

Upvotes: 1

Views: 1978

Answers (1)

mKorbel
mKorbel

Reputation: 109813

  • prepareRenderer is by default line Renderer, is about hightlighting whole row

  • you have to set and test row and columns, both coordinates, when you want to change entire, single cell

Upvotes: 2

Related Questions