简体   繁体   English

如何一次对齐所有jtable单元格和标头?

[英]How to align all jtable cells and headers at once?

Is there an --> efficient <-- way to center everything at once ? 是否有-> 高效 <-方式一次所有内容居中

I don't mind if it's in the initialization part or after it, both are welcome. 我不在乎它是在初始化部分还是在初始化之后,都欢迎使用。

My code so far: 到目前为止,我的代码:

jTable2 = new javax.swing.JTable();

jTable2.setModel(new javax.swing.table.DefaultTableModel(
    new Object [][] {
    },
    new String [] {
        "header1", "header2", "header3"
    }
)); 

I managed to center the headers but centering everything piece by piece is just wrong :S 我设法使标题居中,但将所有内容逐个居中只是错误的:S

DefaultTableCellRenderer renderer;
renderer = (DefaultTableCellRenderer)
            jTable2.getTableHeader().getDefaultRenderer();
renderer.setHorizontalAlignment(JLabel.CENTER);

I suppose you can get reference from this question: java - How to center in JTable cell a value? 我想您可以从以下问题获得参考: java-如何在JTable单元中居中放置一个值? - Stack Overflow - 堆栈溢出
And the second answer center all columns with a loop always works properly for me. 第二个答案中心的所有带有循环的列始终对我有效。

PS just as @camickr mentioned, the second answer may cause the losing of the custom rendering. PS @camickr提到过,第二个答案可能会导致自定义渲染丢失。

According to How to Use Tables , if we use the constructor below. 根据如何使用表 ,如果我们使用下面的构造函数。

  • JTable(Object[][] rowData, Object[] columnNames) JTable(Object [] [] rowData,Object [] columnNames)
  • JTable(Vector rowData, Vector columnNames) JTable(向量rowData,向量columnNames)

Then they will treat all data types as string. 然后,他们会将所有数据类型视为字符串。 In this situation, the second answer will work well. 在这种情况下,第二个答案将很有效。

But if user has override the getColumnClass(int c) function in AbstractTableModel ( see here TableDemo.java ), then the second answer will cause the lose of default renderer: the default render will print Boolean type as a checkbox, after using the second answer, the table will print "true"/"false" instead. 但是,如果用户在AbstractTableModel中重写了getColumnClass(int c)函数( 请参见TableDemo.java ),则第二个答案将导致默认渲染器的丢失:使用第二个答案后,默认渲染器将布尔类型打印为复选框,表格将改为显示“ true” /“ false”。

So, if you want to use the default render and center the cell at the same time. 因此,如果要使用默认渲染并同时将单元居中。 This answer will help you: 该答案将帮助您:
java - Overriding JTable's DefaultTableCellRenderer to center all the cells in a JTable - Stack Overflow java-重写JTable的DefaultTableCellRenderer以将JTable中的所有单元格居中

it could be a little old post, but for whoever is searching the same problem as i did. 这可能是一个旧的帖子,但对于正在寻找与我一样的问题的人。

this for alot of tables to be easily aligned at once( i am kind of lazy at this things so i keep searching and trying for the easiest way :P ). 这使很多表可以一次轻松地对齐(我在这方面有点懒,所以我一直在寻找并尝试最简单的方法:P)。

    ArrayList<JTable> Tables = new ArrayList(); //JTable or javax.swing.JTable ... as it confused me as a beginner in Java codding

    Tables.add(jTable1);
    Tables.add(jTable2);
        //.........
        //.........

    DefaultTableCellRenderer centerRenderer_c = new DefaultTableCellRenderer();
    DefaultTableCellRenderer centerRenderer_h = new DefaultTableCellRenderer();
    centerRenderer_c.setHorizontalAlignment( javax.swing.JLabel.CENTER );

    for(JTable t : Tables){
        t.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);// this will reverse the headers visually 

        //for the headers :
        centerRenderer_h = (DefaultTableCellRenderer)t.getTableHeader().getDefaultRenderer();
        centerRenderer_h.setHorizontalAlignment( javax.swing.JLabel.CENTER );

        //for cells :
        for(int i=0; i < t.getColumnCount(); i++){
            t.getColumnModel().getColumn(i).setCellRenderer(centerRenderer_c);
        }
    }

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

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