How to add an actionListener to a table column?

My program looks as follow:

import javax.swing.JFrame;
import javax.swing.table.DefaultTableModel;


/**
 * @ *
 * Folgendes auswählen, um die Schablone für den erstellten Typenkommentar zu ändern:
 * Fenster>Benutzervorgaben>Java>Codegenerierung>Code und Kommentare
 */
public class TestSwing extends JFrame {

      private javax.swing.JPanel jContentPane = null;

      private javax.swing.JScrollPane jScrollPane = null;
      private javax.swing.JTable jTable = null;
      private DefaultTableModel model = null;
      
      public static void main(String[] args) {
            new TestSwing().show();
      }
      /**
       * This is the default constructor
       */
      public TestSwing() {
            super();
            initialize();
      }
      /**
       * This method initializes this
       *
       * @return void
       */
      private void initialize() {
            this.setSize(300, 200);
            this.setContentPane(getJContentPane());
            this.setTitle("Test Swing");
      }
      /**
       * This method initializes jContentPane
       *
       * @return javax.swing.JPanel
       */
      private javax.swing.JPanel getJContentPane() {
            if (jContentPane == null) {
                  jContentPane = new javax.swing.JPanel();
                  jContentPane.setLayout(null);
                  jContentPane.add(getJScrollPane(), null);
            }
            return jContentPane;
      }
      /**
       * This method initializes jScrollPane
       *
       * @return javax.swing.JScrollPane
       */
      private javax.swing.JScrollPane getJScrollPane() {
            if(jScrollPane == null) {
                  jScrollPane = new javax.swing.JScrollPane();
                  jScrollPane.setViewportView(getJTable());
                  jScrollPane.setBounds(6, 5, 285, 97);
            }
            return jScrollPane;
      }
      /**
       * This method initializes jTable
       *
       * @return javax.swing.JTable
       */
      private javax.swing.JTable getJTable() {
            model = new DefaultTableModel();
            model.addColumn("click me");
            if(jTable == null) {
                  jTable = new javax.swing.JTable(model);
            }
            return jTable;
      }
}

I want to add an actionListener to the column tiltle "click me". When I click this title, it will do sonething.
chenweiAsked:
Who is Participating?
 
cpa199Commented:
You need to write a class that extends MouseAdapter() and in this override "public void mouseClicked(MouseEvent e)" with whatever you want to do on click and then assign this by :

JTableHeader th = table.getTableHeader();
th.addMouseListener(yourMouseAdapter);

This should do the trick, hope it helps.

Carl
0
 
cpa199Commented:
Oh, btw, just missed a simple bit above. Create yourMouseAdapter before you add it:

YourMouseAdapter yourMouseAdapter = new YourMouseAdapter();

:P Silly me
0
 
zzynxSoftware engineerCommented:
JTableHeader th = yourTable.getTableHeader();
th.addMouseListener( new MyMouseAdapter(this) );

with

public MyMouseAdapter extends MouseAdapter {

        public void mousePressed(MouseEvent e) {
              int iViewColumnPressed = yourTable.getColumnModel().getColumnIndexAtX(e.getX());
              int iModelColumnPressed = yourTable.convertColumnIndexToModel(iViewColumnPressed);
             // Do what you want to
        }

}
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
cpa199Commented:
That is what I just said, just in a very slightly different way, oh and you told him how to get the indecies of the cell :P
0
 
zzynxSoftware engineerCommented:
Sorry, cpa199. Didn't saw your comments when I posted. (forgot to refresh)

Correction: read

             th.addMouseListener( new MyMouseAdapter(this) );
as
             th.addMouseListener( new MyMouseAdapter() );
0
 
zzynxSoftware engineerCommented:
>> oh and you told him how to get the indecies of the cell
Copied that from my code to sort on header column clicks. (Then I have to determine which column is clicked on)
but indeed, in chenwei's situation, it's rather useless to determine the column clicked on.
It's just the one that has the "click me" header.
So, just ignore me ;°)
0
 
cpa199Commented:
No worries :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.