当前位置: 首页 > 面试题库 >

彩色表格单元格

齐招
2023-03-14
问题内容

我想根据第1列中的值给表格单元格上色,如果值不等于第1列中的值,则颜色应为青色,但我的代码不起作用:

table = new JTable(){
           public TableCellRenderer getCellRenderer(int row, int column) {
              TableCellRenderer tcr=null;
               Color c;
               if(column>=1&&getValueAt(row, column)!=null &&getValueAt(row, 1)!=null &&!getValueAt(row, column).equals(getValueAt(row, 1)))
                   c=Color.CYAN;
               else
                   c=Color.white;
                if(getValueAt(row, column) instanceof Boolean) {
                  tcr= super.getDefaultRenderer(Boolean.class);
                } else {
                  tcr=  super.getCellRenderer(row, column);
                }

               tcr.getTableCellRendererComponent(this, getValueAt(row, column), isCellSelected(row, column) , hasFocus(), row, column).setBackground(c);
               return tcr;

            }
            public TableCellEditor getCellEditor(int row, int column) {
                if(getValueAt(row, column) instanceof Boolean) {
                    return super.getDefaultEditor(Boolean.class);
                } else {
                    return super.getCellEditor(row, column);
                }
            }
    };
          table.setModel(new DefaultTableModel(
            new Object[][] {
             {"Row1", "1","2","3"},
             {"Row2", "2","2","3"},
             {"Row3", "2","2","2"},
             {"Row3", "2","1","2"}
                           },
            new String[] {"Header1", "Header2","Header3","Header4"}
    ));

问题答案:

是否必须突出显示单元格的决定必须在TableCellRenderer中进行。它不能在JTable中进行。因此,(没有)没有理由要重写JTable中的方法。相反,您可以将列的CellRenderer设置为TableCellRenderer的自己实现。

在此示例中,列2和3接收“
ColoringCellRenderer”。如果此单元格中的值不为null且与列1中的值相同,则此ColoringCellRenderer将突出显示该单元格。

import java.awt.Color;
import java.awt.Component;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;


public class ColoredTableCells
{
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI()
    {
        JFrame f = new JFrame();
        f.getContentPane().add(new JScrollPane(createTable()));
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    static JTable createTable()
    {
        class ColoringTableCellRenderer extends DefaultTableCellRenderer
        {
            ColoringTableCellRenderer()
            {
                setOpaque(true);
            }

            @Override
            public Component getTableCellRendererComponent(JTable table,
                Object value, boolean isSelected, boolean hasFocus, int row,
                int column)
            {
                super.getTableCellRendererComponent(
                    table, value, isSelected, hasFocus, row, column);
                if (highlightCell(table, row, column))
                {
                    setBackground(Color.CYAN);
                }
                else
                {
                    setBackground(Color.WHITE);
                }
                return this;
            }

            private boolean highlightCell(JTable table, int row, int column)
            {
                Object thisValue = table.getValueAt(row, column);
                Object referenceValue = table.getValueAt(row, 1);
                if (thisValue != null && referenceValue != null)
                {
                    return thisValue.equals(referenceValue);
                }
                return false;
            }
        }

        JTable table = new JTable();
        table.setModel(new DefaultTableModel(new Object[][] {
            { "Row1", "1", "2", "3" },
            { "Row2", "2", "2", "3" },
            { "Row3", "2", "2", "2" }, 
            { "Row3", "2", "1", "2" } },
            new String[] { "Header1", "Header2", "Header3", "Header4" }));


        for (int i=2; i<table.getColumnCount(); i++)
        {
            TableColumn column = table.getColumnModel().getColumn(i);
            column.setCellRenderer(new ColoringTableCellRenderer());
        }
        return table;
    }
}

编辑:答案已经被接受,但针对评论,一个不同的版本:

该类使用两个类“ DelegatingTableCellRenderer”和“
DelegatingTableCellEditor”。这些类各自接收各自的委托,并且仅设置委托所传递的单元格组件的背景色。

这样,着色表单元格(用于渲染器或编辑器)的功能就与实际渲染器分离了。因此,可以为默认的CellRenderer的背景着色(例如Boolean显示JCheckBox的值)。

import java.awt.Color;
import java.awt.Component;
import java.util.EventObject;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.event.CellEditorListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;


public class ColoredTableCells2
{
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI()
    {
        JFrame f = new JFrame();
        f.getContentPane().add(new JScrollPane(createTable()));
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    static JTable createTable()
    {
        JTable table = new JTable()
        {
            @Override
            public Class<?> getColumnClass(int column) 
            {
                if (column == 0)
                {
                    return Object.class;
                }
                return Boolean.class;
            }
        };

        table.setDefaultEditor(Boolean.class, 
            new DelegatingTableCellEditor(table.getDefaultEditor(Boolean.class)));

        table.setDefaultRenderer(Boolean.class, 
            new DelegatingTableCellRenderer(table.getDefaultRenderer(Boolean.class)));

        table.setModel(new DefaultTableModel(new Object[][] {
            { "Row1", true,  false, true },
            { "Row2", true,  true,  false },
            { "Row3", false, false, true }, 
            { "Row3", false, true,  false } },
            new String[] { "Header1", "Header2", "Header3", "Header4" }));


        return table;
    }


    static class DelegatingTableCellEditor implements TableCellEditor
    {
        private final TableCellEditor delegate;

        DelegatingTableCellEditor(TableCellEditor delegate)
        {
            this.delegate = delegate;
        }

        @Override
        public Object getCellEditorValue()
        {
            return delegate.getCellEditorValue();
        }

        @Override
        public boolean isCellEditable(EventObject anEvent)
        {
            return delegate.isCellEditable(anEvent); 
        }

        @Override
        public boolean shouldSelectCell(EventObject anEvent)
        {
            return delegate.shouldSelectCell(anEvent);
        }

        @Override
        public boolean stopCellEditing()
        {
            return delegate.stopCellEditing();
        }

        @Override
        public void cancelCellEditing()
        {
            delegate.cancelCellEditing();
        }

        @Override
        public void addCellEditorListener(CellEditorListener l)
        {
            delegate.addCellEditorListener(l);
        }

        @Override
        public void removeCellEditorListener(CellEditorListener l)
        {
            delegate.removeCellEditorListener(l);
        }

        @Override
        public Component getTableCellEditorComponent(JTable table,
            Object value, boolean isSelected, int row, int column)
        {

            Component component = 
                delegate.getTableCellEditorComponent(
                    table, value, isSelected, row, column);
            if (component instanceof JComponent)
            {
                JComponent c = (JComponent)component;
                c.setOpaque(true);
            }
            if (highlightCell(table, row, column))
            {
                component.setBackground(Color.CYAN);
            }
            else
            {
                component.setBackground(Color.WHITE);
            }
            return component;
        }

    }


    static class DelegatingTableCellRenderer extends DefaultTableCellRenderer          
    {
        private final TableCellRenderer delegate;

        DelegatingTableCellRenderer(TableCellRenderer delegate)
        {
            this.delegate = delegate;
        }

        @Override
        public Component getTableCellRendererComponent(
            JTable table, Object value, boolean isSelected,
            boolean hasFocus, int row, int column)
        {
            Component component = 
                delegate.getTableCellRendererComponent(
                    table, value, isSelected, hasFocus, row, column);
            if (component instanceof JComponent)
            {
                JComponent c = (JComponent)component;
                c.setOpaque(true);
            }
            if (highlightCell(table, row, column))
            {
                component.setBackground(Color.CYAN);
            }
            else
            {
                component.setBackground(Color.WHITE);
            }
            return component;
        } 
    }

    private static boolean highlightCell(JTable table, int row, int column)
    {
        if (column == 1)
        {
            return false;
        }
        Object thisValue = table.getModel().getValueAt(row, column);
        Object referenceValue = table.getModel().getValueAt(row, 1);
        if (thisValue != null && referenceValue != null)
        {
            return thisValue.equals(referenceValue);
        }
        return false;
    }

}


 类似资料:
  • 我想让标题行有不同的颜色。到目前为止,我只是对字体有不同的颜色,因为背景颜色不能像我希望的那样工作。我的输出文件应为xls格式。 我尝试了命令,但这是igno。有没有另一种给背景着色的方法?

  • 对于某些表格单元格,我希望有一个多色的上边框或下边框。 基于如何创建多色边框与css和css-多色边框,我能够创建多色边框。 问题是,我想将其组合为一个表格单元格,其中一些单元格具有普通边框,而另一些单元格具有多色边框。 下面的代码设置单元格的多色,但只要我想为“普通”单元格添加黑色边框,它就会覆盖多色边框(请参见代码笔) 这似乎使它部分起作用,必须为表格设置背景颜色。但这会导致粗边框线。 能做些

  • 我有2张纸A和B。如果B张纸中完全相同的单元格不是空的,我想让A张纸给单元格着色。例如,如果页B中的单元格具有字符串,则页A中的单元格将被着色。 假设我想做这个来比较一个大范围,我应该怎么做呢? 示例:https://docs.google.com/spreadsheets/d/1P3Ob_mclpXWmILfKwD4R6JN2wAYPUcNZlmtF9LxilV0/edit?usp=sharin

  • 问题内容: 我正在尝试使用渲染器为jTable的单元格上色,但是它们工作不佳,因为它们滞后于表格并且无法看到。这是我的代码: 我没有将其放入rendererclass中,因为它滞后了,我将其放入cicle的双精度中,具体地说,放入了第二个cicle。我希望它为超过24的单元格上色,如果现在我写的话,那是行不通的 它使桌子完全着色 编辑 按照要求,我创建了一个描述我的问题的小示例,我不知道是否存在发