博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Table中的JCheckBox TableHeader的全选(全反选)功能
阅读量:6091 次
发布时间:2019-06-20

本文共 4111 字,大约阅读时间需要 13 分钟。

菜鸟学习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); }}

 

 

 

转载地址:http://jhlwa.baihongyu.com/

你可能感兴趣的文章
提示Windows Phone IP over USB Transport (IpOverUsbSvc)未运行,如何解决
查看>>
DHCP服务器构建
查看>>
银商通电子商务有限公司(总结)
查看>>
cisco vlan trunk 以太网 三层交换
查看>>
20条Linux命令面试问答
查看>>
沟通CTBS助六和集团实现财务集中管理
查看>>
Office 365 将在2018年3月1日弃用TLS 1.0/1.1
查看>>
linux的nohup命令的用法
查看>>
Activiti 环境
查看>>
python pip 安装
查看>>
查看CPU信息的命令详解
查看>>
Bitnami-Redmine通过https远程连接svn
查看>>
买家与卖家也能战略合作
查看>>
shell删除每行开始的数字
查看>>
前端--CSS
查看>>
DoD模型与OSI模型的关系及其协议对应关系
查看>>
网卡报错:Failed to start LSB: Bring up/down networking
查看>>
MySQL的root密码忘记后重置方法
查看>>
boost read_some函数历程
查看>>
lvm逻辑卷管理
查看>>