Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
162 views
in Technique[技术] by (71.8m points)

java - Changing Swing JTable Cell Colors

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

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

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.

The code:

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

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

} 

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 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!

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

In the future post a proper SSCCE with your question.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...