Solved

How to add an actionListener to a table column?

Posted on 2004-10-25
306 Views
Last Modified: 2009-07-29
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.
0
Question by:chenwei
    7 Comments
     
    LVL 1

    Accepted Solution

    by:
    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
     
    LVL 1

    Expert Comment

    by:cpa199
    Oh, btw, just missed a simple bit above. Create yourMouseAdapter before you add it:

    YourMouseAdapter yourMouseAdapter = new YourMouseAdapter();

    :P Silly me
    0
     
    LVL 37

    Expert Comment

    by:zzynx
    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
     
    LVL 1

    Expert Comment

    by:cpa199
    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
     
    LVL 37

    Expert Comment

    by:zzynx
    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
     
    LVL 37

    Expert Comment

    by:zzynx
    >> 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
     
    LVL 1

    Expert Comment

    by:cpa199
    No worries :)
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Suggested Solutions

    This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
    Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
    Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

    884 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now