简体   繁体   English

JTable彩色行标题出现在没有表格的地方

[英]JTable coloured row headings appearing where table is not

I am creating a JTable with 3 columns, and have set the colour of the headings to be color.CYAN. 我正在创建一个包含3列的JTable,并将标题的颜色设置为color.CYAN。

The problem is that after the JTable ends, the rest of the row is still coloured CYAN, even though there should not be a table there at all. 问题是在JTable结束之后,即使根本不存在任何表,该行的其余部分仍将显示为CYAN。

I have tried fixing the number of columns but cant seem to do it. 我尝试固定列数,但似乎无法做到。

Below is my code: 下面是我的代码:

class SimpleTableExample extends JFrame {

     public static JFrame frame = new JFrame();

     public static void frameGui(JPanel panel, String name){             
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);            
            frame.setContentPane(panel);    
            frame.setSize(500,200);      
            frame.setVisible(true);
     }       

    public static JPanel Table(){    
        JPanel topPanel = new JPanel();
        topPanel.setLayout(new BorderLayout());    
        String columnNames[] = { "Column 1", "Column 2", "Column 3" };    
        String dataValues[][] =
        {
            { "12", "234", "67" },
            { "-123", "43", "853" },
            { "93", "89.2", "109" },
            { "93", "89.2", "109" },
            { "93", "89.2", "109" },
            { "93", "89.2", "109" },
            { "93", "89.2", "109" },
            { "93", "89.2", "109" },
            { "93", "89.2", "109" },
            { "279", "9033", "3092" }
        };

        JTable table = new JTable( dataValues, columnNames );
        table.getColumnModel().getColumn(0).setPreferredWidth(100);
        table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );          
        JTableHeader header = table.getTableHeader();
        header.setBackground(Color.cyan);    
        JScrollPane scrollPane = new JScrollPane( table );
        topPanel.add( scrollPane, BorderLayout.CENTER );        
        return topPanel;    
    }

    public static void main( String args[] )
    {    
        frameGui(Table(),"TEST");
    }
}

That would be the expected behaviour, as the JTableHeader expands the entire width of the JScrollPane 's headers area. 这是预期的行为,因为JTableHeader扩展了JScrollPane的标头区域的整个宽度。

Instead, change the JTableHeade r's renderers, like you would the JTable 's dell renderers 相反,就像更改JTable的Dell渲染器一样,更改JTableHeade r的渲染器

Take a look at JTableHeader#setDefaultRenderer for more details 查看JTableHeader#setDefaultRenderer了解更多详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM