1

I Have developed an application in which i take data from the database and populate in jtable.But the problem is in rendering checkbox.i have coded for select all checkbox but it is giving error in UIManager.Tried a lot but of no use. the code is:-

 public jtable1_1() throws Exception {

    DBEngine dbengine = new DBEngine();
    data = dbengine.getcandidatereport();

    //create header for the table
    header = new Vector<String>();

    header.add("check");
    header.add("Name");
    header.add("UserID"); //Empid
    header.add("EName"); // employee position
    header.add("LeadName");

    initComponents();
    TableColumn tc = jTable1.getColumnModel().getColumn(0);   
    tc.setHeaderRenderer(new CheckBoxHeader(new MyItemListener()));
    tc.setCellEditor(jTable1.getDefaultEditor(Boolean.class));     
    tc.setCellRenderer(jTable1.getDefaultRenderer(Boolean.class));} 



    private void jTable1MouseClicked(java.awt.event.MouseEvent evt)   
    {                                     
       // TODO add your handling code here:
       int row = jTable1.getSelectedRow();
       jTextField1.setText(jTable1.getModel().getValueAt(row,1).toString());
       jTextField2.setText(jTable1.getModel().getValueAt(row,2).toString());
       jTextField3.setText(jTable1.getModel().getValueAt(row,3).toString());
    } 

    public static void main(String args[]) {
        /* Create and display the form */
       java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
           try
            {
                new jtable1_1().setVisible(true);
            }catch(Exception e){e.printStackTrace();}
        }
      });
    }




     public 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 = jTable1.getRowCount(); x < y; x++) {
            jTable1.setValueAt(new Boolean(checked), x, 0);
        }
     }
    }

     public class CheckBoxHeader extends JCheckBox implements TableCellRenderer, MouseListener {

    protected CheckBoxHeader rendererComponent;
    protected int column;
    protected boolean mousePressed = false;

    public CheckBoxHeader(ItemListener itemListener) {
        rendererComponent = this;
        rendererComponent.addItemListener(itemListener);
    }

    public Component getTableCellRendererComponent(JTable jTable1, 
              Object value, boolean isSelected, boolean hasFocus, 
                                          int row, int column) 
    {
        if (jTable1 != null) {
            JTableHeader header = jTable1.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");
        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) {
                doClick();
            }
        }
    }

    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) {
    }
    } 
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
javacode
  • 11
  • 2

2 Answers2

2

public class CheckBoxHeader extends JCheckBox implements TableCellRenderer, MouseListener {

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
0

These two lines are not required. JTable by default does that for you.

tc.setCellEditor(jTable1.getDefaultEditor(Boolean.class));     
tc.setCellRenderer(jTable1.getDefaultRenderer(Boolean.class));

Only thing you have to do is to override getColumnClass(..) in the table model.

private Class<?> getColumnClass(int columnIndex) {
     switch(columnIndex) {
          // This is for your case.
          case 0:
                 return Boolean.class;
          default: 
                 return String.class;
     }
}
Amarnath
  • 8,736
  • 10
  • 54
  • 81