1

I have a table with 3 columns, the first column is special and contains a checkbox instead of a title.

The problem is that the layout (appareance) of the checkbox header is different from the other headers. Can anyone help?

The Code:

import java.awt.*;  
import javax.swing.*;  
import javax.swing.table.*;  
import java.awt.event.*; 

public class JTableHeaderCheckBox  
{  
  Object colNames[] = {"", "String", "String"};  
  Object[][] data = {};  
  DefaultTableModel dtm;  
  JTable table;  
  public void buildGUI()  
  {  
    dtm = new DefaultTableModel(data,colNames);  
    table = new JTable(dtm);  
    for(int x = 0; x < 2; x++)  
    {  
      dtm.addRow(new Object[]{new Boolean(false),"Row "+(x+1)+" Col 2","Row "+(x+1)+" Col 3"});  
    }  
    JScrollPane sp = new JScrollPane(table);  
    TableColumn tc = table.getColumnModel().getColumn(0);  
    tc.setCellEditor(table.getDefaultEditor(Boolean.class));  
    tc.setCellRenderer(table.getDefaultRenderer(Boolean.class));  
    tc.setHeaderRenderer(new CheckBoxHeader());  
    JFrame f = new JFrame();  
    f.getContentPane().add(sp);  
    f.pack();  
    f.setLocationRelativeTo(null);  
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    f.setVisible(true);  
  }  

  public static void main (String[] args)  
  {  
    SwingUtilities.invokeLater(new Runnable(){  
      public void run(){  
        new JTableHeaderCheckBox().buildGUI();  
      }  
    });  
  }  
}  

class CheckBoxHeader extends JCheckBox implements TableCellRenderer {  
    protected CheckBoxHeader rendererComponent;  
    protected int column;  

    public CheckBoxHeader() {  
        rendererComponent = this;   
    }  

    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {  
        setColumn(column);   
        return rendererComponent;  
    }  

    protected void setColumn(int column) {  
        this.column = column;  
    }  
    public int getColumn() {  
        return column;  
    }    
} 

The wierd output:

enter image description here

UPDATE:
What I want:

  1. Center align the checkbox in the header
  2. If you pay close attention, you notice the background of the checkbox Header (it seems popping to the inside) is different from the other 2 headers (they seem popping out)
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Adel Boutros
  • 10,205
  • 7
  • 55
  • 89

2 Answers2

4

If you're referring to the border of the checkbox you can try this to make the header look more consistent:

public CheckBoxHeader() {  
    rendererComponent = this;  

    setHorizontalAlignment(JLabel.CENTER);
    setBorderPaintedFlat(true);
    setBorderPainted(true);
}  
Adel Boutros
  • 10,205
  • 7
  • 55
  • 89
tenorsax
  • 21,123
  • 9
  • 60
  • 107
  • Ok, the border is fine now. How do I **center-align** the checkbox in the table header? If you answer this one, I will accept your answer. – Adel Boutros Feb 01 '12 at 17:37
  • I have modified your answer to add the solution for the center-alignment. Thanks for your answer – Adel Boutros Feb 01 '12 at 17:54
3

TableCellRenderer returns JLabel/JComponents and these JComponents haven't got implemented any LayoutManager, but there isn't any restictions for setting some of LayoutManagers

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • so how can I replicate the effect of the other table headers (String, String)? – Adel Boutros Jan 31 '12 at 19:32
  • If you are saying I should set a layout, may I ask how can I get the Layout used for the other 2 headers? – Adel Boutros Jan 31 '12 at 19:34
  • hehehe good question, that's only about that you put a JComponent to the TableHeader/Cell – mKorbel Jan 31 '12 at 19:43
  • TableHeader have got same logics as TableCell there is only about ColumnModel :-) – mKorbel Jan 31 '12 at 19:45
  • Sorry man but I am very confused and lost (Work Overload)!! Could you please provide me with a fix in the shape of code? – Adel Boutros Jan 31 '12 at 19:52
  • what do you want 1) center JCheckBox to CENTER ??? 2) align another TableColumn same way as is placed JCheckBox in the 1st column ??? – mKorbel Jan 31 '12 at 19:58
  • I answering by posting here simle shortcuts, very interesting question by @trashgod http://stackoverflow.com/questions/7137786/how-can-i-put-a-control-in-the-jtableheader-of-a-jtable, there are described more than layout issue – mKorbel Jan 31 '12 at 20:16
  • Max's answer was what I wanted, you have to set the horizontalAlignment, BorderPaintedFlat and BorderPainted. Anyway, thanks for your time and answer – Adel Boutros Feb 01 '12 at 17:54