3

puuuuuuf,我开始喜欢摇摆 :) 我正在尝试编写一个 cellRenderer 来自定义渲染除第一行和第一列中的所有单元格之外的所有单元格。所以我写了以下内容:

public class CustomTableCellRenderer extends DefaultTableCellRenderer {
        public Component getTableCellRendererComponent(JTable table, Object obj, boolean isSelected, boolean hasFocus, int row, int column) {
            Component cell = super.getTableCellRendererComponent(table, obj, isSelected, hasFocus, row, column);

            if(row >0&&column>0){
                cell.setBackground(Color.GREEN);
            }

            return cell;
        }
    }

并将渲染器设置如下:

scheduleTable.setDefaultRenderer(Object.class, new CustomTableCellRenderer()); 

但由于某种原因,这种方法将渲染器应用于所有单元格。所以他们都是绿色的。如果我做错了什么,你能帮我吗?

提前致谢!

添加

scheduleTable = new JTable() {
                    @Override
                    public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
                        Component comp = super.prepareRenderer(renderer, row, column);
                        int modelRow = convertRowIndexToModel(row);
                        int modelColumn = convertColumnIndexToModel(column);
                        if (modelColumn != 0 && modelRow != 0) {
                            comp.setBackground(Color.GREEN);
                        }
                        return comp;
                    }
                };  

这段代码也使所有表格都变绿了。

这段代码:

scheduleTable = new JTable(tableModel) {
                    @Override
                    public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
                        Component comp = super.prepareRenderer(renderer, row, column);
                        int modelRow = convertRowIndexToModel(row);
                        int modelColumn = convertColumnIndexToModel(column);

                            if (modelRow != 0 && modelColumn != 0) {
                                setBackground(Color.GREEN);
                            } else {
                                setBackground(Color.WHITE);
                            }

                        return comp;
                    }
                };

给我以下结果;(
在此处输入图像描述

以下情况我有不同n的表达方式row != 0 && column != 0
在此处输入图像描述

4

2 回答 2

7

编辑:

if (modelColumn != 0 || modelRow != 0) {

在此处输入图像描述

if (modelColumn != 0 && modelRow != 0) {

在此处输入图像描述

从代码

import java.awt.*;
import java.awt.font.TextAttribute;
import java.util.Map;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.MatteBorder;
import javax.swing.table.*;

public class TablePrepareRenderer extends JFrame {

    private static final long serialVersionUID = 1L;
    private JTable table;

    public TablePrepareRenderer() {
        Object[] columnNames = {"Type", "Company", "Shares", "Price", "Boolean"};
        Object[][] data = {
            {"Buy", "IBM", new Integer(1000), new Double(80.50), false},
            {"Sell", "MicroSoft", new Integer(2000), new Double(6.25), true},
            {"Sell", "Apple", new Integer(3000), new Double(7.35), true},
            {"Buy", "Nortel", new Integer(4000), new Double(20.00), false}
        };
        DefaultTableModel model = new DefaultTableModel(data, columnNames) {

            private static final long serialVersionUID = 1L;

            @Override
            public Class getColumnClass(int column) {
                return getValueAt(0, column).getClass();
            }
        };
        table = new JTable(model) {

            private static final long serialVersionUID = 1L;
            private Border outside = new MatteBorder(1, 0, 1, 0, Color.red);
            private Border inside = new EmptyBorder(0, 1, 0, 1);
            private Border highlight = new CompoundBorder(outside, inside);

            @Override
            public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
                int modelRow = convertRowIndexToModel(row);
                int modelColumn = convertColumnIndexToModel(column);
                if (!isRowSelected(modelRow)) {
                    if (modelColumn != 0 || modelRow != 0) {
                        comp.setBackground(Color.GREEN);
                    } else {
                        comp.setBackground(table.getBackground());
                    }
                }
                return comp;

                /*Component comp = super.prepareRenderer(renderer, row, column);
                JComponent jc = (JComponent) comp;
                Map attributes = (new Font("Serif", Font.PLAIN, 12)).getAttributes();
                //attributes.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
                attributes.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
                if (!isRowSelected(row)) {
                comp.setForeground(Color.black);
                comp.setBackground(row % 2 == 0 ? Color.white : Color.orange);
                int modelRow = convertRowIndexToModel(row);
                String type = (String) getModel().getValueAt(modelRow, 0);
                if (type.equals("Sell")) {
                comp.setFont(new Font(attributes));
                comp.setForeground(Color.red);
                } else {
                comp.setFont(new Font("Serif", Font.BOLD, 12));
                }
                } else {
                comp.setFont(table.getFont());
                }
                jc.setBorder(BorderFactory.createCompoundBorder(jc.getBorder(), BorderFactory.createEmptyBorder(0, 0, 0, 5)));
                return comp;*/
            }
        };
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        JScrollPane scrollPane = new JScrollPane(table);
        getContentPane().add(scrollPane);
    }

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

            @Override
            public void run() {
                TablePrepareRenderer frame = new TablePrepareRenderer();
                frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}
于 2012-05-17T11:31:40.553 回答
0

使用行和列值而不转换为模型

于 2013-04-17T06:39:24.600 回答