本文介绍了JTable prepareEditor的工作原理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白prepareEditor是如何工作的,我无法使用prepareEditor中的Action为prepareRenderer设置可见性规则,因为我在附加图像中捕获了

I don't understand how prepareEditor works, I'm not able set visibility rules for prepareRenderer with Action from prepareEditor, as I captured in attached Image

第一行的规则正如我所料,如果没有选择JCheckBox,其他TableCells是不可见的(使用JTable选择hightlighter),但是如果我在1.st TableColumn中选择JCheckBox,那么我不能只显示Rule + Mail TableColumn,只能显示TableRow中的所有组件,我怎样才能实现

rules for 1.st row works as I expected, other TableCells are un-visible if JCheckBox isn't selected (with JTable selection hightlighter too), but if I select JCheckBox in 1.st TableColumn, then I can't shows only Rule + Mail TableColumn, only to shows all Components in the TableRow, how can I archieve that

import java.awt.Component;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

public class TableWithCheckBoxOrBlank {

    private Object[] columns = new Object[]{
        "Select", "Name", "Rule", "Mail", "Include", "Phone"};
    private Object[][] data = {
        {false, "Bill", false, "Blabla@bla", false, "00 000 000"},
        {false, "Edd", false, "Blabla@bla", false, "00 000 000"},
        {false, "Paul", false, "Blabla@bla", false, "00 000 000"},
        {false, "ZOO", false, "Blabla@bla", false, "00 000 000"}};

    public void makeUI() {
        DefaultTableModel model = new DefaultTableModel(data, columns) {

            private static final long serialVersionUID = 1L;

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

            @Override
            public Class getColumnClass(int columnIndex) {
                switch (columnIndex) {
                    case 0:
                        return Boolean.class;
                    case 1:
                        return String.class;
                    case 2:
                        return Boolean.class;
                    case 3:
                        return String.class;
                    case 4:
                        return Boolean.class;
                    case 5:
                        return String.class;
                }
                return null;
            }
        };
        final JTable table = new JTable(model) {

            private static final long serialVersionUID = 1L;

            @Override
            public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
                Component c = super.prepareRenderer(renderer, row, column);
                if (column == 0 || column == 1 || (Boolean) getValueAt(row, 0)) {
                    return c;
                }
                return Box.createRigidArea(c.getPreferredSize());
            }

            @Override
            public Component prepareEditor(TableCellEditor editor, int row, int column) {
                Component c = super.prepareEditor(editor, row, column);
                if (column == 0 || column == 1 || (Boolean) getValueAt(row, 0)) {
                    return c;
                }
                return Box.createRigidArea(c.getPreferredSize());
            }
        };
        model.addTableModelListener(new TableModelListener() {

            @Override
            public void tableChanged(TableModelEvent e) {
                if (e.getColumn() == 0) {
                    if ((Boolean) table.getValueAt(table.getSelectedRow(), 0)) {
                        table.setValueAt(Boolean.FALSE, table.getSelectedRow(), 2);
                    }
                    table.repaint();
                }
            }
        });
        JScrollPane scroll = new JScrollPane(table);
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        JFrame frame = new JFrame("Table With Check Box Or Blank");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(150, 150);
        frame.add(scroll);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TableWithCheckBoxOrBlank().makeUI();
            }
        });
    }
}

+++

编辑真的没有包括在内,我看不到森林中的树木: - (

EDIT really no baterry included, I can't see the forest for the trees :-(

感谢@camickr眼睛之间的踢,到处都有很多重大错误/问题...

thanks to @camickr for kick between eyes, there were lots of big mistakes/problems everywhere...




import java.awt.Component;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

public class TableWithCheckBoxOrBlank {

    private Object[] columns = new Object[]{
        "Select", "Name", "Rule", "Mail", "Include", "Phone"};
    private Object[][] data = {
        {false, "Bill", false, "Blabla@bla", false, "00 000 000"},
        {false, "Edd", false, "Blabla@bla", false, "00 000 000"},
        {false, "Paul", false, "Blabla@bla", false, "00 000 000"},
        {false, "ZOO", false, "Blabla@bla", false, "00 000 000"}};

    public void makeUI() {
        DefaultTableModel model = new DefaultTableModel(data, columns) {

            private static final long serialVersionUID = 1L;

            @Override
            public boolean isCellEditable(int row, int column) {
                if (column == 0 || column == 2 || column == 4) {
                    return true;
                }
                if (column == 1 || column == 3 || column == 5) {
                    return false;
                }
                return (Boolean) getValueAt(row, 0);
            }

            @Override
            public Class getColumnClass(int columnIndex) {
                switch (columnIndex) {
                    case 0:
                        return Boolean.class;
                    case 1:
                        return String.class;
                    case 2:
                        return Boolean.class;
                    case 3:
                        return String.class;
                    case 4:
                        return Boolean.class;
                    case 5:
                        return String.class;
                }
                return null;
            }
        };
        final JTable table = new JTable(model) {

            private static final long serialVersionUID = 1L;

            @Override
            public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
                Component c = super.prepareRenderer(renderer, row, column);
                if ((column == 4 || column == 5) && (Boolean) getValueAt(row, 2)) {
                    return c;
                } else if ((column == 2 || column == 3) && (Boolean) getValueAt(row, 0)) {
                    return c;
                } else if (column == 0 || column == 1) {
                    return c;
                }
                return Box.createRigidArea(c.getPreferredSize());
            }

            @Override
            public Component prepareEditor(TableCellEditor editor, int row, int column) {
                Component c = super.prepareEditor(editor, row, column);
                if ((column == 4 || column == 5) && (Boolean) getValueAt(row, 2)) {
                    return c;
                } else if ((column == 2 || column == 3) && (Boolean) getValueAt(row, 0)) {
                    return c;
                } else if (column == 0 || column == 1) {
                    return c;
                }
                return Box.createRigidArea(c.getPreferredSize());
            }
        };
        model.addTableModelListener(new TableModelListener() {

            @Override
            public void tableChanged(TableModelEvent e) {
                if (e.getColumn() == 0) {
                    if ((Boolean) table.getValueAt(table.getSelectedRow(), 0)) {
                        table.setValueAt(Boolean.FALSE, table.getSelectedRow(), 2);
                    }
                    table.repaint();
                } else if (e.getColumn() == 2) {
                    if ((Boolean) table.getValueAt(table.getSelectedRow(), 2)) {
                        table.setValueAt(Boolean.FALSE, table.getSelectedRow(), 4);
                    }
                    table.repaint();
                }
            }
        });
        JScrollPane scroll = new JScrollPane(table);
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        JFrame frame = new JFrame("Table With Check Box Or Blank");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(150, 150);
        frame.add(scroll);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TableWithCheckBoxOrBlank().makeUI();
            }
        });
    }
}


推荐答案

我认为 prepareRenderer(...)代码应为:

        public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
            Component c = super.prepareRenderer(renderer, row, column);

            if ((column == 2 || column == 3) && (Boolean)getValueAt(row, 0)) {
                return c;
            }

        //    if (column == 0 || column == 1 || (Boolean) getValueAt(row, 0)) {
            if (column == 0 || column == 1) {
                return c;
            }

            return Box.createRigidArea(c.getPreferredSize());
        }

然后你应该摆脱 prepareEditor(。 ..)代码。相反,你应该在 isCellEditable(...)方法中有更多的逻辑。类似于:

Then you should get rid of the prepareEditor(...) code. Instead you should have more logic in the isCellEditable(...) method. Something like:

        public boolean isCellEditable(int row, int column) {

            if (column == 0) return true;

            if (column == 1 || column == 4 || column == 5) return false;

            return (Boolean)getValueAt(row, 0);
        }

这篇关于JTable prepareEditor的工作原理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 10:50