TableでのJCheckBox TableHeaderの全選択(全逆選択)機能

4615 ワード

初心者の勉強
 
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);

    }

}