Problem in JTable row selection
Hi All,
I'm using JTable in my application. It has 4 columns and rows are fetched from database. 1st column contains JCheckBox to select the particular row in the table. 1st column header also contains JCheckBox. All rows in the table are selected and unselected by check and uncheck the column header JCheckBox.
The problem is,
If I check anyone of the row JCheckBox then the particular row got selected and If I check the column header JCheckBox then that check box is checked and all rows are selected.
But If I check the 1st row JCheckBox then the 1st row got selected and If I check the Column header JCheckBox then column header JCheckBox is chekced but no rows are selected (1st row also deselected).
I tested the same scenario for 2nd and 3rd rows but everything works fine. Only 1st row gives this problem.
My Code is,
Code:
// Creates the JCheckBox in the table header
TableColumn tc = table.getColumnModel().getColumn(0);
tc.setHeaderRenderer(new APUCheckBoxHeader(new MyItemListener()));
APUCheckBoxHeader class :
Code:
public class APUCheckBoxHeader extends JCheckBox implements TableCellRenderer, MouseListener {
/**
*
*/
private static final long serialVersionUID = 6418751901993722076L;
protected APUCheckBoxHeader rendererComponent;
protected int column;
protected boolean mousePressed = false;
public APUCheckBoxHeader(ItemListener itemListener) {
rendererComponent = this;
rendererComponent.addItemListener(itemListener);
}
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
if (table != null) {
JTableHeader header = table.getTableHeader();
if (header != null) {
rendererComponent.setForeground(header.getForeground());
rendererComponent.setBackground(header.getBackground());
rendererComponent.setFont(header.getFont());
header.addMouseListener(rendererComponent);
}
}
setColumn(column);
// rendererComponent.setText("Check All");
rendererComponent.setHorizontalAlignment(SwingConstants.CENTER);
setBorder(UIManager.getBorder("TableHeader.cellBorder"));
return rendererComponent;
}
protected void setColumn(int column) {
this.column = column;
}
public int getColumn() {
return column;
}
protected void handleClickEvent(MouseEvent e) {
if (mousePressed) {
mousePressed = false;
JTableHeader header = (JTableHeader) (e.getSource());
JTable tableView = header.getTable();
TableColumnModel columnModel = tableView.getColumnModel();
int viewColumn = columnModel.getColumnIndexAtX(e.getX());
// int column = tableView.convertColumnIndexToModel(viewColumn);
// if ((viewColumn == this.column) && (e.getClickCount() == 1) && (column != -1)) {
if (viewColumn == this.column) {
doClick();
}
}
}
public void mouseClicked(MouseEvent e) {
handleClickEvent(e);
((JTableHeader) e.getSource()).repaint();
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
mousePressed = true;
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
}
MyItemListener class :
Code:
class MyItemListener implements ItemListener {
public void itemStateChanged(ItemEvent e) {
Object source = e.getSource();
if ((source instanceof AbstractButton) == false) {
return;
}
boolean checked = e.getStateChange() == ItemEvent.SELECTED;
if (checked) {
// Select all rows if JCheckBox in the table header is selected and enable the Archive button
table.selectAll();
getBtnMaintArchive().setEnabled(true);
} else {
// Uncheck all rows if JCheckBox in the table header is unselected and disable the Archive
// button
table.clearSelection();
getBtnMaintArchive().setEnabled(false);
}
}
}
Could anyone please tell me how to solve this issue?
Thanks in advance,
Shans