6

Is it possible to add an ActionListener to a column header for JTable.

Here is my tableMy table image

Now, I want to add an ActionListener to the column headers (e.g. WQE, SDM) I would like to be able to show the column description in another window.

Nathan
  • 8,093
  • 8
  • 50
  • 76
Parag
  • 7,746
  • 9
  • 24
  • 29

2 Answers2

21

See fully working example below

  • add a MouseListener to the column header
  • use table.columnAtPoint() to find out which column header was clicked

Code:

// example table with 2 cols
JFrame frame = new JFrame();
final JTable table = new JTable(new DefaultTableModel(new String[] {
        "foo", "bar" }, 2));
frame.getContentPane().setLayout(
        new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
frame.getContentPane().add(table.getTableHeader());
frame.getContentPane().add(table);
frame.pack();
frame.setVisible(true);

// listener
table.getTableHeader().addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent e) {
        int col = table.columnAtPoint(e.getPoint());
        String name = table.getColumnName(col);
        System.out.println("Column index selected " + col + " " + name);
    }
});
Adam
  • 35,919
  • 9
  • 100
  • 137
  • 1
    +1 This is the approach suggested in the tutorial and illustrated [here](http://stackoverflow.com/a/7137801/230513). – trashgod Aug 02 '12 at 18:42
  • great! i never realized that the event mouseclicked on header is better than the events on table.:D – gumuruh Oct 09 '17 at 16:31
1

Yes it is Possible. You can add Mouse Event Both on the Column headers and cells Like this:

private class MyMouseAdapter extends MouseAdapter {

    public void mousePressed(MouseEvent e) {

        if (table.equals(e.getSource())) {

            int colIdx = table.columnAtPoint(e.getPoint());
            int rowIdx = table.rowAtPoint(e.getPoint());
Object obj = table.getModel().getValueAt(rowIdx, colIdx) ;//This gets the value in the cells
           String str = obj.toString();//This converts that Value to String
           JTextField somefield = new JTextField();//Choose a JTextField
           somefield.setText(str);//Populates the Clicked value to the JTextField

            System.out.println("Row: " + rowIdx + " " + "Colulmn: " + colIdx);
        }
        else if (header.equals(e.getSource())) {

            int selectedColumnIdx = header.columnAtPoint(e.getPoint());
            String colName = table.getColumnName(header.columnAtPoint(e.getPoint()));

            System.out.println("Column Name: " + colName);
            System.out.println("Selected Column: " + selectedColumnIdx);
        }
    }
}

Fix the Sample Code to Suit Your Taste and Preference;

Stanley Mungai
  • 4,044
  • 30
  • 100
  • 168
  • Better to use a [`ListSelectionListener`](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#selection) on the table itself. The header listener repeats @Adam's much earlier [answer](http://stackoverflow.com/a/9992631/230513). – trashgod Aug 02 '12 at 10:46