Java JTable不显示表头的解决方法

在Java的swing中,如果创建了新的面板,在新面板中布置表格控件将不会显示表头,所以需要设置一下面板布局。并且手动设置表头。

默认在子面板中创建的

   String[] jTable_Permissions_colname={"编号","英文名","权限名称","权限注释"};
        String[][] jTable_Permissions_Values={{"1","2","3","4"},{"1","2","3","4"}};
        jTable_Permissions=new JTable(jTable_Permissions_Values,jTable_Permissions_colname);
        JTableHeader jTableHeader=jTable_Permissions.getTableHeader();
        jPanel_Permissions.add(jTableHeader);
        jPanel_Permissions.add(jTable_Permissions);

为面板手动添加JTableHeader 可以看到显示了表头了,但是并不会是充满容器。

给面板设置布局即可

String[] jTable_Permissions_colname={"编号","英文名","权限名称","权限注释"};
        String[][] jTable_Permissions_Values={{"1","2","3","4"},{"1","2","3","4"}};
        jTable_Permissions=new JTable(jTable_Permissions_Values,jTable_Permissions_colname);
        jPanel_Permissions.setLayout(new BorderLayout());
        JTableHeader jTableHeader=jTable_Permissions.getTableHeader();
        jPanel_Permissions.add(jTableHeader,BorderLayout.NORTH);
        jPanel_Permissions.add(jTable_Permissions,BorderLayout.CENTER);

 

暂无评论

发表评论

电子邮件地址不会被公开。 必填项已用*标注