Last Updated on 04 July 2019   |   Print Email
There would be a case in which we want to do something when the user clicks on a column header of a JTable. To do so, follow these simple steps:
Create a mouse event handler class that implements the MouseListener interface (or preferably extending its adapter class, i.e. the MouseAdapter class) and override the mouseClicked()method. For example:
public class TableHeaderMouseListener extends MouseAdapter {
public void mouseClicked(MouseEvent event) {
// do something when mouse clicked...
}
}
Obtain JTableHeader from the JTable:
JTableHeader header = table.getTableHeader();
Add the mouse event handler class as MouseListenerfor the table header:
To know which column header is clicked, we call the JTable’s method columnAtPoint(Point) inside the mouseClicked() method as follows:
Point point = event.getPoint();
int column = table.columnAtPoint(point);
The return value is index number of the column being clicked. So here’s a complete example code of the TableHeaderMouseListener class:
package net.codejava.swing.jtable;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JOptionPane;
import javax.swing.JTable;
/**
* A mouse listener class which is used to handle mouse clicking event
* on column headers of a JTable.
* @author www.codejava.net
*
*/
public class TableHeaderMouseListener extends MouseAdapter {
private JTable table;
public TableHeaderMouseListener(JTable table) {
this.table = table;
}
public void mouseClicked(MouseEvent event) {
Point point = event.getPoint();
int column = table.columnAtPoint(point);
JOptionPane.showMessageDialog(table, "Column header #" + column + " is clicked");
// do your real thing here...
}
}
And following is code of a Swing-based demo program that creates a simple JTable component which uses the TableHeaderMouseListener class above as its header’s mouse listener:
package net.codejava.swing.jtable;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.JTableHeader;
/**
* A Swing program demonstrates how to handle mouse clicking event
* on column headers of a JTable component.
* @author www.codejava.net
*
*/
public class JTableHeaderMouseClickDemo extends JFrame {
private JTable table;
public JTableHeaderMouseClickDemo() {
super("JTable Column Header Mouse Click Demo");
// constructs the table
String[] columnNames = new String[] {"Title", "Author", "Published Date"};
String[][] rowData = new String[][] {
{"Spring in Action", "Craig Walls", "June 29th 2011"},
{"Struts 2 in Action", "Donald Brown", "May 1st 2008"},
{"Hibernate Made Easy", "Cameron Wallace McKenzie", "April 25th 2008"},
};
table = new JTable(rowData, columnNames);
table.setAutoCreateRowSorter(true);
JTableHeader header = table.getTableHeader();
header.addMouseListener(new TableHeaderMouseListener(table));
add(new JScrollPane(table));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(640, 150);
setLocationRelativeTo(null);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new JTableHeaderMouseClickDemo().setVisible(true);
}
});
}
}
Output when running the above program:When clicking on a column header, a message dialog appears saying which column is clicked:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.
thanks. I'm better with c#, and java android, so this is a newbie question.
I wanted to sort the table depending on the column clicked but the header class is all by itself and can't see the main form with the table. so I don't know the java intended way, to send the click back to the form, to do the sort. I know it's simple, but drawing a blank
Comments
I'm better with c#, and java android, so this is a newbie question.
I wanted to sort the table depending on the column clicked but the header class is all by itself and can't see the main form with the table. so I don't know the java intended way, to send the click back to the form, to do the sort. I know it's simple, but drawing a blank