Adding Action to the Button which is in JTableHeader

Hi, I can able to add a button to the JTableHeader. But i would like to add Action to that Button. When i add Action Listener no action is happening. So Could any one suggest how to solve this problem.

In the below code, You can find i am using one Parser java file. From that Java file i am getting data i need to display in table body.

Here i pasted my complete code:

package src;

import java.awt.Component;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.ArrayList;

import java.util.Iterator;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JScrollPane;

import javax.swing.JTable;

import javax.swing.table.AbstractTableModel;

import javax.swing.table.DefaultTableCellRenderer;

import javax.swing.table.TableCellRenderer;

import javax.swing.table.TableColumn;

public class TestTable extends JFrame implements ActionListener

{

public TestTable(){

JTable table = new JTable(this.new ParticipantDataModel());

MyButton select = new MyButton(table,"S_ALL");

table.getColumnModel().getColumn(0).setHeaderRenderer(new MyButton(table,"Select All"));

TableColumn tColumn4 = table.getColumnModel().getColumn(3);

DefaultTableCellRenderer defaultRender = new DefaultTableCellRenderer();

defaultRender.setToolTipText("<html><body>Tool Tip Testing................

Tool Tip Testing................

Tool Tip Testing................</body></html>");

tColumn4.setCellRenderer(defaultRender);

JScrollPane scrollPane = new JScrollPane(table);

add(scrollPane);

setVisible(true);

}

public static void main(String[] args) {

TestTable test = new TestTable();

}

public Object[][] getParticipantData(){

Object[][] data = null;

Parser parse = new Parser();

ArrayList rules = (ArrayList)parse.getRuleInfos("test.xml");

data = new Object[rules.size()][];

int i = 0;

for (Iterator iter = rules.iterator(); iter.hasNext();i++) {

RuleInfo element = (RuleInfo) iter.next();

data = new Object[4];

data[0] = new Boolean(false);

data[1] = element.getRuleName();

data[2] = element.getRuleAlias();

data[3] = element.getRuleDesc();

}

return data;

}

class ParticipantDataModel extends AbstractTableModel{

public Class getColumnClass(int col) {

return getValueAt(0, col).getClass();

}

public String getColumnName(int col) {

return (String) colNames[col];

}

public boolean isCellEditable(int row, int col) {

return true;

}

Object[] colNames = {"Select All","Role Name", "Role Alias", "Description" };

Object[][] colValues = getParticipantData();

public int getColumnCount() {

return 4;

}

public int getRowCount() {

return colValues.length;

}

public Object getValueAt(int row, int col) {

return colValues[row][col];

}

public void setValueAt(Object colValue, int row, int col) {

colValues[row][col] = colValue;

fireTableCellUpdated(row, col);

}

}

public void actionPerformed(ActionEvent e) {

System.out.println(e.getSource());

}

private static class MyButton extends JButton implements TableCellRenderer,ActionListener{

JTable table = null;

public MyButton(){

super();

addActionListener(this);

}

public MyButton(JTable table,String value){

setText(value);

addActionListener(this);

this.table = table;

}

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) {

return this;

}

public void actionPerformed(ActionEvent arg0) {

System.out.println(arg0);

}

}

}

Thanks

Mohan

Message was edited by:

mohangupta_chegu

Message was edited by:

mohangupta_chegu

[4139 byte] By [mohangupta_chegua] at [2007-11-27 10:46:57]
# 1

The below code will make the table header respond to mouse events....

table.getTableHeader().addMouseListener(new MouseAdapter(){

public void mousePressed(MouseEvent e){

int column = table.getColumnModel().getColumnIndexAtX(e.getX());

if (column == 0){ //check if it is column zero....

//do your stuff here....

}

}

});

imran_ea at 2007-7-28 20:21:51 > top of Java-index,Desktop,Core GUI APIs...
# 2

Thanks Very Much Imran

-Mohan

mohangupta_chegua at 2007-7-28 20:21:51 > top of Java-index,Desktop,Core GUI APIs...
# 3

Your welcome Mohan. And if it did work for you, don't forget to award the duke stars !

imran_ea at 2007-7-28 20:21:51 > top of Java-index,Desktop,Core GUI APIs...