如何将我的JTable中我的第一行的颜色设置为任何颜色,并保持以前设置的颜色在表中 [英] How to set the colour of my 1st row in my JTable to any color AND keep previous set colors in the table

查看:244
本文介绍了如何将我的JTable中我的第一行的颜色设置为任何颜色,并保持以前设置的颜色在表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Java中阅读了很多关于CellRendering的内容,我也从这个伟大的网站访问了其他Q& As。不幸的是,我仍然没有找到解决方案为以下问题:

I've already read a lot about CellRendering in Java and I have also visited other Q&As from this great site. Unfortunately I still haven't found the solution for the following Problem:

我想渲染一个JTable显示StatusEvents - 这是监视正在运行的系统所必需的。但是,这些StatusEvent由时间戳,文本和颜色组成。

I want to render a JTable which displays StatusEvents - this is necessary for monitoring a running System. However, those StatusEvents consist of a timestamp, a text and a color.

我的目标是启用多个彩色行。为了实现这一点,我已经定义了一个新的JTable子类(重载的getCellRenderer与在插入过程中绘制的Row相关)和一个新的TableCellRenderer子类,它将颜色应用到单元格。

My goal is it to enable multiple colored rows. To achieve this, I've already defined a new JTable-subclass (Overloading "getCellRenderer related to the Row which is being painted during the inseration process) and a new TableCellRenderer-Subclass, which applies the Color to the Cell.

方法如下所示:

MyCustomJTable:

MyCustomJTable:

 @Override
    public TableCellRenderer getCellRenderer(int row, int column) {
        TableCellRenderer result = super.getCellRenderer(row, column);
        if ( row == 0 )
        {
            result = colcr;
        }        
        return result;
        }

colcr是我的自定义CellRenderer,它正在为之前设置的特定颜色的单元格着色。

colcr is my custom CellRenderer which is coloring a Cell in a specific color which is being set before.

Renderer看起来像下面这样:

The new Cell Renderer looks like the following:

    public class ColorCellRenderer extends DefaultTableCellRenderer {

    ColorCellRenderer ( )
    {
        this.m_Color = null;
    }

    @Override
    public Component getTableCellRendererComponent ( JTable table , Object value , boolean isSelected , 
            boolean hasFocus, int row, int column)
    {
        Component c = super.getTableCellRendererComponent
(table, value, isSelected, hasFocus, row, column);
        if ( m_Color != null )
        {
            if ( row == 0 && column == 0)
            {
                c.setForeground(m_Color);      
            }
        }
        return c;
    } 

    public void setColor ( Color c )
    {
        this.m_Color = c;
    }

    private Color m_Color;

}


$ b $ p

不幸的是,目前的解决方案只是最新的第一行配置的颜色,但以前着色的行会丢失其颜色并默认格式化。

Unfortunately the current solution colors just the first row in the latest configured color, but the previously colored rows lose their color and get formatted by default.

我必须避免这种行为吗?

Which possibilities do I have to avoid this behaviour?

真诚

Markus

推荐答案

,例如使用 prepareRenderer()

import java.awt.*;
import javax.swing.*;
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"};
        Object[][] data = {
            {"Buy", "IBM", new Integer(1000), new Double(80.50)},
            {"Sell", "MicroSoft", new Integer(2000), new Double(6.25)},
            {"Sell", "Apple", new Integer(3000), new Double(7.35)},
            {"Buy", "Nortel", new Integer(4000), new Double(20.00)}
        };
        DefaultTableModel model = new DefaultTableModel(data, columnNames);
        table = new JTable(model) {

            private static final long serialVersionUID = 1L;

            @Override
            public Class getColumnClass(int column) {
                return getValueAt(0, column).getClass();
            }

            @Override
            public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
                Component c = super.prepareRenderer(renderer, row, column);
                int firstRow = 0;
                int lastRow = table.getRowCount() - 1;
                if (row == lastRow) {
                    ((JComponent) c).setBackground(Color.red);
                } else if (row == firstRow) {
                    ((JComponent) c).setBackground(Color.blue);
                } else {
                    ((JComponent) c).setBackground(table.getBackground());
                }
                /*if (!isRowSelected(row)) {
                String type = (String) getModel().getValueAt(row, 0);
                c.setBackground("Buy".equals(type) ? Color.GREEN : Color.YELLOW);
                }
                if (isRowSelected(row) && isColumnSelected(column)) {
                ((JComponent) c).setBorder(new LineBorder(Color.red));
                }*/
                return c;
            }
        };
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        JScrollPane scrollPane = new JScrollPane(table);
        getContentPane().add(scrollPane);
    }

    public static void main(String[] args) {
        TablePrepareRenderer frame = new TablePrepareRenderer();
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

这篇关于如何将我的JTable中我的第一行的颜色设置为任何颜色,并保持以前设置的颜色在表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆