Results 1 to 2 of 2
  1. #1
    dinu is offline Member
    Join Date
    Jan 2013
    Posts
    31
    Rep Power
    0

    Default chechbox : deselect all not working

    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++)
    { // when checkbox selected
    String Sensor =(String) table1.getValueAt(i, column-3); // this 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.

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: chechbox : deselect all not working

    Please post your code in [code] tags [/code] so it retains formatting.
    Many of us will not bother reading unformatted code, expecially lots of unformatted code.

    I would suggest printing out the value you are getting in the item listener so you get an idea of what the code is doing.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 1
    Last Post: 03-17-2012, 10:38 PM
  2. Replies: 2
    Last Post: 02-03-2011, 05:19 AM
  3. Replies: 2
    Last Post: 01-04-2011, 04:30 AM
  4. Deselect Radio Buttons
    By linux1man in forum AWT / Swing
    Replies: 7
    Last Post: 03-21-2009, 04:01 AM
  5. Replies: 8
    Last Post: 05-28-2008, 07:00 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •