1 Reply Latest reply: Apr 9, 2013 2:44 AM by TPD-Opitz RSS

    chechbox header : deselect all not working


      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;

      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.getForegrou nd());
      rendererComponent.setBackground(header.getBackgrou nd());
      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) {
      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) {
      int row=table1.getRowCount();
      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) {
      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
          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