title: How to select a JTable column on selection of header in Java
body: <p>I found a code to select a column on click of JTable header by applying a mouse listener on the header. But unfortunately, on click of a table cell whole JTable column is selected and the selection strategy is not reversed as before. Instead it selects the whole column on selection of a cell.</p>
  
  <p><img src="https://i.stack.imgur.com/lRIUT.jpg" alt="enter image description here"></p>
  
  <p>Code:-</p>
  
  <pre><code>import java.awt.Component;
  import java.awt.Cursor;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.awt.event.MouseAdapter;
  import java.awt.event.MouseEvent;
  
  import javax.swing.JFrame;
  import javax.swing.JMenu;
  import javax.swing.JMenuBar;
  import javax.swing.JMenuItem;
  import javax.swing.JScrollPane;
  import javax.swing.JTable;
  import javax.swing.ListSelectionModel;
  import javax.swing.SwingUtilities;
  import javax.swing.UIManager;
  import javax.swing.UnsupportedLookAndFeelException;
  import javax.swing.table.DefaultTableModel;
  import javax.swing.table.JTableHeader;
  
  public class HeaderLocation {  
      private JTable getTable() {  
          int rows = 32, cols = 4;  
          String[] colIds = { "column 1", "column 2", "column 3", "column 4" };  
          Object[][] data = new Object[rows][cols];  
          for(int row = 0; row &lt; rows; row++) {  
              for(int col = 0; col &lt; cols; col++) {  
                  data[row][col] = "item " + (row*cols+col+1);  
              }  
          }  
          DefaultTableModel model = new DefaultTableModel(data, colIds);  
          final JTable table = new JTable(model);  
          final JTableHeader header = table.getTableHeader();  
          table.setCellEditor(new CustomCellEditor());
          header.setReorderingAllowed(false);  
          header.addMouseListener(new MouseAdapter() {  
              public void mouseClicked(MouseEvent e) {  
                  int col = header.columnAtPoint(e.getPoint());  
                  System.out.printf("click cursor = %d%n",  
                                     header.getCursor().getType());  
                  if(header.getCursor().getType() == Cursor.E_RESIZE_CURSOR)  
                      e.consume();  
                  else {  
                      //System.out.printf("sorting column %d%n", col); 
                      table.setColumnSelectionAllowed(true);
                      table.setRowSelectionAllowed(false);
                      table.clearSelection();
                      table.setColumnSelectionInterval(col,col);
                      //tableModel[selectedTab].sortArrayList(col);  
                  }  
              }  
          });  
  
  
          return table;  
      }  
  
      private JMenuBar getMenuBar() {  
          final JMenu view = new JMenu("view");  
          ActionListener l = new ActionListener() {  
              public void actionPerformed(ActionEvent e) {  
                  JMenuItem item = (JMenuItem)e.getSource();  
                  String className = item.getActionCommand();  
                  changePLAF(className, view.getTopLevelAncestor());  
              }  
          };  
          UIManager.LookAndFeelInfo[] info = UIManager.getInstalledLookAndFeels();  
          for(int j = 0; j &lt; info.length; j++) {  
              JMenuItem item = new JMenuItem(info[j].getName());  
              item.setActionCommand(info[j].getClassName());  
              item.addActionListener(l);  
              view.add(item);  
          }  
          JMenuBar menuBar = new JMenuBar();  
          menuBar.add(view);  
          return menuBar;  
      }  
  
      private void changePLAF(String className, Component c) {  
          try {  
              UIManager.setLookAndFeel(className);  
          } catch(ClassNotFoundException cnfe) {  
              System.err.println("class not found: " + cnfe.getMessage());  
          } catch(InstantiationException ie) {  
              System.err.println("instantiation: " + ie.getMessage());  
          } catch(IllegalAccessException iae) {  
              System.err.println("illegal access: " + iae.getMessage());  
          } catch(UnsupportedLookAndFeelException ulafe) {  
              System.err.println("unsupported laf: " + ulafe.getMessage());  
          }  
          SwingUtilities.updateComponentTreeUI(c);  
      }  
  
      public static void main(String[] args) {  
          HeaderLocation test = new HeaderLocation();  
          JFrame f = new JFrame();  
          f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
          f.setJMenuBar(test.getMenuBar());  
          f.getContentPane().add(new JScrollPane(test.getTable()));  
          f.pack();  
          f.setLocation(200,200);  
          f.setVisible(true);  
      }  
  }
  </code></pre>
  
  <p>CustomCellEditor.java</p>
  
  <pre><code>import java.awt.Component;
  
  import javax.swing.DefaultCellEditor;
  import javax.swing.JTable;
  import javax.swing.JTextField;
  
  public class CustomCellEditor extends DefaultCellEditor {
  
      public CustomCellEditor() {
          super(new JTextField());
          // TODO Auto-generated constructor stub
      }
  
      public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
          table.clearSelection();
          table.setColumnSelectionAllowed(false);
          table.setRowSelectionAllowed(true);
          return super.getTableCellEditorComponent(table, value, isSelected, row, column);
          }
  
  }  
  </code></pre>
  
  <p>From the above code:-</p>
  
  <p>1.) On click of header column is properly selected.
  2.) But on click of a cell whole column is selected not only one cell as expected. I tried to invert Column selection but it's not working as expected.</p>
  
  <p>Any help related with this would be greatly appreciated.</p>
  
  <p>Thanks</p>