更改 Swing JTable 单元格颜色 [英] Changing Swing JTable Cell Colors

查看:30
本文介绍了更改 Swing JTable 单元格颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试适应 JTables、TableModels、JTableHeaders、渲染器等.我正在尝试制作一个简单的虚拟表(用于练习),如下所示:

I'm trying to get comfortable with JTables, TableModels, JTableHeaders, renderers, etc. I am trying to make a simple dummy table (for practice purposes) that looks like this:

-    1    2   3
A    A1   A2  A3
B    B1   B2  B3
C    C1   C2  C3

我还希望 B2 单元格 - 并且只有那个单元格 - 具有蓝色 (Color.BLUE) 背景 - 所有其他单元格都可以具有自动分配的 Swing 默认颜色.

I also want the B2 cell - and only that cell - to have a blue (Color.BLUE) background - all other cells can have the Swing default color they are assigned automagically.

我的代码如下,基于我在本网站和整个互联网上找到的无数示例.但我没有得到我想要的结果.相反,我得到了一个看起来像这样的表:

My code is below and is based off countless examples I have found on this website and the internet at large. But I am not getting the results I want. Instead I'm getting a table that looks like this:

A    A1   A2  A3
B    B1   B2  B3
C    C1   C2  C3

请注意,第一行(标题)根本不存在.此外,使用我在下面列出的代码,这会执行并设置所有有颜色的单元格的颜色,而不仅仅是我想要的 B2 单元格.

Notice that the first row (the header) isn't there at all. Additionally, with the code I list below, this executes and sets the color of all the cells that color, not just the B2 cell that I want.

代码:

public class MyTable
{
    public static void main(String[] args)
    {
        String[][] data = getTableData();
        String[] cols = getTableCols();

        JFrame frame = magicallyCreateJFrame();     // I promise this works!
        MyRenderer myRenderer = new MyRenderer();   // See below

        DefaultTableModel defModel = new DefaultTableModel(data, cols);
        JTable myTable = new JTable(defModel);

        myTable.setDefaultRenderer(Object.class, myRenderer);

        frame.add(myTable);
        frame.pack();
        frame.setVisible(true);            
    }
}

public static String[] getTableCols()
{
    String cols =
    {
        "-",
        "1",
        "2",
        "3",
    };
}

public static String[][] getTableData()
{
    String[][] data =
    {
        {
            "A",
            "A1",
            "A2",
            "A3",
        },
        {
            "B",
            "B1",
            "B2",
            "B3",
        },
        {
            "C",
            "C1",
            "C2",
            "C3",
        },
    };

    return data;
}

还有快速的 MyRenderer 类:

public class MyRenderer extends DefaultTableCellRenderer  
{ 
    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(row == 2 && column == 2)
        c.setBackground(new java.awt.Color(0, 0, 255)); 

    return c; 
} 

} 

除了这是可怕的代码并且破坏了许多最佳实践"类型的模式和技术(记住这只是我正在玩的东西)之外,是有什么我在这里做的事情很明显?为什么我没有得到表头(第一行- 1 2 3")?为什么我的默认单元格渲染器不能在我指定的特定 B2 单元格上工作?

Besides the fact that this is horrible code and breaks a lot of "best practices"-type patterns and techniques (remember this is just something I'm playing around with), is there anything I'm doing here that is glaringly-obvious? Why am I not getting a table header (first row "- 1 2 3")? Why is my default cell renderer not working on the specific B2 cell I am specifying?

JTables 似乎是奇怪的、美丽的和强大的野兽.我正在慢慢地思考它们,但对实施感到窒息.感谢任何可以提供帮助的人!

JTables seem to be strange, beautiful and powerful beasts. I'm slowly wrapping my mind around them but am choking on the implementation. Thanks to any that can help!

推荐答案

您需要确保将渲染器重置为其默认背景颜色(并处理行选择):

You need to make sure you reset the renderer to its default background color (and handle row selection):

if (! table.isRowSelected(row))
{
    if(row == 2 && column == 2)
        c.setBackground(new java.awt.Color(0, 0, 255));
    else
        c.setBackground(table.getBackground());
}

在未来发布一个适当的 SSCCE 与您的问题.

In the future post a proper SSCCE with your question.

这篇关于更改 Swing JTable 单元格颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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