Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Thanks both to afk and camickr who led me to my final decision to write a TableCellRendererWrapper:</p> <pre><code>public class ColumnAlignmentRenderer implements TableCellRenderer { private TableCellRenderer mWrappedRenderer; private int mDefaultAlignment = SwingConstants.LEFT; private Map&lt;Integer, Integer&gt; mSpecialColumnAlignmentMap = new Hashtable&lt;Integer, Integer&gt;(); public ColumnAlignmentRenderer(TableCellRenderer pWrappedRenderer) { mWrappedRenderer = pWrappedRenderer; } public Component getTableCellRendererComponent(JTable pTable, Object pValue, boolean pIsSelected, boolean pHasFocus, int pRow, int pColumn) { // Use the wrapped renderer Component renderedComponent = mWrappedRenderer.getTableCellRendererComponent(pTable, pValue, pIsSelected, pHasFocus, pRow, pColumn); // Set the alignment Integer alignment = mSpecialColumnAlignmentMap.get(pColumn); if (alignment != null) { ((JLabel)renderedComponent).setHorizontalAlignment(alignment); } else { ((JLabel)renderedComponent).setHorizontalAlignment(mDefaultAlignment); } return renderedComponent; } public void setSpecialColumnAlignment(Integer pColumn, Integer pAlignment) { mSpecialColumnAlignmentMap.put(pColumn, pAlignment); } public void setDefaultAlignment(int pAlignment) { mDefaultAlignment = pAlignment; } } </code></pre> <p>It's not perfect, but it gets the job done for me. To use it I do this:</p> <pre><code> // Center alignment for "selected" column ColumnAlignmentRenderer cellRenderer = new ColumnAlignmentRenderer(getTable().getDefaultRenderer(Object.class)); cellRenderer.setSpecialColumnAlignment(MyTableModel.COL_TO_CENTER, SwingConstants.CENTER); getTable().setDefaultRenderer(Object.class, cellRenderer); // Center alignmet for "selected" column header ColumnAlignmentRenderer headerRenderer = new ColumnAlignmentRenderer(getTable().getTableHeader().getDefaultRenderer()); headerRenderer.setSpecialColumnAlignment(MyTableModel.COL_TO_CENTER, SwingConstants.CENTER); getTable().getTableHeader().setDefaultRenderer(headerRenderer); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload