Here is a modification of camickr code to make buttons clickable in the jtable cells. The question is how to modify it to make them clickable in the table headers. It looks like there is quite a different behavior in cells and headers (naturally it probably should be this way). More specifically, I guess my question is how do you modify MyColHeaderRenderer class in the code below. Btw, this is also an example I've mentioned in one of the previous posts when a component responds to a press mouse event but not click event when first pressed.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
/**
*
* @author
*/
public class Example4a extends JFrame {
MyCell[][] data = {{new MyCell("A1"), new MyCell("A2")}};
MyColHeader[] headers = {new MyColHeader("col 1"), new MyColHeader("col 2")};
/** Creates a new instance of Example5 */
public Example4a() {
setTable();
setSize(200, 200);
setVisible(true);
super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void setTable() {
DefaultTableModel model = new DefaultTableModel(data, headers){
public Class getColumnClass(int column) {
return MyCell.class;
}
};
MyTable table = new MyTable(model);
JScrollPane scrollpane = new JScrollPane(table);
JPanel top_panel = new JPanel();
top_panel.setLayout(new BorderLayout());
getContentPane().add(top_panel);
top_panel.add(scrollpane, BorderLayout.CENTER);
}
public static void main(String[] args) {
Example4a ex = new Example4a();
}
class MyTable extends JTable {
public MyTable(TableModel model) {
super(model);
MyCellRenderer cell_renderer = new MyCellRenderer();
MyColHeaderRenderer header_renderer = new MyColHeaderRenderer();
TableColumnModel columnModel = getColumnModel();
for(int i=0; i<columnModel.getColumnCount(); i++) {
TableColumn column = columnModel.getColumn(i);
column.setCellRenderer(cell_renderer);
column.setCellEditor(cell_renderer);
column.setHeaderRenderer(header_renderer);
}
this.setRowHeight(50);
}
}
class MyCellRenderer extends AbstractCellEditor
implements TableCellRenderer, TableCellEditor {
MyCell editCell;
public MyCellRenderer() {
}
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row, int column) {
return (MyCell)value;
}
public Component getTableCellEditorComponent(
JTable table, Object value, boolean isSelected, int row, int column) {
editCell = (MyCell)value;
return editCell;
}
public Object getCellEditorValue() {
return editCell;
}
}
class MyCell extends JPanel {
String text;
JButton button;
public MyCell(String text_) {
text = text_;
setLayout(new GridBagLayout());
button = new JButton(text);
button.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
System.out.println("cell button is clicked");
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
System.out.println("cell button is pressed");
}
public void mouseReleased(MouseEvent e) {
}
});
add(button, new GridBagConstraints(0, 0, 1, 1, 0, 100.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(10, 5, 10, 5), 20, 10));
}
}
class MyColHeader extends JPanel {
String text;
JButton button;
public MyColHeader(String text_) {
text = text_;
setLayout(new GridBagLayout());
setBorder(BorderFactory.createEtchedBorder());
button = new JButton(text);
button.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
System.out.println("column header button is clicked");
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
System.out.println("column header button is pressed");
}
public void mouseReleased(MouseEvent e) {
}
});
add(button, new GridBagConstraints(0, 0, 1, 1, 0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(3, 0, 3, 0), 5, 0));
}
}
class MyColHeaderRenderer implements TableCellRenderer {
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row, int column) {
return headers[column];
}
}
}