Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to add an actionListener to a table column?

Posted on 2004-10-25
7
Medium Priority
?
313 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
Comment
Question by:chenwei
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 1

Accepted Solution

by:
cpa199 earned 120 total points
ID: 12399493
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
ID: 12399499
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
ID: 12399537
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:cpa199
ID: 12399564
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
ID: 12399566
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
ID: 12399599
>> 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
ID: 12399675
No worries :)
0

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

618 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