• Table中的JCheckBox TableHeader的全选(全反选)功能


    菜鸟学习ing

    class CheckDefaultModel extends DefaultTableModel

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package Test2;
    
    import javax.swing.table.DefaultTableModel;
    
    /**
     *
     * @author Administrator
     */
    public class CheckDefaultModel extends DefaultTableModel {
    
        Object[] columns;
        Object[][] data ;
    
        public CheckDefaultModel(Object[][] data, Object[] columnNames) {
            super(data, columnNames);
        }
    
        public Class<?> getColumnClass(int columnIndex) {
            return getValueAt(0, columnIndex).getClass();
        }
    
        public void setAllSelected(boolean value) {
            for (int i = 0; i < getRowCount(); i++) {
                this.setValueAt(value, i, 0);
            }
        }
    }
    


     

    class TableHeaderRender implements TableCellRenderer

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package Test2;
    
    import java.awt.Component;
    import java.awt.Point;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import javax.swing.JCheckBox;
    import javax.swing.JComponent;
    import javax.swing.JLabel;
    import javax.swing.JTable;
    import javax.swing.table.JTableHeader;
    import javax.swing.table.TableCellRenderer;
    
    /**
     *
     * @author Administrator
     */
    public class TableHeaderRender implements TableCellRenderer {
    
        JTable table;
        JTableHeader tableHeader;
        CheckDefaultModel model;
        JCheckBox headerBox;
    
        public TableHeaderRender(JTable table) {
            this.table = table;
            this.tableHeader = table.getTableHeader();
            this.model = (CheckDefaultModel) table.getModel();
    
            headerBox = new JCheckBox(model.getColumnName(0));
            headerBox.setSelected(false);
    
            tableHeader.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    int selectColumn = tableHeader.columnAtPoint(e.getPoint());
                    if (selectColumn == 0) {
                        boolean value = !headerBox.isSelected();
                        headerBox.setSelected(value);
                        model.setAllSelected(value);
                        tableHeader.repaint();
                    }
                }
            });
        }
    
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            String valueStr = (String) value;
            JLabel label = new JLabel(valueStr);
            JComponent component = (column == 0) ? headerBox : label;
            return component;
        }
    }
    


     

    class TableTester extends JFrame

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package Test2;
    
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import javax.swing.DefaultCellEditor;
    import javax.swing.JCheckBox;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.border.EmptyBorder;
    import javax.swing.table.TableCellRenderer;
    import javax.swing.table.TableColumn;
    
    /**
     *
     * @author Administrator
     */
    public class TableTester extends JFrame {
    
        JPanel contentPanel = new JPanel();
        JTable table = new JTable();
        Object[] columns = {"choose"};
        Object[][] data = {{new Boolean(true)}, {new Boolean(false)}, {new Boolean(false)}};
    
        public TableTester() {
            setTitle("CheckBox Table2");
            setPreferredSize(new Dimension(300, 200));
            setLocationRelativeTo(this);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            pack();
    
            contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
            contentPanel.setLayout(new BorderLayout());
            add(contentPanel, BorderLayout.NORTH);
    
            JScrollPane scrollPane = new JScrollPane(table);
            scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
            contentPanel.add(scrollPane, BorderLayout.NORTH);
    
            initTable();
        }
    
        public void initTable() {
            
            table.setModel(new CheckDefaultModel(data, columns));
            table.getTableHeader().setDefaultRenderer(new TableHeaderRender(table));
        }
    
        public static void main(String[] args) {
            new TableTester().setVisible(true);
        }
    }
    


     

  • 相关阅读:
    ajax异步服务器获取时间
    JavaScript基本知识
    JavaScript使用button提交表单
    spring与hibernate整合
    Spring的事务属性
    注解方式实现Spring声明式事务管理
    svn的安装使用
    sbn
    恢复oracle中误删除drop掉的表
    ORA-20000: ORU-10027: buffer overflow, limit of 2000 bytes的解决办法
  • 原文地址:https://www.cnblogs.com/james1207/p/3299448.html
Copyright © 2020-2023  润新知