This discussion is archived
1 Reply Latest reply: Apr 9, 2013 12:44 AM by TPD-Opitz-Consulting-com RSS

chechbox header : deselect all not working

989157 Newbie
Currently Being Moderated
folks,

I have a jtable generated from my database. IT has 3 columns . The 3rd column has checkboxes in it. I am implementing a "CHECK ALL"
checkbox on the header for selection and deselection of checkboxes. the below code works for selection of all boxes.. IT DOES NOT
WORK WHEN IT IS DESELECTED ? i want to make it work for deselection also

I call the custom table renderer from main program as follows :

tc = table.getColumnModel().getColumn(3);
tc.setHeaderRenderer(new CustomTableCellRenderer1(new MyItemListener(),Name));

this is item listner in main program for checkbox "check all" present in the table header :

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;
for(int x = 0, y = table.getRowCount(); x < y; x++)
{
table.setValueAt(new Boolean(checked),x,3);
}
if (e.getStateChange() == ItemEvent.DESELECTED) // dosent work
{     

System.out.println("deselect all"); // may b code for deselection

}
}
}

this is CustomTableCellRenderer1 class below:

package moxaclient;


import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.event.*;

class CustomTableCellRenderer1 extends JCheckBox
implements TableCellRenderer, MouseListener {
private static final long serialVersionUID = 1L;
protected CustomTableCellRenderer1 rendererComponent;
protected int column;
String Name;
private JTable table1;
Object abc=null;
protected boolean mousePressed = false;


public CustomTableCellRenderer1(ItemListener itemListener,String name) {
rendererComponent = this;
rendererComponent.addItemListener(itemListener);
this.Name=name;
}


public Component getTableCellRendererComponent(
JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (table != null) {
JTableHeader header = table.getTableHeader();
this.table1=table;
if (header != null) {
rendererComponent.setForeground(header.getForegrou nd());
rendererComponent.setBackground(header.getBackgrou nd());
rendererComponent.setFont(header.getFont());
header.addMouseListener(rendererComponent);
}
}
setColumn(column);
rendererComponent.setText("Check All");
setBorder(UIManager.getBorder("TableHeader.cellBor der"));
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) {
doClick();
int row=table1.getRowCount();
if(row!=0)
{
for(int i=0;i<=row-1;i++)
{
String Sensor =(String) table1.getValueAt(i, column-3); // // when checkbox selectedthis code gets all value present in table and stores in string abc
Double Value =(Double) table1.getValueAt(i, column-2);
String Date =(String) table1.getValueAt(i, column-1);
abc += Sensor+"\t"+Value+"\t"+Name+"\t"+Date+"\t";
}
}
}
}

public void mouseClicked(MouseEvent e) {
handleClickEvent(e);
((JTableHeader)e.getSource()).repaint();
}
public void mousePressed(MouseEvent e) {
mousePressed = true;
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
}

plzz help.
  • 1. Re: chechbox header : deselect all not working
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    please add
     tags around the code in your posts.
    -----
    
    Dinud123 wrote:
    IT DOES NOT
    WORK WHEN IT IS DESELECTED ? i want to make it work for deselection also
    I don't know any user interface where I would toggle "select all" and "deselect all" via the same control. That may be for a reason. So my suggestion is: provide a second button for "deselect all". bye TPD                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points