如何取消选中用作标题栏之一的JCheckBox?

时间:2018-10-17 00:14:40

标签: java swing jtable jcheckbox jtableheader

我的JCheckBox“全部选中”标题列的工作方式是,如果我选中该复选框,则此列中的所有复选框都被选中。我要这样做,以便如果您选中复选框标题,以使该列中的所有复选框都被选中,请取消选中该列中的复选框之一,之后将取消选中该复选框标题。

public class TablePanel extends javax.swing.JPanel {

public TablePanel() {
        initComponents();
        populateStringArrays(); //populate string arrays that will populate my table
        table.setModel(tableModel);
        table.getTableHeader().setReorderingAllowed(false);
        setWidthOfTable();
        loadTable(); //loads table with my array
        table.getColumnModel().getColumn(5).setHeaderRenderer(new BoxRenderer(new MyItemListener()));    
}


public class BoxRenderer extends JCheckBox implements TableCellRenderer, MouseListener{

        String columnTitle = "Rings";
        protected BoxRenderer rendererComponent;
        int column;
        boolean mousePressed = false;

        public BoxRenderer(ItemListener listener){
            rendererComponent = this;
            rendererComponent.addItemListener(listener);
        }

        @Override
        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());
                            Border border = header.getBorder();

        header.setBorder(BorderFactory.createRaisedBevelBorder());
                       rendererComponent.setHorizontalTextPosition(SwingConstants.LEADING); 
                            rendererComponent.setHorizontalAlignment(CENTER);  
                            header.addMouseListener(rendererComponent);
                    }
               }
               setColumn(column);
               rendererComponent.setText(columnTitle);
           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) {
            ((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) {

        }



}

class MyItemListener implements ItemListener{

        @Override
        public void itemStateChanged(ItemEvent e) {

                Object source = e.getSource();

                if (source instanceof AbstractButton == false){
                    return;}

                boolean checked = e.getStateChange() == ItemEvent.SELECTED;
                System.out.println("Boolean checked = " + checked);

                if(checked == true){
                    checkAll();
                }else{
                    uncheckAll();
                }


        }

 }

private void checkAll(){

        for(int i=0; i<tableRecordArrayList.size(); i++){    

            table.getModel().setValueAt(true, i, 5);


        }

}

private void uncheckAll(){

        for(int i=0; i<tableRecordArrayList.size(); i++){ 
            table.getModel().setValueAt(false, i, 5);
        }

}

class TableModel extends javax.swing.table.AbstractTableModel{


        /**Constructor**/

        @Override
        public int getRowCount() {
            return tableRecordArrayList.size();
        }

        @Override
        public int getColumnCount() {
            return 6;
        }

        /**refresh for changes**/
        public void refreshTable(){
            fireTableDataChanged();
        }

        @Override
        public Object getValueAt(int row, int column) {
            TableRecord record = tableRecordArrayList.get(row);
            switch(column){

                case 0:
                    return record.firstName;
                case 1:
                    return record.middleName;
                case 2:
                    return record.lastName;
                case 3:
                    return record.age;
                case 4:
                    return record.height;
                case 5:
                    return record.ring;

            }

            return "N/A";
        }

        @Override
        public String getColumnName(int column){

            /**set the title of the columns of the table**/
            switch(column){

                case 0:
                    return "First Name";
                case 1:
                    return "Middle Name";
                case 2:
                    return "Last Name";
                case 3:
                    return "Age";
                case 4:
                    return "Height(cm)";
                case 5:
                    return "Has Championship Ring";

            }

            return "N/A";

        }

        /**turn column into checkboxes**/
        @Override
        public Class<?> getColumnClass(int column){
            if(column == 5){
                return Boolean.class;
            }
            return String.class;
        }


        @Override
        public boolean isCellEditable(int row, int column){
            return column == 5;
        }

        @Override
        public void setValueAt(Object avalue, int row, int column){
            TableRecord record;
            Boolean v;
            switch(column){
                case 5:
                record = tableRecordArrayList.get(row);
                v = (Boolean) avalue;
                record.ring = v;
                fireTableCellUpdated(row,1);
                printArrayList();
                fireTableDataChanged();
                break;
            }
        }




}

}

此问题已解决。请访问此链接以找到解决方案。

https://coderanch.com/t/700630/java/uncheck-JCheckBox-header-columns#3288215

1 个答案:

答案 0 :(得分:0)

  

我要这样做,以便如果您选中复选框标题,以使该列中的所有复选框都被选中,请取消选中该列中的复选框之一,之后将取消选中该复选框标题。

TableModelListener添加到TableModel

生成TableModelEvent后,您可以检查包含复选框的列中的数据是否已更改,然后在需要时重置标题复选框。

签出:JTable -> TableModeListener,了解使用TableModelListener的基本示例。