What would be the approach to align additional components, such as a button, a label etc. under a specific column of the JTable? I would like to, for instance, add under the column with checkboxes, another checkbox that would select all the values in that particular column.
            Asked
            
        
        
            Active
            
        
            Viewed 1,755 times
        
    1
            
            
        - 
                    1What about a checkbox in the header? http://java-swing-tips.blogspot.de/2009/02/jtableheader-checkbox.html – Stephan Aug 02 '12 at 13:25
 - 
                    do you want to put JComponent under JTable alingh them with current column wight and resize them with column resize or reordering??? – mKorbel Aug 02 '12 at 15:26
 - 
                    I want to put them right under the columns and resize them with columns resizing – Bober02 Aug 02 '12 at 16:12
 
2 Answers
7
            as far as I understood, usage of TableColumnModelListener, with proper events from resize and reordering



import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class TableFilterRow extends JFrame implements TableColumnModelListener {
    private static final long serialVersionUID = 1L;
    private JTable table;
    private JPanel filterRow;   // Panel for text fields
    public TableFilterRow() {
        table = new JTable(3, 5);
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        JScrollPane scrollPane = new JScrollPane(table);
        getContentPane().add(scrollPane);
        table.getColumnModel().addColumnModelListener(this);
        filterRow = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
        for (int i = 0; i < table.getColumnCount(); i++) {
            filterRow.add(new JTextField(" Sum at - " + i));
        }
        columnMarginChanged(new ChangeEvent(table.getColumnModel()));
        getContentPane().add(filterRow, BorderLayout.SOUTH);
    }
    //  Implement TableColumnModelListener methods
    //  (Note: instead of implementing a listener you should be able to
    //  override the columnMarginChanged and columMoved methods of JTable)
    @Override
    public void columnMarginChanged(ChangeEvent e) {
        TableColumnModel tcm = table.getColumnModel();
        int columns = tcm.getColumnCount();
        for (int i = 0; i < columns; i++) {
            JTextField textField = (JTextField) filterRow.getComponent(i);
            Dimension d = textField.getPreferredSize();
            d.width = tcm.getColumn(i).getWidth();
            textField.setPreferredSize(d);
        }
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                filterRow.revalidate();
            }
        });
    }
    @Override
    public void columnMoved(TableColumnModelEvent e) {
        Component moved = filterRow.getComponent(e.getFromIndex());
        filterRow.remove(e.getFromIndex());
        filterRow.add(moved, e.getToIndex());
        filterRow.validate();
    }
    @Override
    public void columnAdded(TableColumnModelEvent e) {
    }
    @Override
    public void columnRemoved(TableColumnModelEvent e) {
    }
    @Override
    public void columnSelectionChanged(ListSelectionEvent e) {
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new TableFilterRow();
                frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}
        mKorbel
        
- 109,525
 - 20
 - 134
 - 319
 
1
            
            
        Cell alignment in a JTable is determined by the alignment of the Component used to render cells. For use in ordinary cells, DefaultTableCellRenderer is a JLabel, which supports comprehensive alignment properties; an example is seen here. For use in table header cells, you should first consider the caveats adduced here. Once certain that such a component is needed, this example may be helpful.
