Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Himesh Sharma
Added: Feb 5, 2021 8:07 AM
Modified: Feb 5, 2021 8:12 AM
Views: 6
  1. private void AlignCenter(JTable jTable, int[] colNo) {
  2.    ColumnAlignmentRenderer cellRenderer = new ColumnAlignmentRenderer(jTable.getDefaultRenderer(Object.class));
  3.    for (int i = 0; i < colNo.length; i++) {
  4.        cellRenderer.setSpecialColumnAlignment(colNo[i], SwingConstants.CENTER);
  5.    }
  6.    jTable.setDefaultRenderer(Object.class, cellRenderer);
  7.  
  8.    // Center alignmet for "selected" column header
  9.    ColumnAlignmentRenderer headerRenderer = new ColumnAlignmentRenderer(jTable.getTableHeader().getDefaultRenderer());
  10.    for (int i = 0; i < colNo.length; i++) {
  11.        headerRenderer.setSpecialColumnAlignment(colNo[i], SwingConstants.CENTER);
  12.    }
  13.    jTable.getTableHeader().setDefaultRenderer(headerRenderer);
  14. }
  15.  
  16. //ColumnAlignmentRenderer
  17. public class ColumnAlignmentRenderer implements TableCellRenderer {
  18.  
  19.     private TableCellRenderer mWrappedRenderer;
  20.     private int mDefaultAlignment = SwingConstants.LEFT;
  21.     private Map<Integer, Integer> mSpecialColumnAlignmentMap = new Hashtable<Integer, Integer>();
  22.  
  23.     public ColumnAlignmentRenderer(TableCellRenderer pWrappedRenderer) {
  24.         mWrappedRenderer = pWrappedRenderer;
  25.     }
  26.  
  27.     @Override
  28.     public Component getTableCellRendererComponent(JTable pTable, Object pValue, boolean pIsSelected,
  29.             boolean pHasFocus, int pRow, int pColumn) {
  30.  
  31.         // Use the wrapped renderer
  32.         Component renderedComponent = mWrappedRenderer.getTableCellRendererComponent(pTable, pValue, pIsSelected, pHasFocus,
  33.                 pRow, pColumn);
  34.  
  35.         // Set the alignment
  36.         Integer alignment = mSpecialColumnAlignmentMap.get(pColumn);
  37.         if (alignment != null) {
  38.             ((JLabel)renderedComponent).setHorizontalAlignment(alignment);
  39.         } else {
  40.             ((JLabel)renderedComponent).setHorizontalAlignment(mDefaultAlignment);
  41.         }
  42.  
  43.         return renderedComponent;
  44.     }
  45.  
  46.     public void setSpecialColumnAlignment(Integer pColumn, Integer pAlignment) {
  47.         mSpecialColumnAlignmentMap.put(pColumn, pAlignment);
  48.     }
  49.  
  50.     public void setDefaultAlignment(int pAlignment) {
  51.         mDefaultAlignment = pAlignment;
  52.     }
  53.    
  54. }
  55.  
  56. //USAGE: Center alignmet for "selected" column header
  57. ColumnAlignmentRenderer headerRenderer = new ColumnAlignmentRenderer(jTable.getTableHeader().getDefaultRenderer());
  58. for (int i = 0; i < colNo.length; i++) {
  59.   headerRenderer.setSpecialColumnAlignment(colNo[i], SwingConstants.CENTER);
  60. }
  61. jTable.getTableHeader().setDefaultRenderer(headerRenderer);
  62.