Search code examples
javaswingjtabletablecellrendererlistselectionlistener

Change row color of Jtable row when user clicks


Below is the code i used to generate table data using JTable

public class Scroller extends JFrame {

    public Scroller() throws HeadlessException {

            String columnNames[] = { "Location", "Lived In People" };

            // Create some data
            String dataValues[][] =
            {
                { "100 Hamilton", "" },
                { "", "Balan" },
                { "", "Kris" },
                { "Parkwood place", "" },
                { "", "Kris" }
            };

        JTable table = new JTable(dataValues, columnNames);
        table.setPreferredSize(new Dimension(250, 600));

        final JScrollPane scroll = new JScrollPane(table);
        scroll.getVerticalScrollBar().setUnitIncrement(50);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());
        add(scroll, BorderLayout.CENTER);
        setSize(300, 300);
        setVisible(true);

    }

    public static void main(final String[] args) throws Exception {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                new Scroller().setVisible(true);
            }
        });
    }

}

The output looks like below

enter image description here

Now, i can able to click on each row. But when i double click any cell, it lets me edit the data in the cell. I found i can use table.setEnabled(false) to make this not to happen.

But i am trying to make the table so that, each row can be selected but the cells should not be editable. is there any straight forward method to achieve this?

NOTE : I tried to override the function isCellEditable as specified in this post. But when running the screen shows only empty table.


Solution

  • You are using a TableModel, which implements isCellEditable by always returning true. Your desired behaviour can be achieved by writing a Table Model that always returns false. Example:

    public class MyTableModel extends DefaultTableModel {
    
     public MyTableModel(Vector data, Vector columnNames) {
        setDataVector(data, columnNames);
     }
    
     @Override
     public boolean isCellEditable(int row, int column) {
             return false;
         }
    }
    

    and add this line to your code:

    table.setModel(new MyTableModel(data, columnNames));
    

    Alternative

    If you wanna stick to the double array, try:

    JTable table = new JTable(new AbstractTableModel() {
                public String getColumnName(int column) { return columnNames[column].toString(); }
                public int getRowCount() { return dataValues.length; }
                public int getColumnCount() { return columnNames.length; }
                public Object getValueAt(int row, int col) { return dataValues[row][col]; }
                public boolean isCellEditable(int row, int column) { return false; }
            });
    

    For that to work, you will need to make your variables columnNames and dataValues final.