0

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

DefaultTableCellRenderer renderer;
renderer = (DefaultTableCellRenderer)
            jTable2.getTableHeader().getDefaultRenderer();
renderer.setHorizontalAlignment(JLabel.CENTER);
user2692669
  • 461
  • 8
  • 23

2 Answers2

1

I suppose you can get reference from this question:java - How to center in JTable cell a value? - Stack Overflow
And the second answer center all columns with a loop always works properly for me.

P.S. just as @camickr mentioned, the second answer may cause the losing of the custom rendering.

According to How to Use Tables, if we use the constructor below.

  • JTable(Object[][] rowData, Object[] columnNames)
  • JTable(Vector rowData, Vector 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.

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

Community
  • 1
  • 1
by_unicorn
  • 36
  • 3
  • 1
    The second answer is not a good solution. Sure it works in you only have strings. But if you have numbers or dates in the table then you will lose the custom rendering of those objects. – camickr Nov 24 '13 at 20:48
  • I agree with @camickr . Additionally, that seems as a bypass to the problem, not a solution. – user2692669 Nov 24 '13 at 21:22
  • Hi, @camickr and user2692669, I have modified my answer, since my reputation is low and cannot add comments under others' post, I send an answer instead. Anyway, thanks a lot for your comments! Now I understand JTable's render better too. – by_unicorn Nov 25 '13 at 06:43
0

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 ).

    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);
        }
    }
OmAr
  • 97
  • 8