jtable 标头中的复选框事件

发布于 2024-11-10 18:31:05 字数 3097 浏览 4 评论 0原文

您好,我有一个 jtable,列中带有复选框,列标题

问题是如果我单击第一个列标题, 第一个列标题是选择的,第二个列标题是

    String[] columnNames = {"Am", "Pm", "Integer", "String"};
    Object[][] data = {{true, true, 1, "BBB"}, {false, true, 12, "AAA"},
        {true, false, 2, "DDD"}, {false, false, 5, "CCC"},
        {true, true, 3, "EEE"}, {false, false, 6, "GGG"},
        {true, true, 4, "FFF"}, {false, false, 7, "HHH"}};
    DefaultTableModel model = new DefaultTableModel(data, columnNames) {

        @Override
        public Class<?> getColumnClass(int column) {
            return getValueAt(0, column).getClass();
        }
    };
    JTable table = new JTable(model);
    table.getColumnModel().getColumn(0).setHeaderRenderer(
            new HeaderRenderer2(table.getTableHeader()));


    table.getColumnModel().getColumn(1).setHeaderRenderer(
            new HeaderRenderer2(table.getTableHeader()));

public class HeaderRenderer2 implements TableCellRenderer {

private final JCheckBox check = new JCheckBox();

public HeaderRenderer2(JTableHeader header) {
    check.setOpaque(false);
    check.setFont(header.getFont());
    header.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent e) {
            JTable table = ((JTableHeader) e.getSource()).getTable();
            TableColumnModel columnModel = table.getColumnModel();
            int viewColumn = columnModel.getColumnIndexAtX(e.getX());
            int modelColumn = table.convertColumnIndexToModel(viewColumn);
            if (modelColumn == 0) {
                check.setSelected(!check.isSelected());
                TableModel m = table.getModel();
                Boolean f = check.isSelected();
                for (int i = 0; i < m.getRowCount(); i++) {
                    m.setValueAt(f, i, 0);
                }
                ((JTableHeader) e.getSource()).repaint();
            }
        }
    });
}

@Override
public Component getTableCellRendererComponent(
        JTable tbl, Object val, boolean isS, boolean hasF, int row, int col) {
    TableCellRenderer r = tbl.getTableHeader().getDefaultRenderer();
    JLabel l = (JLabel) r.getTableCellRendererComponent(tbl, val, isS, hasF, row, col);
    l.setIcon(new CheckBoxIcon(check));
    return l;
}

private static class CheckBoxIcon implements Icon {

    private final JCheckBox check;

    public CheckBoxIcon(JCheckBox check) {
        this.check = check;
    }

    @Override
    public int getIconWidth() {
        return check.getPreferredSize().width;
    }

    @Override
    public int getIconHeight() {
        return check.getPreferredSize().height;
    }

    @Override
    public void paintIcon(Component c, Graphics g, int x, int y) {
        SwingUtilities.paintComponent(
                g, check, (Container) c, x, y, getIconWidth(), getIconHeight());
    }
}
}

在顶部创建了一个表格单元格渲染器图像,没有选择任何内容显示的问题 单击列标题 (AM)

getTableCellRendererComponent 后底部的图像发生在 mouseClicked 事件之前,因此我无法获取单击的列

任何想法?

hi i have a jtable with checkbox in column and column header

problem is if i click on the first column header,
the first column header is select and the the second one

    String[] columnNames = {"Am", "Pm", "Integer", "String"};
    Object[][] data = {{true, true, 1, "BBB"}, {false, true, 12, "AAA"},
        {true, false, 2, "DDD"}, {false, false, 5, "CCC"},
        {true, true, 3, "EEE"}, {false, false, 6, "GGG"},
        {true, true, 4, "FFF"}, {false, false, 7, "HHH"}};
    DefaultTableModel model = new DefaultTableModel(data, columnNames) {

        @Override
        public Class<?> getColumnClass(int column) {
            return getValueAt(0, column).getClass();
        }
    };
    JTable table = new JTable(model);
    table.getColumnModel().getColumn(0).setHeaderRenderer(
            new HeaderRenderer2(table.getTableHeader()));


    table.getColumnModel().getColumn(1).setHeaderRenderer(
            new HeaderRenderer2(table.getTableHeader()));

i created a table cell renderer

public class HeaderRenderer2 implements TableCellRenderer {

private final JCheckBox check = new JCheckBox();

public HeaderRenderer2(JTableHeader header) {
    check.setOpaque(false);
    check.setFont(header.getFont());
    header.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent e) {
            JTable table = ((JTableHeader) e.getSource()).getTable();
            TableColumnModel columnModel = table.getColumnModel();
            int viewColumn = columnModel.getColumnIndexAtX(e.getX());
            int modelColumn = table.convertColumnIndexToModel(viewColumn);
            if (modelColumn == 0) {
                check.setSelected(!check.isSelected());
                TableModel m = table.getModel();
                Boolean f = check.isSelected();
                for (int i = 0; i < m.getRowCount(); i++) {
                    m.setValueAt(f, i, 0);
                }
                ((JTableHeader) e.getSource()).repaint();
            }
        }
    });
}

@Override
public Component getTableCellRendererComponent(
        JTable tbl, Object val, boolean isS, boolean hasF, int row, int col) {
    TableCellRenderer r = tbl.getTableHeader().getDefaultRenderer();
    JLabel l = (JLabel) r.getTableCellRendererComponent(tbl, val, isS, hasF, row, col);
    l.setIcon(new CheckBoxIcon(check));
    return l;
}

private static class CheckBoxIcon implements Icon {

    private final JCheckBox check;

    public CheckBoxIcon(JCheckBox check) {
        this.check = check;
    }

    @Override
    public int getIconWidth() {
        return check.getPreferredSize().width;
    }

    @Override
    public int getIconHeight() {
        return check.getPreferredSize().height;
    }

    @Override
    public void paintIcon(Component c, Graphics g, int x, int y) {
        SwingUtilities.paintComponent(
                g, check, (Container) c, x, y, getIconWidth(), getIconHeight());
    }
}
}

image at the top nothing selectedproblem displayed
image at the bottom after a click on the column header (AM)

getTableCellRendererComponent happen before the mouseClicked event so i'm not able to get the clicked column

any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

流年里的时光 2024-11-17 18:31:10

您应该在设置值循环中传递列索引

public void mouseClicked(MouseEvent e) {
             JTable table = ((JTableHeader) e.getSource()).getTable();
             TableColumnModel columnModel = table.getColumnModel();
             int viewColumn = columnModel.getColumnIndexAtX(e.getX());
             int modelColumn = table.convertColumnIndexToModel(viewColumn);
             if (modelColumn == 0 || modelColumn == 1) {
                 check.setSelected(!check.isSelected());
                 TableModel m = table.getModel();
                 Boolean f = check.isSelected();
                 for (int i = 0; i < m.getRowCount(); i++) {
                     m.setValueAt(f, i, modelColumn);
                 }
                 ((JTableHeader) e.getSource()).repaint();
             }
         } 

You should pass your column index in the set value loop

public void mouseClicked(MouseEvent e) {
             JTable table = ((JTableHeader) e.getSource()).getTable();
             TableColumnModel columnModel = table.getColumnModel();
             int viewColumn = columnModel.getColumnIndexAtX(e.getX());
             int modelColumn = table.convertColumnIndexToModel(viewColumn);
             if (modelColumn == 0 || modelColumn == 1) {
                 check.setSelected(!check.isSelected());
                 TableModel m = table.getModel();
                 Boolean f = check.isSelected();
                 for (int i = 0; i < m.getRowCount(); i++) {
                     m.setValueAt(f, i, modelColumn);
                 }
                 ((JTableHeader) e.getSource()).repaint();
             }
         } 
旧人 2024-11-17 18:31:10

尝试此操作,

您必须指出要修改的列

String[] columnNames = {"Am", "Pm", "Integer", "String"};
Object[][] data = {{true, true, 1, "BBB"}, {false, true, 12, "AAA"},
    {true, false, 2, "DDD"}, {false, false, 5, "CCC"},
    {true, true, 3, "EEE"}, {false, false, 6, "GGG"},
    {true, true, 4, "FFF"}, {false, false, 7, "HHH"}};
DefaultTableModel model = new DefaultTableModel(data, columnNames) {

    @Override
    public Class<?> getColumnClass(int column) {
        return getValueAt(0, column).getClass();
    }
};
JTable table = new JTable(model);
table.getColumnModel().getColumn(0).setHeaderRenderer(
        new HeaderRenderer2(table.getTableHeader(), 0));


table.getColumnModel().getColumn(1).setHeaderRenderer(
        new HeaderRenderer2(table.getTableHeader(), 1));

public HeaderRenderer2(JTableHeader header, final int index) {

    // index is the column to be modified

    check.setOpaque(false);
    check.setFont(header.getFont());
    header.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent e) {
            JTable table = ((JTableHeader) e.getSource()).getTable();
            TableColumnModel columnModel = table.getColumnModel();
            int viewColumn = columnModel.getColumnIndexAtX(e.getX());
            int modelColumn = table.convertColumnIndexToModel(viewColumn);
            if (modelColumn == index) {
                check.setSelected(!check.isSelected());
                TableModel m = table.getModel();
                Boolean f = check.isSelected();
                for (int i = 0; i < m.getRowCount(); i++) {
                    m.setValueAt(f, i, index);
                }
                ((JTableHeader) e.getSource()).repaint();
            }else return;
        } 
    }
            );
}

try this

you mus indicate the column to modify

String[] columnNames = {"Am", "Pm", "Integer", "String"};
Object[][] data = {{true, true, 1, "BBB"}, {false, true, 12, "AAA"},
    {true, false, 2, "DDD"}, {false, false, 5, "CCC"},
    {true, true, 3, "EEE"}, {false, false, 6, "GGG"},
    {true, true, 4, "FFF"}, {false, false, 7, "HHH"}};
DefaultTableModel model = new DefaultTableModel(data, columnNames) {

    @Override
    public Class<?> getColumnClass(int column) {
        return getValueAt(0, column).getClass();
    }
};
JTable table = new JTable(model);
table.getColumnModel().getColumn(0).setHeaderRenderer(
        new HeaderRenderer2(table.getTableHeader(), 0));


table.getColumnModel().getColumn(1).setHeaderRenderer(
        new HeaderRenderer2(table.getTableHeader(), 1));

and

public HeaderRenderer2(JTableHeader header, final int index) {

    // index is the column to be modified

    check.setOpaque(false);
    check.setFont(header.getFont());
    header.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent e) {
            JTable table = ((JTableHeader) e.getSource()).getTable();
            TableColumnModel columnModel = table.getColumnModel();
            int viewColumn = columnModel.getColumnIndexAtX(e.getX());
            int modelColumn = table.convertColumnIndexToModel(viewColumn);
            if (modelColumn == index) {
                check.setSelected(!check.isSelected());
                TableModel m = table.getModel();
                Boolean f = check.isSelected();
                for (int i = 0; i < m.getRowCount(); i++) {
                    m.setValueAt(f, i, index);
                }
                ((JTableHeader) e.getSource()).repaint();
            }else return;
        } 
    }
            );
}
她比我温柔 2024-11-17 18:31:09

问题出在鼠标监听器上。两个实例都列出了鼠标单击标题上的列 ==0 的情况。因此,当您单击该标题时,两者都会被选中或取消选中。

The problem is in the mouse listener. Both instances are listing for mouse clicks on the header that have a column ==0. Therefore both get checked or unchecked when you click on that header.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文