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

查看:93
本文介绍了更改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 class:

And the quick-n-dirty MyRenderer class:

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; 
} 

} 

除了这是可怕的代码并打破了许多最佳实践类型的模式和技术(记住这只是我正在玩的东西),我在这里做的任何东西都是明显的-obvious ?为什么我没有得到表头(第一行 - 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?

JTable似乎是奇怪,美丽和强大的野兽。我正在慢慢地把我的思绪包裹在他们周围,但是我对实施感到窒息。感谢任何有用的东西!

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

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

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